mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use more shorthand attributes in tplink (#108284)
* Use more shorthand attributes in tplink * naming * unused
This commit is contained in:
parent
4d5a511001
commit
e94493f83d
@ -50,8 +50,3 @@ class CoordinatedTPLinkEntity(CoordinatorEntity[TPLinkDataUpdateCoordinator]):
|
|||||||
sw_version=device.hw_info["sw_ver"],
|
sw_version=device.hw_info["sw_ver"],
|
||||||
hw_version=device.hw_info["hw_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)
|
|
||||||
|
@ -194,6 +194,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
|
|||||||
if not modes:
|
if not modes:
|
||||||
modes.add(ColorMode.ONOFF)
|
modes.add(ColorMode.ONOFF)
|
||||||
self._attr_supported_color_modes = modes
|
self._attr_supported_color_modes = modes
|
||||||
|
self._async_update_attrs()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_extract_brightness_transition(
|
def _async_extract_brightness_transition(
|
||||||
@ -271,24 +272,7 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
|
|||||||
transition = int(transition * 1_000)
|
transition = int(transition * 1_000)
|
||||||
await self.device.turn_off(transition=transition)
|
await self.device.turn_off(transition=transition)
|
||||||
|
|
||||||
@property
|
def _determine_color_mode(self) -> ColorMode:
|
||||||
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:
|
|
||||||
"""Return the active color mode."""
|
"""Return the active color mode."""
|
||||||
if self.device.is_color:
|
if self.device.is_color:
|
||||||
if self.device.is_variable_color_temp and self.device.color_temp:
|
if self.device.is_variable_color_temp and self.device.color_temp:
|
||||||
@ -299,6 +283,27 @@ class TPLinkSmartBulb(CoordinatedTPLinkEntity, LightEntity):
|
|||||||
|
|
||||||
return ColorMode.BRIGHTNESS
|
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):
|
class TPLinkSmartLightStrip(TPLinkSmartBulb):
|
||||||
"""Representation of a TPLink Smart Light Strip."""
|
"""Representation of a TPLink Smart Light Strip."""
|
||||||
@ -306,19 +311,19 @@ class TPLinkSmartLightStrip(TPLinkSmartBulb):
|
|||||||
device: SmartLightStrip
|
device: SmartLightStrip
|
||||||
_attr_supported_features = LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
|
_attr_supported_features = LightEntityFeature.TRANSITION | LightEntityFeature.EFFECT
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def effect_list(self) -> list[str] | None:
|
def _async_update_attrs(self) -> None:
|
||||||
"""Return the list of available effects."""
|
"""Update the entity's attributes."""
|
||||||
if effect_list := self.device.effect_list:
|
super()._async_update_attrs()
|
||||||
return cast(list[str], effect_list)
|
device = self.device
|
||||||
return None
|
if (effect := device.effect) and effect["enable"]:
|
||||||
|
self._attr_effect = effect["name"]
|
||||||
@property
|
else:
|
||||||
def effect(self) -> str | None:
|
self._attr_effect = None
|
||||||
"""Return the current effect."""
|
if effect_list := device.effect_list:
|
||||||
if (effect := self.device.effect) and effect["enable"]:
|
self._attr_effect_list = effect_list
|
||||||
return cast(str, effect["name"])
|
else:
|
||||||
return None
|
self._attr_effect_list = None
|
||||||
|
|
||||||
@async_refresh_after
|
@async_refresh_after
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
@ -9,7 +9,7 @@ from kasa import SmartDevice, SmartPlug
|
|||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import legacy_device_id
|
from . import legacy_device_id
|
||||||
@ -61,13 +61,8 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the LED switch."""
|
"""Initialize the LED switch."""
|
||||||
super().__init__(device, coordinator)
|
super().__init__(device, coordinator)
|
||||||
|
|
||||||
self._attr_unique_id = f"{self.device.mac}_led"
|
self._attr_unique_id = f"{self.device.mac}_led"
|
||||||
|
self._async_update_attrs()
|
||||||
@property
|
|
||||||
def icon(self) -> str:
|
|
||||||
"""Return the icon for the LED."""
|
|
||||||
return "mdi:led-on" if self.is_on else "mdi:led-off"
|
|
||||||
|
|
||||||
@async_refresh_after
|
@async_refresh_after
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
@ -79,10 +74,18 @@ class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
|||||||
"""Turn the LED switch off."""
|
"""Turn the LED switch off."""
|
||||||
await self.device.set_led(False)
|
await self.device.set_led(False)
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def is_on(self) -> bool:
|
def _async_update_attrs(self) -> None:
|
||||||
"""Return true if LED switch is on."""
|
"""Update the entity's attributes."""
|
||||||
return bool(self.device.led)
|
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):
|
class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
||||||
@ -99,6 +102,7 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
|||||||
super().__init__(device, coordinator)
|
super().__init__(device, coordinator)
|
||||||
# For backwards compat with pyHS100
|
# For backwards compat with pyHS100
|
||||||
self._attr_unique_id = legacy_device_id(device)
|
self._attr_unique_id = legacy_device_id(device)
|
||||||
|
self._async_update_attrs()
|
||||||
|
|
||||||
@async_refresh_after
|
@async_refresh_after
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
@ -110,6 +114,17 @@ class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
|||||||
"""Turn the switch off."""
|
"""Turn the switch off."""
|
||||||
await self.device.turn_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):
|
class SmartPlugSwitchChild(SmartPlugSwitch):
|
||||||
"""Representation of an individual plug of a TPLink Smart Plug strip."""
|
"""Representation of an individual plug of a TPLink Smart Plug strip."""
|
||||||
@ -121,8 +136,8 @@ class SmartPlugSwitchChild(SmartPlugSwitch):
|
|||||||
plug: SmartDevice,
|
plug: SmartDevice,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the child switch."""
|
"""Initialize the child switch."""
|
||||||
super().__init__(device, coordinator)
|
|
||||||
self._plug = plug
|
self._plug = plug
|
||||||
|
super().__init__(device, coordinator)
|
||||||
self._attr_unique_id = legacy_device_id(plug)
|
self._attr_unique_id = legacy_device_id(plug)
|
||||||
self._attr_name = plug.alias
|
self._attr_name = plug.alias
|
||||||
|
|
||||||
@ -136,7 +151,7 @@ class SmartPlugSwitchChild(SmartPlugSwitch):
|
|||||||
"""Turn the child switch off."""
|
"""Turn the child switch off."""
|
||||||
await self._plug.turn_off()
|
await self._plug.turn_off()
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def is_on(self) -> bool:
|
def _async_update_attrs(self) -> None:
|
||||||
"""Return true if child switch is on."""
|
"""Update the entity's attributes."""
|
||||||
return bool(self._plug.is_on)
|
self._attr_is_on = self._plug.is_on
|
||||||
|
Loading…
x
Reference in New Issue
Block a user