Use shorthand attributes in Tradfri (#99890)

This commit is contained in:
Joost Lekkerkerker 2023-09-08 01:15:34 +02:00 committed by GitHub
parent c2b119bfaf
commit 56f05bee91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 32 deletions

View File

@ -55,7 +55,16 @@ class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]):
self._device_id = self._device.id
self._api = handle_error(api)
self._attr_unique_id = f"{self._gateway_id}-{self._device.id}"
info = self._device.device_info
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
manufacturer=info.manufacturer,
model=info.model_number,
name=self._device.name,
sw_version=info.firmware_version,
via_device=(DOMAIN, gateway_id),
)
self._attr_unique_id = f"{gateway_id}-{self._device_id}"
@abstractmethod
@callback
@ -71,19 +80,6 @@ class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]):
self._refresh()
super()._handle_coordinator_update()
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
info = self._device.device_info
return DeviceInfo(
identifiers={(DOMAIN, self._device.id)},
manufacturer=info.manufacturer,
model=info.model_number,
name=self._device.name,
sw_version=info.firmware_version,
via_device=(DOMAIN, self._gateway_id),
)
@property
def available(self) -> bool:
"""Return if entity is available."""

View File

@ -56,6 +56,14 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
_attr_name = None
_attr_supported_features = FanEntityFeature.PRESET_MODE | FanEntityFeature.SET_SPEED
_attr_preset_modes = [ATTR_AUTO]
# These are the steps:
# 0 = Off
# 1 = Preset: Auto mode
# 2 = Min
# ... with step size 1
# 50 = Max
_attr_speed_count = ATTR_MAX_FAN_STEPS
def __init__(
self,
@ -77,19 +85,6 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
"""Refresh the device."""
self._device_data = self.coordinator.data.air_purifier_control.air_purifiers[0]
@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports.
These are the steps:
0 = Off
1 = Preset: Auto mode
2 = Min
... with step size 1
50 = Max
"""
return ATTR_MAX_FAN_STEPS
@property
def is_on(self) -> bool:
"""Return true if switch is on."""
@ -97,11 +92,6 @@ class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
return False
return cast(bool, self._device_data.state)
@property
def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes."""
return [ATTR_AUTO]
@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""