From e94493f83d468f7b69d19194a089ba0d95aca8ec Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" <nick@koston.org> Date: Sun, 21 Jan 2024 10:34:06 -1000 Subject: [PATCH] Use more shorthand attributes in tplink (#108284) * Use more shorthand attributes in tplink * naming * unused --- homeassistant/components/tplink/entity.py | 5 -- homeassistant/components/tplink/light.py | 67 ++++++++++++----------- homeassistant/components/tplink/switch.py | 47 ++++++++++------ 3 files changed, 67 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 84781597b93..577e1995d4a 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -50,8 +50,3 @@ class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]): sw_version=device.hw_info["sw_ver"], hw_version=device.hw_info["hw_ver"], ) - - @property - def is_on(self) -> bool: - """Return true if switch is on.""" - return bool(self.device.is_on) diff --git a/homeassistant/components/tplink/light.py b/homeassistant/components/tplink/light.py index c57fc9bfd85..70a078928d9 100644 --- a/homeassistant/components/tplink/light.py +++ b/homeassistant/components/tplink/light.py @@ -194,6 +194,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity): if not modes: modes.add(ColorMode.ONOFF) self._attr_supported_color_modes = modes + self._async_update_attrs() @callback def _async_extract_brightness_transition( @@ -271,24 +272,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity): transition = int(transition * 1_000) await self.device.turn_off(transition=transition) - @property - def color_temp_kelvin(self) -> int: - """Return the color temperature of this light.""" - return cast(int, self.device.color_temp) - - @property - def brightness(self) -> int | None: - """Return the brightness of this light between 0..255.""" - return round((cast(int, self.device.brightness) * 255.0) / 100.0) - - @property - def hs_color(self) -> tuple[int, int] | None: - """Return the color.""" - hue, saturation, _ = self.device.hsv - return hue, saturation - - @property - def color_mode(self) -> ColorMode: + def _determine_color_mode(self) -> ColorMode: """Return the active color mode.""" if self.device.is_color: if self.device.is_variable_color_temp and self.device.color_temp: @@ -299,6 +283,27 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity): return ColorMode.BRIGHTNESS + @callback + def _async_update_attrs(self) -> None: + """Update the entity's attributes.""" + device = self.device + self._attr_is_on = device.is_on + if device.is_dimmable: + self._attr_brightness = round((device.brightness * 255.0) / 100.0) + color_mode = self._determine_color_mode() + self._attr_color_mode = color_mode + if color_mode is ColorMode.COLOR_TEMP: + self._attr_color_temp_kelvin = device.color_temp + elif color_mode is ColorMode.HS: + hue, saturation, _ = device.hsv + self._attr_hs_color = hue, saturation + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._async_update_attrs() + super()._handle_coordinator_update() + class TPLinkSmartLightStrip(TPLinkSmartBulb): """Representation of a TPLink Smart Light Strip.""" @@ -306,19 +311,19 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb): device: SmartLightStrip _attr_supported_features = LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT - @property - def effect_list(self) -> list[str] | None: - """Return the list of available effects.""" - if effect_list := self.device.effect_list: - return cast(list[str], effect_list) - return None - - @property - def effect(self) -> str | None: - """Return the current effect.""" - if (effect := self.device.effect) and effect["enable"]: - return cast(str, effect["name"]) - return None + @callback + def _async_update_attrs(self) -> None: + """Update the entity's attributes.""" + super()._async_update_attrs() + device = self.device + if (effect := device.effect) and effect["enable"]: + self._attr_effect = effect["name"] + else: + self._attr_effect = None + if effect_list := device.effect_list: + self._attr_effect_list = effect_list + else: + self._attr_effect_list = None @async_refresh_after async def async_turn_on(self, **kwargs: Any) -> None: diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index 9a54a952666..3e81870d80f 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -9,7 +9,7 @@ from kasa import SmartDevice, SmartPlug from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import legacy_device_id @@ -61,13 +61,8 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity): ) -> None: """Initialize the LED switch.""" super().__init__(device, coordinator) - self._attr_unique_id = f"{self.device.mac}_led" - - @property - def icon(self) -> str: - """Return the icon for the LED.""" - return "mdi:led-on" if self.is_on else "mdi:led-off" + self._async_update_attrs() @async_refresh_after async def async_turn_on(self, **kwargs: Any) -> None: @@ -79,10 +74,18 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity): """Turn the LED switch off.""" await self.device.set_led(False) - @property - def is_on(self) -> bool: - """Return true if LED switch is on.""" - return bool(self.device.led) + @callback + def _async_update_attrs(self) -> None: + """Update the entity's attributes.""" + is_on = self.device.led + self._attr_is_on = is_on + self._attr_icon = "mdi:led-on" if is_on else "mdi:led-off" + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._async_update_attrs() + super()._handle_coordinator_update() class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity): @@ -99,6 +102,7 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity): super().__init__(device, coordinator) # For backwards compat with pyHS100 self._attr_unique_id = legacy_device_id(device) + self._async_update_attrs() @async_refresh_after async def async_turn_on(self, **kwargs: Any) -> None: @@ -110,6 +114,17 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity): """Turn the switch off.""" await self.device.turn_off() + @callback + def _async_update_attrs(self) -> None: + """Update the entity's attributes.""" + self._attr_is_on = self.device.is_on + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._async_update_attrs() + super()._handle_coordinator_update() + class SmartPlugSwitchChild(SmartPlugSwitch): """Representation of an individual plug of a TPLink Smart Plug strip.""" @@ -121,8 +136,8 @@ class SmartPlugSwitchChild(SmartPlugSwitch): plug: SmartDevice, ) -> None: """Initialize the child switch.""" - super().__init__(device, coordinator) self._plug = plug + super().__init__(device, coordinator) self._attr_unique_id = legacy_device_id(plug) self._attr_name = plug.alias @@ -136,7 +151,7 @@ class SmartPlugSwitchChild(SmartPlugSwitch): """Turn the child switch off.""" await self._plug.turn_off() - @property - def is_on(self) -> bool: - """Return true if child switch is on.""" - return bool(self._plug.is_on) + @callback + def _async_update_attrs(self) -> None: + """Update the entity's attributes.""" + self._attr_is_on = self._plug.is_on -- GitLab