Improve type hints in vesync lights (#75998)

* Improve type hints in vesync lights

* Adjust import
This commit is contained in:
epenet 2022-08-03 09:56:13 +02:00 committed by GitHub
parent 98f0b24c42
commit 1ba18f8df6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 27 deletions

View file

@ -21,7 +21,7 @@ from .const import (
VS_SWITCHES, VS_SWITCHES,
) )
PLATFORMS = [Platform.SWITCH, Platform.FAN, Platform.LIGHT, Platform.SENSOR] PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View file

@ -1,7 +1,10 @@
"""Common utilities for VeSync Component.""" """Common utilities for VeSync Component."""
import logging import logging
from typing import Any
from homeassistant.helpers.entity import Entity, ToggleEntity from pyvesync.vesyncbasedevice import VeSyncBaseDevice
from homeassistant.helpers.entity import DeviceInfo, Entity, ToggleEntity
from .const import DOMAIN, VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES from .const import DOMAIN, VS_FANS, VS_LIGHTS, VS_SENSORS, VS_SWITCHES
@ -48,7 +51,7 @@ async def async_process_devices(hass, manager):
class VeSyncBaseEntity(Entity): class VeSyncBaseEntity(Entity):
"""Base class for VeSync Entity Representations.""" """Base class for VeSync Entity Representations."""
def __init__(self, device): def __init__(self, device: VeSyncBaseDevice) -> None:
"""Initialize the VeSync device.""" """Initialize the VeSync device."""
self.device = device self.device = device
self._attr_unique_id = self.base_unique_id self._attr_unique_id = self.base_unique_id
@ -65,7 +68,7 @@ class VeSyncBaseEntity(Entity):
return self.device.cid return self.device.cid
@property @property
def base_name(self): def base_name(self) -> str:
"""Return the name of the device.""" """Return the name of the device."""
# Same story here as `base_unique_id` above # Same story here as `base_unique_id` above
return self.device.device_name return self.device.device_name
@ -76,17 +79,17 @@ class VeSyncBaseEntity(Entity):
return self.device.connection_status == "online" return self.device.connection_status == "online"
@property @property
def device_info(self): def device_info(self) -> DeviceInfo:
"""Return device information.""" """Return device information."""
return { return DeviceInfo(
"identifiers": {(DOMAIN, self.base_unique_id)}, identifiers={(DOMAIN, self.base_unique_id)},
"name": self.base_name, name=self.base_name,
"model": self.device.device_type, model=self.device.device_type,
"default_manufacturer": "VeSync", default_manufacturer="VeSync",
"sw_version": self.device.current_firm_version, sw_version=self.device.current_firm_version,
} )
def update(self): def update(self) -> None:
"""Update vesync device.""" """Update vesync device."""
self.device.update() self.device.update()
@ -100,10 +103,10 @@ class VeSyncDevice(VeSyncBaseEntity, ToggleEntity):
return self.device.details return self.device.details
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return True if device is on.""" """Return True if device is on."""
return self.device.device_status == "on" return self.device.device_status == "on"
def turn_off(self, **kwargs): def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
self.device.turn_off() self.device.turn_off()

View file

@ -67,7 +67,7 @@ class VeSyncBaseLight(VeSyncDevice, LightEntity):
"""Base class for VeSync Light Devices Representations.""" """Base class for VeSync Light Devices Representations."""
@property @property
def brightness(self): def brightness(self) -> int:
"""Get light brightness.""" """Get light brightness."""
# get value from pyvesync library api, # get value from pyvesync library api,
result = self.device.brightness result = self.device.brightness
@ -141,10 +141,12 @@ class VeSyncTunableWhiteLightHA(VeSyncBaseLight, LightEntity):
"""Representation of a VeSync Tunable White Light device.""" """Representation of a VeSync Tunable White Light device."""
_attr_color_mode = ColorMode.COLOR_TEMP _attr_color_mode = ColorMode.COLOR_TEMP
_attr_max_mireds = 370 # 1,000,000 divided by 2700 Kelvin = 370 Mireds
_attr_min_mireds = 154 # 1,000,000 divided by 6500 Kelvin = 154 Mireds
_attr_supported_color_modes = {ColorMode.COLOR_TEMP} _attr_supported_color_modes = {ColorMode.COLOR_TEMP}
@property @property
def color_temp(self): def color_temp(self) -> int:
"""Get device white temperature.""" """Get device white temperature."""
# get value from pyvesync library api, # get value from pyvesync library api,
result = self.device.color_temp_pct result = self.device.color_temp_pct
@ -169,13 +171,3 @@ class VeSyncTunableWhiteLightHA(VeSyncBaseLight, LightEntity):
) )
# ensure value between minimum and maximum Mireds # ensure value between minimum and maximum Mireds
return max(self.min_mireds, min(color_temp_value, self.max_mireds)) return max(self.min_mireds, min(color_temp_value, self.max_mireds))
@property
def min_mireds(self):
"""Set device coldest white temperature."""
return 154 # 154 Mireds ( 1,000,000 divided by 6500 Kelvin = 154 Mireds)
@property
def max_mireds(self):
"""Set device warmest white temperature."""
return 370 # 370 Mireds ( 1,000,000 divided by 2700 Kelvin = 370 Mireds)