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 <allen.porter@gmail.com>

* Wrap

---------

Co-authored-by: Allen Porter <allen.porter@gmail.com>
This commit is contained in:
cdnninja 2025-01-05 10:18:52 -07:00 committed by GitHub
parent 66b4b24612
commit d662a4465c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 35 deletions

View File

@ -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] = []

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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