mirror of
https://github.com/micahqcade/custom_vesync.git
synced 2025-05-08 15:56:30 +02:00
Refactor devices detection and classification. (#55)
* Add lv600s device * refactor and simplify code.
This commit is contained in:
parent
f15e78e7af
commit
0b5810e272
@ -8,8 +8,10 @@ from pyvesync.vesyncfan import model_features
|
|||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
VS_BINARY_SENSORS,
|
VS_BINARY_SENSORS,
|
||||||
|
VS_FAN_TYPES,
|
||||||
VS_FANS,
|
VS_FANS,
|
||||||
VS_HUMIDIFIERS,
|
VS_HUMIDIFIERS,
|
||||||
|
VS_HUMIDIFIERS_TYPES,
|
||||||
VS_LIGHTS,
|
VS_LIGHTS,
|
||||||
VS_NUMBERS,
|
VS_NUMBERS,
|
||||||
VS_SENSORS,
|
VS_SENSORS,
|
||||||
@ -26,12 +28,12 @@ def has_feature(device, dictionary, attribute):
|
|||||||
|
|
||||||
def is_humidifier(device_type: str) -> bool:
|
def is_humidifier(device_type: str) -> bool:
|
||||||
"""Return true if the device type is a humidifier."""
|
"""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:
|
def is_air_purifier(device_type: str) -> bool:
|
||||||
"""Return true if the device type is a an air purifier."""
|
"""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):
|
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)
|
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(
|
_LOGGER.debug(
|
||||||
"Found the following devices: %s",
|
"Found the following devices: %s",
|
||||||
async_redact_data(
|
redacted,
|
||||||
{k: [d.__dict__ for d in v] for k, v in manager._dev_list.items()},
|
|
||||||
["cid", "uuid", "mac_id"],
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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:
|
if manager.fans:
|
||||||
for fan in manager.fans:
|
for fan in manager.fans:
|
||||||
# VeSync classifies humidifiers as fans
|
# VeSync classifies humidifiers as fans
|
||||||
if is_humidifier(fan.device_type):
|
if is_humidifier(fan.device_type):
|
||||||
devices[VS_HUMIDIFIERS].append(fan)
|
devices[VS_HUMIDIFIERS].append(fan)
|
||||||
else:
|
elif is_air_purifier(fan.device_type):
|
||||||
devices[VS_FANS].append(fan)
|
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_NUMBERS].append(fan)
|
||||||
devices[VS_SWITCHES].append(fan)
|
devices[VS_SWITCHES].append(fan)
|
||||||
devices[VS_SENSORS].append(fan)
|
devices[VS_SENSORS].append(fan)
|
||||||
|
@ -23,26 +23,19 @@ VS_MODE_SLEEP = "sleep"
|
|||||||
|
|
||||||
VS_TO_HA_ATTRIBUTES = {"humidity": "current_humidity"}
|
VS_TO_HA_ATTRIBUTES = {"humidity": "current_humidity"}
|
||||||
|
|
||||||
|
VS_FAN_TYPES = ["VeSyncAirBypass", "VeSyncAir131"]
|
||||||
|
VS_HUMIDIFIERS_TYPES = ["VeSyncHumid200300S", "VeSyncHumid200S"]
|
||||||
|
|
||||||
DEV_TYPE_TO_HA = {
|
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",
|
"ESL100": "bulb-dimmable",
|
||||||
"ESL100CW": "bulb-tunable-white",
|
"ESL100CW": "bulb-tunable-white",
|
||||||
"wifi-switch-1.3": "outlet",
|
"ESO15-TB": "outlet",
|
||||||
"ESW03-USA": "outlet",
|
"ESW03-USA": "outlet",
|
||||||
"ESW01-EU": "outlet",
|
"ESW01-EU": "outlet",
|
||||||
"ESW15-USA": "outlet",
|
"ESW15-USA": "outlet",
|
||||||
|
"wifi-switch-1.3": "outlet",
|
||||||
"ESWL01": "switch",
|
"ESWL01": "switch",
|
||||||
"ESWL03": "switch",
|
"ESWL03": "switch",
|
||||||
"ESO15-TB": "outlet",
|
"ESD16": "walldimmer",
|
||||||
|
"ESWD16": "walldimmer",
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Support for VeSync fans."""
|
"""Support for VeSync fans."""
|
||||||
import logging
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
||||||
@ -15,10 +14,8 @@ from homeassistant.util.percentage import (
|
|||||||
|
|
||||||
from .common import VeSyncDevice, has_feature
|
from .common import VeSyncDevice, has_feature
|
||||||
from .const import (
|
from .const import (
|
||||||
DEV_TYPE_TO_HA,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
VS_DISCOVERY,
|
VS_DISCOVERY,
|
||||||
VS_FAN,
|
|
||||||
VS_FANS,
|
VS_FANS,
|
||||||
VS_LEVELS,
|
VS_LEVELS,
|
||||||
VS_MODE_AUTO,
|
VS_MODE_AUTO,
|
||||||
@ -28,8 +25,6 @@ from .const import (
|
|||||||
VS_TO_HA_ATTRIBUTES,
|
VS_TO_HA_ATTRIBUTES,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -55,18 +50,7 @@ async def async_setup_entry(
|
|||||||
@callback
|
@callback
|
||||||
def _setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
entities = []
|
async_add_entities([VeSyncFanHA(dev) for dev in devices], update_before_add=True)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
class VeSyncFanHA(VeSyncDevice, FanEntity):
|
class VeSyncFanHA(VeSyncDevice, FanEntity):
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Support for VeSync humidifiers."""
|
"""Support for VeSync humidifiers."""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components.humidifier import HumidifierEntity
|
from homeassistant.components.humidifier import HumidifierEntity
|
||||||
from homeassistant.components.humidifier.const import (
|
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.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import VeSyncDevice, is_humidifier
|
from .common import VeSyncDevice
|
||||||
from .const import (
|
from .const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
VS_DISCOVERY,
|
VS_DISCOVERY,
|
||||||
@ -23,8 +22,6 @@ from .const import (
|
|||||||
VS_TO_HA_ATTRIBUTES,
|
VS_TO_HA_ATTRIBUTES,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
MAX_HUMIDITY = 80
|
MAX_HUMIDITY = 80
|
||||||
MIN_HUMIDITY = 30
|
MIN_HUMIDITY = 30
|
||||||
|
|
||||||
@ -55,17 +52,9 @@ async def async_setup_entry(
|
|||||||
@callback
|
@callback
|
||||||
def _setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
entities = []
|
async_add_entities(
|
||||||
for dev in devices:
|
[VeSyncHumidifierHA(dev) for dev in devices], update_before_add=True
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
class VeSyncHumidifierHA(VeSyncDevice, HumidifierEntity):
|
class VeSyncHumidifierHA(VeSyncDevice, HumidifierEntity):
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Support for number settings on VeSync devices."""
|
"""Support for number settings on VeSync devices."""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components.number import NumberEntity
|
from homeassistant.components.number import NumberEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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 import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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
|
from .const import DOMAIN, VS_DISCOVERY, VS_NUMBERS
|
||||||
|
|
||||||
MAX_HUMIDITY = 80
|
MAX_HUMIDITY = 80
|
||||||
MIN_HUMIDITY = 30
|
MIN_HUMIDITY = 30
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -61,10 +58,6 @@ class VeSyncNumberEntity(VeSyncBaseEntity, NumberEntity):
|
|||||||
def __init__(self, device):
|
def __init__(self, device):
|
||||||
"""Initialize the VeSync fan device."""
|
"""Initialize the VeSync fan device."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
if is_air_purifier(device.device_type):
|
|
||||||
self.smartfan = device
|
|
||||||
if is_humidifier(device.device_type):
|
|
||||||
self.smarthumidifier = device
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def entity_category(self):
|
def entity_category(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user