Move vesync base entity to separate module (#126187)

This commit is contained in:
epenet 2024-09-18 09:49:10 +02:00 committed by GitHub
parent 4aaba171ca
commit e7bb9a440a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 75 additions and 71 deletions

View File

@ -1,14 +1,8 @@
"""Common utilities for VeSync Component.""" """Common utilities for VeSync Component."""
import logging import logging
from typing import Any
from pyvesync.vesyncbasedevice import VeSyncBaseDevice from .const import VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity, ToggleEntity
from .const import DOMAIN, VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -48,62 +42,3 @@ async def async_process_devices(hass, manager):
_LOGGER.info("%d VeSync switches found", len(manager.switches)) _LOGGER.info("%d VeSync switches found", len(manager.switches))
return devices return devices
class VeSyncBaseEntity(Entity):
"""Base class for VeSync Entity Representations."""
_attr_has_entity_name = True
def __init__(self, device: VeSyncBaseDevice) -> None:
"""Initialize the VeSync device."""
self.device = device
self._attr_unique_id = self.base_unique_id
@property
def base_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.
if isinstance(self.device.sub_device_no, int):
return f"{self.device.cid}{self.device.sub_device_no!s}"
return self.device.cid
@property
def available(self) -> bool:
"""Return True if device is available."""
return self.device.connection_status == "online"
@property
def device_info(self) -> DeviceInfo:
"""Return device information."""
return DeviceInfo(
identifiers={(DOMAIN, self.base_unique_id)},
name=self.device.device_name,
model=self.device.device_type,
manufacturer="VeSync",
sw_version=self.device.current_firm_version,
)
def update(self) -> None:
"""Update vesync device."""
self.device.update()
class VeSyncDevice(VeSyncBaseEntity, ToggleEntity):
"""Base class for VeSync Device Representations."""
@property
def details(self):
"""Provide access to the device details dictionary."""
return self.device.details
@property
def is_on(self) -> bool:
"""Return True if device is on."""
return self.device.device_status == "on"
def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
self.device.turn_off()

View File

@ -12,8 +12,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.device_registry import DeviceEntry
from .common import VeSyncBaseDevice
from .const import DOMAIN, VS_MANAGER from .const import DOMAIN, VS_MANAGER
from .entity import VeSyncBaseDevice
KEYS_TO_REDACT = {"manager", "uuid", "mac_id"} KEYS_TO_REDACT = {"manager", "uuid", "mac_id"}

View File

@ -0,0 +1,69 @@
"""Common entity for VeSync Component."""
from typing import Any
from pyvesync.vesyncbasedevice import VeSyncBaseDevice
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity, ToggleEntity
from .const import DOMAIN
class VeSyncBaseEntity(Entity):
"""Base class for VeSync Entity Representations."""
_attr_has_entity_name = True
def __init__(self, device: VeSyncBaseDevice) -> None:
"""Initialize the VeSync device."""
self.device = device
self._attr_unique_id = self.base_unique_id
@property
def base_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.
if isinstance(self.device.sub_device_no, int):
return f"{self.device.cid}{self.device.sub_device_no!s}"
return self.device.cid
@property
def available(self) -> bool:
"""Return True if device is available."""
return self.device.connection_status == "online"
@property
def device_info(self) -> DeviceInfo:
"""Return device information."""
return DeviceInfo(
identifiers={(DOMAIN, self.base_unique_id)},
name=self.device.device_name,
model=self.device.device_type,
manufacturer="VeSync",
sw_version=self.device.current_firm_version,
)
def update(self) -> None:
"""Update vesync device."""
self.device.update()
class VeSyncDevice(VeSyncBaseEntity, ToggleEntity):
"""Base class for VeSync Device Representations."""
@property
def details(self):
"""Provide access to the device details dictionary."""
return self.device.details
@property
def is_on(self) -> bool:
"""Return True if device is on."""
return self.device.device_status == "on"
def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
self.device.turn_off()

View File

@ -17,8 +17,8 @@ from homeassistant.util.percentage import (
) )
from homeassistant.util.scaling import int_states_in_range from homeassistant.util.scaling import int_states_in_range
from .common import VeSyncDevice
from .const import DEV_TYPE_TO_HA, DOMAIN, SKU_TO_BASE_DEVICE, VS_DISCOVERY, VS_FANS from .const import DEV_TYPE_TO_HA, DOMAIN, SKU_TO_BASE_DEVICE, VS_DISCOVERY, VS_FANS
from .entity import VeSyncDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -14,8 +14,8 @@ 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
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_LIGHTS from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_LIGHTS
from .entity import VeSyncDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -30,8 +30,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from .common import VeSyncBaseEntity
from .const import DEV_TYPE_TO_HA, DOMAIN, SKU_TO_BASE_DEVICE, VS_DISCOVERY, VS_SENSORS from .const import DEV_TYPE_TO_HA, DOMAIN, SKU_TO_BASE_DEVICE, VS_DISCOVERY, VS_SENSORS
from .entity import VeSyncBaseEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -9,8 +9,8 @@ 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
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES
from .entity import VeSyncDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)