From e7bb9a440a190c4b4dc236237c7fe594da30ec2e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 18 Sep 2024 09:49:10 +0200 Subject: [PATCH] Move vesync base entity to separate module (#126187) --- homeassistant/components/vesync/common.py | 67 +----------------- .../components/vesync/diagnostics.py | 2 +- homeassistant/components/vesync/entity.py | 69 +++++++++++++++++++ homeassistant/components/vesync/fan.py | 2 +- homeassistant/components/vesync/light.py | 2 +- homeassistant/components/vesync/sensor.py | 2 +- homeassistant/components/vesync/switch.py | 2 +- 7 files changed, 75 insertions(+), 71 deletions(-) create mode 100644 homeassistant/components/vesync/entity.py diff --git a/homeassistant/components/vesync/common.py b/homeassistant/components/vesync/common.py index 33fc88f32d6..b57b49f9994 100644 --- a/homeassistant/components/vesync/common.py +++ b/homeassistant/components/vesync/common.py @@ -1,14 +1,8 @@ """Common utilities for VeSync Component.""" import logging -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, VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES +from .const import VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES _LOGGER = logging.getLogger(__name__) @@ -48,62 +42,3 @@ async def async_process_devices(hass, manager): _LOGGER.info("%d VeSync switches found", len(manager.switches)) 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() diff --git a/homeassistant/components/vesync/diagnostics.py b/homeassistant/components/vesync/diagnostics.py index 9af8a7fed67..e1c092b1e32 100644 --- a/homeassistant/components/vesync/diagnostics.py +++ b/homeassistant/components/vesync/diagnostics.py @@ -12,8 +12,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import DeviceEntry -from .common import VeSyncBaseDevice from .const import DOMAIN, VS_MANAGER +from .entity import VeSyncBaseDevice KEYS_TO_REDACT = {"manager", "uuid", "mac_id"} diff --git a/homeassistant/components/vesync/entity.py b/homeassistant/components/vesync/entity.py new file mode 100644 index 00000000000..fd636561e9e --- /dev/null +++ b/homeassistant/components/vesync/entity.py @@ -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() diff --git a/homeassistant/components/vesync/fan.py b/homeassistant/components/vesync/fan.py index 6ef9e41eb43..58a262e769f 100644 --- a/homeassistant/components/vesync/fan.py +++ b/homeassistant/components/vesync/fan.py @@ -17,8 +17,8 @@ from homeassistant.util.percentage import ( ) 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 .entity import VeSyncDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/vesync/light.py b/homeassistant/components/vesync/light.py index 9b15e635903..6e449f63394 100644 --- a/homeassistant/components/vesync/light.py +++ b/homeassistant/components/vesync/light.py @@ -14,8 +14,8 @@ 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 from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_LIGHTS +from .entity import VeSyncDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/vesync/sensor.py b/homeassistant/components/vesync/sensor.py index 8939295a2db..79061ec0c4c 100644 --- a/homeassistant/components/vesync/sensor.py +++ b/homeassistant/components/vesync/sensor.py @@ -30,8 +30,8 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 .entity import VeSyncBaseEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/vesync/switch.py b/homeassistant/components/vesync/switch.py index 1d0c3472d53..a162a648ad7 100644 --- a/homeassistant/components/vesync/switch.py +++ b/homeassistant/components/vesync/switch.py @@ -9,8 +9,8 @@ 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 from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES +from .entity import VeSyncDevice _LOGGER = logging.getLogger(__name__)