2022-04-01 15:02:13 +02:00
|
|
|
"""Common utilities for VeSync Component."""
|
|
|
|
import logging
|
|
|
|
|
2024-03-21 11:44:36 +01:00
|
|
|
from pyvesync.vesyncfan import model_features as fan_model_features
|
|
|
|
from pyvesync.vesynckitchen import model_features as kitchen_model_features
|
|
|
|
|
2022-08-03 11:59:11 +02:00
|
|
|
from homeassistant.components.diagnostics import async_redact_data
|
2022-04-01 15:02:13 +02:00
|
|
|
from homeassistant.helpers.entity import Entity, ToggleEntity
|
2023-04-28 09:57:43 +02:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2022-04-01 15:02:13 +02:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
2023-10-27 08:33:05 +01:00
|
|
|
VS_AIRFRYER_TYPES,
|
2022-04-01 15:02:13 +02:00
|
|
|
VS_BINARY_SENSORS,
|
2023-10-27 08:33:05 +01:00
|
|
|
VS_BUTTON,
|
2022-11-08 08:34:52 +01:00
|
|
|
VS_FAN_TYPES,
|
2022-04-01 15:02:13 +02:00
|
|
|
VS_FANS,
|
|
|
|
VS_HUMIDIFIERS,
|
2022-11-08 08:34:52 +01:00
|
|
|
VS_HUMIDIFIERS_TYPES,
|
2022-04-01 15:02:13 +02:00
|
|
|
VS_LIGHTS,
|
|
|
|
VS_NUMBERS,
|
|
|
|
VS_SENSORS,
|
|
|
|
VS_SWITCHES,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-07-30 09:59:00 +02:00
|
|
|
def has_feature(device, dictionary, attribute):
|
|
|
|
"""Return the detail of the attribute."""
|
2022-08-17 11:36:40 +02:00
|
|
|
return getattr(device, dictionary, {}).get(attribute, None) is not None
|
2022-07-30 09:59:00 +02:00
|
|
|
|
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
async def async_process_devices(hass, manager):
|
|
|
|
"""Assign devices to proper component."""
|
|
|
|
devices = {
|
|
|
|
VS_SWITCHES: [],
|
|
|
|
VS_FANS: [],
|
|
|
|
VS_LIGHTS: [],
|
|
|
|
VS_SENSORS: [],
|
|
|
|
VS_HUMIDIFIERS: [],
|
|
|
|
VS_NUMBERS: [],
|
|
|
|
VS_BINARY_SENSORS: [],
|
2023-10-27 08:33:05 +01:00
|
|
|
VS_BUTTON: [],
|
2022-04-01 15:02:13 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 08:34:52 +01:00
|
|
|
redacted = async_redact_data(
|
|
|
|
{k: [d.__dict__ for d in v] for k, v in manager._dev_list.items()},
|
|
|
|
["cid", "uuid", "mac_id"],
|
|
|
|
)
|
2022-04-01 15:02:13 +02:00
|
|
|
|
2023-10-27 08:33:05 +01:00
|
|
|
_LOGGER.warning(
|
2022-08-17 11:36:40 +02:00
|
|
|
"Found the following devices: %s",
|
2022-11-08 08:34:52 +01:00
|
|
|
redacted,
|
2022-08-17 11:36:40 +02:00
|
|
|
)
|
2022-08-03 11:59:11 +02:00
|
|
|
|
2022-11-08 08:34:52 +01:00
|
|
|
if (
|
2023-10-27 08:33:05 +01:00
|
|
|
manager.bulbs is None
|
|
|
|
and manager.fans is None
|
|
|
|
and manager.kitchen is None
|
2022-11-08 08:34:52 +01:00
|
|
|
and manager.outlets is None
|
|
|
|
and manager.switches is None
|
|
|
|
):
|
|
|
|
_LOGGER.error("Could not find any device to add in %s", redacted)
|
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
if manager.fans:
|
|
|
|
for fan in manager.fans:
|
|
|
|
# VeSync classifies humidifiers as fans
|
2023-10-27 08:33:05 +01:00
|
|
|
if fan_model_features(fan.device_type)["module"] in VS_HUMIDIFIERS_TYPES:
|
2022-04-01 15:02:13 +02:00
|
|
|
devices[VS_HUMIDIFIERS].append(fan)
|
2023-10-27 08:33:05 +01:00
|
|
|
elif fan_model_features(fan.device_type)["module"] in VS_FAN_TYPES:
|
2022-04-01 15:02:13 +02:00
|
|
|
devices[VS_FANS].append(fan)
|
2022-11-08 08:34:52 +01:00
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
2023-10-27 08:33:05 +01:00
|
|
|
"Unknown fan type %s %s (enable debug for more info)",
|
2022-11-08 08:34:52 +01:00
|
|
|
fan.device_name,
|
|
|
|
fan.device_type,
|
|
|
|
)
|
|
|
|
continue
|
2022-07-30 09:59:00 +02:00
|
|
|
devices[VS_NUMBERS].append(fan)
|
|
|
|
devices[VS_SWITCHES].append(fan)
|
|
|
|
devices[VS_SENSORS].append(fan)
|
|
|
|
devices[VS_BINARY_SENSORS].append(fan)
|
|
|
|
devices[VS_LIGHTS].append(fan)
|
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
if manager.bulbs:
|
|
|
|
devices[VS_LIGHTS].extend(manager.bulbs)
|
|
|
|
|
|
|
|
if manager.outlets:
|
|
|
|
devices[VS_SWITCHES].extend(manager.outlets)
|
|
|
|
# Expose outlets' power & energy usage as separate sensors
|
|
|
|
devices[VS_SENSORS].extend(manager.outlets)
|
|
|
|
|
|
|
|
if manager.switches:
|
|
|
|
for switch in manager.switches:
|
|
|
|
if not switch.is_dimmable():
|
|
|
|
devices[VS_SWITCHES].append(switch)
|
|
|
|
else:
|
|
|
|
devices[VS_LIGHTS].append(switch)
|
|
|
|
|
2023-10-27 08:33:05 +01:00
|
|
|
if manager.kitchen:
|
|
|
|
for airfryer in manager.kitchen:
|
|
|
|
if (
|
|
|
|
kitchen_model_features(airfryer.device_type)["module"]
|
|
|
|
in VS_AIRFRYER_TYPES
|
|
|
|
):
|
|
|
|
_LOGGER.warning(
|
2024-03-21 11:44:36 +01:00
|
|
|
"Found air fryer %s, support in progress.\n", airfryer.device_name
|
2023-10-27 08:33:05 +01:00
|
|
|
)
|
|
|
|
devices[VS_SENSORS].append(airfryer)
|
|
|
|
devices[VS_BINARY_SENSORS].append(airfryer)
|
|
|
|
devices[VS_SWITCHES].append(airfryer)
|
|
|
|
devices[VS_BUTTON].append(airfryer)
|
|
|
|
else:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Unknown device type %s %s (enable debug for more info)",
|
|
|
|
airfryer.device_name,
|
|
|
|
airfryer.device_type,
|
|
|
|
)
|
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
return devices
|
|
|
|
|
|
|
|
|
2023-04-28 09:57:43 +02:00
|
|
|
class VeSyncBaseEntity(CoordinatorEntity, Entity):
|
2022-04-01 15:02:13 +02:00
|
|
|
"""Base class for VeSync Entity Representations."""
|
|
|
|
|
2023-10-27 08:33:05 +01:00
|
|
|
def __init__(self, device, coordinator) -> None:
|
2022-04-01 15:02:13 +02:00
|
|
|
"""Initialize the VeSync device."""
|
|
|
|
self.device = device
|
2023-04-28 09:57:43 +02:00
|
|
|
super().__init__(coordinator, context=device)
|
2022-04-01 15:02:13 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def base_unique_id(self):
|
|
|
|
"""Return the ID of this device."""
|
|
|
|
if isinstance(self.device.sub_device_no, int):
|
|
|
|
return f"{self.device.cid}{str(self.device.sub_device_no)}"
|
|
|
|
return self.device.cid
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the ID of this device."""
|
|
|
|
# The unique_id property may be overridden in subclasses, such as in sensors. Maintaining base_unique_id allows
|
|
|
|
# us to group related entities under a single device.
|
|
|
|
return self.base_unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def base_name(self):
|
|
|
|
"""Return the name of the device."""
|
2024-06-03 14:55:56 +02:00
|
|
|
return self.device.device_name
|
2022-04-01 15:02:13 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the entity (may be overridden)."""
|
|
|
|
return self.base_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if device is available."""
|
|
|
|
return self.device.connection_status == "online"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device information."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self.base_unique_id)},
|
|
|
|
"name": self.base_name,
|
|
|
|
"model": self.device.device_type,
|
2023-08-05 12:20:27 -04:00
|
|
|
"manufacturer": "VeSync",
|
2022-04-01 15:02:13 +02:00
|
|
|
"sw_version": self.device.current_firm_version,
|
|
|
|
}
|
|
|
|
|
2023-04-28 09:57:43 +02:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
self.async_on_remove(
|
|
|
|
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
2022-04-01 15:02:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VeSyncDevice(VeSyncBaseEntity, ToggleEntity):
|
|
|
|
"""Base class for VeSync Device Representations."""
|
|
|
|
|
2023-10-27 08:33:05 +01:00
|
|
|
def __init__(self, device, coordinator) -> None:
|
2023-04-28 09:57:43 +02:00
|
|
|
"""Initialize the VeSync device."""
|
|
|
|
super().__init__(device, coordinator)
|
|
|
|
|
2022-04-01 15:02:13 +02:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if device is on."""
|
|
|
|
return self.device.device_status == "on"
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the device off."""
|
|
|
|
self.device.turn_off()
|