Refactor devices detection and classification. (#55)

* Add lv600s device

* refactor and simplify code.
This commit is contained in:
Vincent Le Bourlot 2022-11-08 08:34:52 +01:00 committed by GitHub
parent f15e78e7af
commit 0b5810e272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 61 deletions

View File

@ -8,8 +8,10 @@ from pyvesync.vesyncfan import model_features
from .const import (
DOMAIN,
VS_BINARY_SENSORS,
VS_FAN_TYPES,
VS_FANS,
VS_HUMIDIFIERS,
VS_HUMIDIFIERS_TYPES,
VS_LIGHTS,
VS_NUMBERS,
VS_SENSORS,
@ -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):
@ -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)

View File

@ -23,26 +23,19 @@ VS_MODE_SLEEP = "sleep"
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",
}

View File

@ -1,5 +1,4 @@
"""Support for VeSync fans."""
import logging
import math
from homeassistant.components.fan import FanEntity, FanEntityFeature
@ -15,10 +14,8 @@ from homeassistant.util.percentage import (
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,
@ -28,8 +25,6 @@ from .const import (
VS_TO_HA_ATTRIBUTES,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
@ -55,18 +50,7 @@ async def async_setup_entry(
@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):

View File

@ -1,5 +1,4 @@
"""Support for VeSync humidifiers."""
import logging
from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import (
@ -13,7 +12,7 @@ from homeassistant.core import HomeAssistant, callback
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,
@ -23,8 +22,6 @@ from .const import (
VS_TO_HA_ATTRIBUTES,
)
_LOGGER = logging.getLogger(__name__)
MAX_HUMIDITY = 80
MIN_HUMIDITY = 30
@ -55,17 +52,9 @@ async def async_setup_entry(
@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):

View File

@ -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
@ -8,14 +7,12 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
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,
@ -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):