From d662a4465c19ed795ba959edc54b271c936156d8 Mon Sep 17 00:00:00 2001 From: cdnninja Date: Sun, 5 Jan 2025 10:18:52 -0700 Subject: [PATCH] Remove unneeded vesync device base class (#134499) * Remove unneeded entity to make code cleaner * Update light.py * Update fan.py * Typing. * Update homeassistant/components/vesync/common.py Co-authored-by: Allen Porter * Wrap --------- Co-authored-by: Allen Porter --- homeassistant/components/vesync/common.py | 11 +++++++++-- homeassistant/components/vesync/entity.py | 21 --------------------- homeassistant/components/vesync/fan.py | 17 ++++++++++++++--- homeassistant/components/vesync/light.py | 19 ++++++++++++++----- homeassistant/components/vesync/switch.py | 21 +++++++++++++++++---- 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/vesync/common.py b/homeassistant/components/vesync/common.py index 5f7b2a3a29e..5412b4f970c 100644 --- a/homeassistant/components/vesync/common.py +++ b/homeassistant/components/vesync/common.py @@ -2,14 +2,21 @@ import logging +from pyvesync import VeSync +from pyvesync.vesyncbasedevice import VeSyncBaseDevice + +from homeassistant.core import HomeAssistant + from .const import VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES _LOGGER = logging.getLogger(__name__) -async def async_process_devices(hass, manager): +async def async_process_devices( + hass: HomeAssistant, manager: VeSync +) -> dict[str, list[VeSyncBaseDevice]]: """Assign devices to proper component.""" - devices = {} + devices: dict[str, list[VeSyncBaseDevice]] = {} devices[VS_SWITCHES] = [] devices[VS_FANS] = [] devices[VS_LIGHTS] = [] diff --git a/homeassistant/components/vesync/entity.py b/homeassistant/components/vesync/entity.py index 68c21a871ab..3aa7b008cc5 100644 --- a/homeassistant/components/vesync/entity.py +++ b/homeassistant/components/vesync/entity.py @@ -1,11 +1,8 @@ """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 ToggleEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN @@ -50,21 +47,3 @@ class VeSyncBaseEntity(CoordinatorEntity[VeSyncDataCoordinator]): manufacturer="VeSync", sw_version=self.device.current_firm_version, ) - - -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 95404a921e8..c6d61feebef 100644 --- a/homeassistant/components/vesync/fan.py +++ b/homeassistant/components/vesync/fan.py @@ -28,7 +28,7 @@ from .const import ( VS_FANS, ) from .coordinator import VeSyncDataCoordinator -from .entity import VeSyncDevice +from .entity import VeSyncBaseEntity _LOGGER = logging.getLogger(__name__) @@ -100,7 +100,7 @@ def _setup_entities( async_add_entities(entities, update_before_add=True) -class VeSyncFanHA(VeSyncDevice, FanEntity): +class VeSyncFanHA(VeSyncBaseEntity, FanEntity): """Representation of a VeSync fan.""" _attr_supported_features = ( @@ -112,11 +112,18 @@ class VeSyncFanHA(VeSyncDevice, FanEntity): _attr_name = None _attr_translation_key = "vesync" - def __init__(self, fan, coordinator: VeSyncDataCoordinator) -> None: + def __init__( + self, fan: VeSyncBaseDevice, coordinator: VeSyncDataCoordinator + ) -> None: """Initialize the VeSync fan device.""" super().__init__(fan, coordinator) self.smartfan = fan + @property + def is_on(self) -> bool: + """Return True if device is on.""" + return self.device.device_status == "on" + @property def percentage(self) -> int | None: """Return the current speed.""" @@ -229,3 +236,7 @@ class VeSyncFanHA(VeSyncDevice, FanEntity): if percentage is None: percentage = 50 self.set_percentage(percentage) + + def turn_off(self, **kwargs: Any) -> None: + """Turn the device off.""" + self.device.turn_off() diff --git a/homeassistant/components/vesync/light.py b/homeassistant/components/vesync/light.py index 4deb250bd43..84324e0af6e 100644 --- a/homeassistant/components/vesync/light.py +++ b/homeassistant/components/vesync/light.py @@ -19,7 +19,7 @@ from homeassistant.util import color as color_util from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DISCOVERY, VS_LIGHTS from .coordinator import VeSyncDataCoordinator -from .entity import VeSyncDevice +from .entity import VeSyncBaseEntity _LOGGER = logging.getLogger(__name__) MAX_MIREDS = 370 # 1,000,000 divided by 2700 Kelvin = 370 Mireds @@ -54,7 +54,7 @@ def _setup_entities( coordinator: VeSyncDataCoordinator, ): """Check if device is online and add entity.""" - entities: list[VeSyncBaseLight] = [] + entities: list[VeSyncBaseLightHA] = [] for dev in devices: if DEV_TYPE_TO_HA.get(dev.device_type) in ("walldimmer", "bulb-dimmable"): entities.append(VeSyncDimmableLightHA(dev, coordinator)) @@ -69,11 +69,16 @@ def _setup_entities( async_add_entities(entities, update_before_add=True) -class VeSyncBaseLight(VeSyncDevice, LightEntity): +class VeSyncBaseLightHA(VeSyncBaseEntity, LightEntity): """Base class for VeSync Light Devices Representations.""" _attr_name = None + @property + def is_on(self) -> bool: + """Return True if device is on.""" + return self.device.device_status == "on" + @property def brightness(self) -> int: """Get light brightness.""" @@ -139,15 +144,19 @@ class VeSyncBaseLight(VeSyncDevice, LightEntity): # send turn_on command to pyvesync api self.device.turn_on() + def turn_off(self, **kwargs: Any) -> None: + """Turn the device off.""" + self.device.turn_off() -class VeSyncDimmableLightHA(VeSyncBaseLight, LightEntity): + +class VeSyncDimmableLightHA(VeSyncBaseLightHA, LightEntity): """Representation of a VeSync dimmable light device.""" _attr_color_mode = ColorMode.BRIGHTNESS _attr_supported_color_modes = {ColorMode.BRIGHTNESS} -class VeSyncTunableWhiteLightHA(VeSyncBaseLight, LightEntity): +class VeSyncTunableWhiteLightHA(VeSyncBaseLightHA, LightEntity): """Representation of a VeSync Tunable White Light device.""" _attr_color_mode = ColorMode.COLOR_TEMP diff --git a/homeassistant/components/vesync/switch.py b/homeassistant/components/vesync/switch.py index eac1d967eee..0b69ca3d44a 100644 --- a/homeassistant/components/vesync/switch.py +++ b/homeassistant/components/vesync/switch.py @@ -13,7 +13,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DEV_TYPE_TO_HA, DOMAIN, VS_COORDINATOR, VS_DISCOVERY, VS_SWITCHES from .coordinator import VeSyncDataCoordinator -from .entity import VeSyncDevice +from .entity import VeSyncBaseEntity _LOGGER = logging.getLogger(__name__) @@ -61,7 +61,7 @@ def _setup_entities( async_add_entities(entities, update_before_add=True) -class VeSyncBaseSwitch(VeSyncDevice, SwitchEntity): +class VeSyncBaseSwitch(VeSyncBaseEntity, SwitchEntity): """Base class for VeSync switch Device Representations.""" _attr_name = None @@ -70,11 +70,22 @@ class VeSyncBaseSwitch(VeSyncDevice, SwitchEntity): """Turn the device on.""" self.device.turn_on() + @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() + class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity): """Representation of a VeSync switch.""" - def __init__(self, plug, coordinator: VeSyncDataCoordinator) -> None: + def __init__( + self, plug: VeSyncBaseDevice, coordinator: VeSyncDataCoordinator + ) -> None: """Initialize the VeSync switch device.""" super().__init__(plug, coordinator) self.smartplug = plug @@ -83,7 +94,9 @@ class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity): class VeSyncLightSwitch(VeSyncBaseSwitch, SwitchEntity): """Handle representation of VeSync Light Switch.""" - def __init__(self, switch, coordinator: VeSyncDataCoordinator) -> None: + def __init__( + self, switch: VeSyncBaseDevice, coordinator: VeSyncDataCoordinator + ) -> None: """Initialize Light Switch device class.""" super().__init__(switch, coordinator) self.switch = switch