Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update from upstream #1

Merged
merged 6 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ logger:
pyvesync: debug
```

This integration is heavily based on [VeSync_bpo](https://github.com/borpin/vesync-bpo) and [pyvesync](https://pypi.org/project/pyvesync/)
This integration is heavily based on [VeSync_bpo](https://github.com/borpin/vesync-bpo) and [pyvesync](https://github.com/webdjoe/pyvesync)
32 changes: 25 additions & 7 deletions custom_components/vesync/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from .const import (
DOMAIN,
VS_BINARY_SENSORS,
VS_FAN_TYPES,
VS_FANS,
VS_HUMIDIFIERS,
VS_HUMIDIFIERS_TYPES,
VS_LIGHTS,
VS_NUMBERS,
VS_SENSORS,
Expand All @@ -26,12 +28,12 @@ def has_feature(device, dictionary, attribute):

def is_humidifier(device_type: str) -> bool:
"""Return true if the device type is a humidifier."""
return model_features(device_type)["module"].find("VeSyncHumid") > -1
return model_features(device_type)["module"] in VS_HUMIDIFIERS_TYPES


def is_air_purifier(device_type: str) -> bool:
"""Return true if the device type is a an air purifier."""
return model_features(device_type)["module"].find("VeSyncAirBypass") > -1
return model_features(device_type)["module"] in VS_FAN_TYPES


async def async_process_devices(hass, manager):
Expand All @@ -47,22 +49,38 @@ async def async_process_devices(hass, manager):
}

await hass.async_add_executor_job(manager.update)
redacted = async_redact_data(
{k: [d.__dict__ for d in v] for k, v in manager._dev_list.items()},
["cid", "uuid", "mac_id"],
)

_LOGGER.debug(
"Found the following devices: %s",
async_redact_data(
{k: [d.__dict__ for d in v] for k, v in manager._dev_list.items()},
["cid", "uuid", "mac_id"],
),
redacted,
)

if (
manager.fans is None
and manager.bulbs is None
and manager.outlets is None
and manager.switches is None
):
_LOGGER.error("Could not find any device to add in %s", redacted)

if manager.fans:
for fan in manager.fans:
# VeSync classifies humidifiers as fans
if is_humidifier(fan.device_type):
devices[VS_HUMIDIFIERS].append(fan)
else:
elif is_air_purifier(fan.device_type):
devices[VS_FANS].append(fan)
else:
_LOGGER.warning(
"Unknown device type %s %s (enable debug for more info)",
fan.device_name,
fan.device_type,
)
continue
devices[VS_NUMBERS].append(fan)
devices[VS_SWITCHES].append(fan)
devices[VS_SENSORS].append(fan)
Expand Down
21 changes: 7 additions & 14 deletions custom_components/vesync/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,19 @@

VS_TO_HA_ATTRIBUTES = {"humidity": "current_humidity"}

VS_FAN_TYPES = ["VeSyncAirBypass", "VeSyncAir131"]
VS_HUMIDIFIERS_TYPES = ["VeSyncHumid200300S", "VeSyncHumid200S"]

DEV_TYPE_TO_HA = {
"Core200S": "fan",
"Core300S": "fan",
"Core400S": "fan",
"LAP-C201S-AUSR": "fan",
"LAP-C202S-WUSR": "fan",
"LAP-C401S-WUSR": "fan",
"LAP-C601S-WUS": "fan",
"LAP-C601S-WEU": "fan",
"LV-PUR131S": "fan",
"Classic300S": "humidifier",
"ESD16": "walldimmer",
"ESWD16": "walldimmer",
"ESL100": "bulb-dimmable",
"ESL100CW": "bulb-tunable-white",
"wifi-switch-1.3": "outlet",
"ESO15-TB": "outlet",
"ESW03-USA": "outlet",
"ESW01-EU": "outlet",
"ESW15-USA": "outlet",
"wifi-switch-1.3": "outlet",
"ESWL01": "switch",
"ESWL03": "switch",
"ESO15-TB": "outlet",
"ESD16": "walldimmer",
"ESWD16": "walldimmer",
}
100 changes: 100 additions & 0 deletions custom_components/vesync/device_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Provides device actions for Humidifier."""
from __future__ import annotations

import logging

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.components.device_automation import toggle_entity
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_MODE,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_ENTITY_ID,
CONF_TYPE,
)
from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity import get_capability
from homeassistant.helpers.typing import ConfigType, TemplateVarsType

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

# mypy: disallow-any-generics

SET_MODE_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
{
vol.Required(CONF_TYPE): "set_mode",
vol.Required(CONF_ENTITY_ID): cv.entity_domain("fan"),
vol.Required(ATTR_MODE): cv.string,
}
)

ACTION_SCHEMA = vol.Any(SET_MODE_SCHEMA)


async def async_get_actions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
"""List device actions for Humidifier devices."""
registry = entity_registry.async_get(hass)
actions = await toggle_entity.async_get_actions(hass, device_id, DOMAIN)

# Get all the integrations entities for this device
for entry in entity_registry.async_entries_for_device(registry, device_id):
if entry.domain != "fan":
continue

base_action = {
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
}

actions.append({**base_action, CONF_TYPE: "set_mode"})

return actions


async def async_call_action_from_config(
hass: HomeAssistant,
config: ConfigType,
variables: TemplateVarsType,
context: Context | None,
) -> None:
"""Execute a device action."""
service_data = {ATTR_ENTITY_ID: config[CONF_ENTITY_ID]}

if config[CONF_TYPE] != "set_mode":
return await toggle_entity.async_call_action_from_config(
hass, config, variables, context, DOMAIN
)

service = "set_preset_mode"
service_data["preset_mode"] = config[ATTR_MODE]
await hass.services.async_call(
"fan", service, service_data, blocking=True, context=context
)


async def async_get_action_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List action capabilities."""
action_type = config[CONF_TYPE]

if action_type != "set_mode":
return {}

try:
available_modes = (
get_capability(hass, config[ATTR_ENTITY_ID], "preset_modes") or []
)
except HomeAssistantError:
available_modes = []
fields = {vol.Required(ATTR_MODE): vol.In(available_modes)}
return {"extra_fields": vol.Schema(fields)}
18 changes: 1 addition & 17 deletions custom_components/vesync/fan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Support for VeSync fans."""
import logging
import math

from homeassistant.components.fan import FanEntity, FanEntityFeature
Expand All @@ -15,10 +14,8 @@

from .common import VeSyncDevice, has_feature
from .const import (
DEV_TYPE_TO_HA,
DOMAIN,
VS_DISCOVERY,
VS_FAN,
VS_FANS,
VS_LEVELS,
VS_MODE_AUTO,
Expand All @@ -28,8 +25,6 @@
VS_TO_HA_ATTRIBUTES,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -55,18 +50,7 @@ def discover(devices):
@callback
def _setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
entities = []
for dev in devices:
_LOGGER.debug("Adding device %s %s", dev.device_name, dev.device_type)
if DEV_TYPE_TO_HA.get(dev.device_type) == VS_FAN:
entities.append(VeSyncFanHA(dev))
else:
_LOGGER.warning(
"Unknown device type %s %s", dev.device_name, dev.device_type
)
continue

async_add_entities(entities, update_before_add=True)
async_add_entities([VeSyncFanHA(dev) for dev in devices], update_before_add=True)


class VeSyncFanHA(VeSyncDevice, FanEntity):
Expand Down
19 changes: 4 additions & 15 deletions custom_components/vesync/humidifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Support for VeSync humidifiers."""
import logging

from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import (
Expand All @@ -13,7 +12,7 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .common import VeSyncDevice, is_humidifier
from .common import VeSyncDevice
from .const import (
DOMAIN,
VS_DISCOVERY,
Expand All @@ -23,8 +22,6 @@
VS_TO_HA_ATTRIBUTES,
)

_LOGGER = logging.getLogger(__name__)

MAX_HUMIDITY = 80
MIN_HUMIDITY = 30

Expand Down Expand Up @@ -55,17 +52,9 @@ def discover(devices):
@callback
def _setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
entities = []
for dev in devices:
if is_humidifier(dev.device_type):
entities.append(VeSyncHumidifierHA(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
continue

async_add_entities(entities, update_before_add=True)
async_add_entities(
[VeSyncHumidifierHA(dev) for dev in devices], update_before_add=True
)


class VeSyncHumidifierHA(VeSyncDevice, HumidifierEntity):
Expand Down
3 changes: 2 additions & 1 deletion custom_components/vesync/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"name": "VeSync",
"documentation": "https://www.home-assistant.io/integrations/vesync",
"codeowners": ["@markperdue", "@webdjoe", "@thegardenmonkey", "@vlebourl"],
"requirements": ["git+https://github.com/webdjoe/pyvesync.git@master#pyvesync==2.1.0"],
"config_flow": true,
"iot_class": "cloud_polling",
"version": "0.1.15",
"version": "0.2.1",
"issue_tracker": "https://github.com/vlebourl/custom_vesync",
"dhcp": [
{
Expand Down
9 changes: 1 addition & 8 deletions custom_components/vesync/number.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Support for number settings on VeSync devices."""
import logging

from homeassistant.components.number import NumberEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -8,14 +7,12 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .common import VeSyncBaseEntity, has_feature, is_air_purifier, is_humidifier
from .common import VeSyncBaseEntity, has_feature
from .const import DOMAIN, VS_DISCOVERY, VS_NUMBERS

MAX_HUMIDITY = 80
MIN_HUMIDITY = 30

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
Expand Down Expand Up @@ -61,10 +58,6 @@ class VeSyncNumberEntity(VeSyncBaseEntity, NumberEntity):
def __init__(self, device):
"""Initialize the VeSync fan device."""
super().__init__(device)
if is_air_purifier(device.device_type):
self.smartfan = device
if is_humidifier(device.device_type):
self.smarthumidifier = device

@property
def entity_category(self):
Expand Down
6 changes: 6 additions & 0 deletions custom_components/vesync/strings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"title": "VeSync Integration for Home Assistant",
"device_automation": {
"action_type": {
"set_mode": "Change mode on {entity_name}"
}
},
"config": {
"flow_title": "Gateway: {gateway_id}",
"step": {
Expand Down
5 changes: 5 additions & 0 deletions custom_components/vesync/translations/bg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"device_automation": {
"action_type": {
"set_mode": "Change mode on {entity_name}."
}
},
"config": {
"step": {
"user": {
Expand Down
5 changes: 5 additions & 0 deletions custom_components/vesync/translations/ca.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"device_automation": {
"action_type": {
"set_mode": "Change mode on {entity_name}."
}
},
"config": {
"abort": {
"single_instance_allowed": "Ja configurat. Nom\u00e9s \u00e9s possible una sola configuraci\u00f3."
Expand Down
5 changes: 5 additions & 0 deletions custom_components/vesync/translations/cs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"device_automation": {
"action_type": {
"set_mode": "Change mode on {entity_name}."
}
},
"config": {
"abort": {
"single_instance_allowed": "Ji\u017e nastaveno. Je mo\u017en\u00e1 pouze jedin\u00e1 konfigurace."
Expand Down
5 changes: 5 additions & 0 deletions custom_components/vesync/translations/da.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"device_automation": {
"action_type": {
"set_mode": "Change mode on {entity_name}."
}
},
"config": {
"step": {
"user": {
Expand Down
Loading