Simplify Idasen Desk entity properties (#133536)

This commit is contained in:
Abílio Costa 2024-12-18 22:47:24 +00:00 committed by GitHub
parent 9f3c549f8d
commit 0076bd8389
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 23 deletions

View File

@ -12,7 +12,7 @@ from homeassistant.components.cover import (
CoverEntity, CoverEntity,
CoverEntityFeature, CoverEntityFeature,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -46,7 +46,6 @@ class IdasenDeskCover(IdasenDeskEntity, CoverEntity):
def __init__(self, coordinator: IdasenDeskCoordinator) -> None: def __init__(self, coordinator: IdasenDeskCoordinator) -> None:
"""Initialize an Idasen Desk cover.""" """Initialize an Idasen Desk cover."""
super().__init__(coordinator.address, coordinator) super().__init__(coordinator.address, coordinator)
self._attr_current_cover_position = self._desk.height_percent
@property @property
def is_closed(self) -> bool: def is_closed(self) -> bool:
@ -83,8 +82,7 @@ class IdasenDeskCover(IdasenDeskEntity, CoverEntity):
"Failed to move to specified position: Bluetooth error" "Failed to move to specified position: Bluetooth error"
) from err ) from err
@callback @property
def _handle_coordinator_update(self, *args: Any) -> None: def current_cover_position(self) -> int | None:
"""Handle data update.""" """Return the current cover position."""
self._attr_current_cover_position = self._desk.height_percent return self._desk.height_percent
self.async_write_ha_state()

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -13,7 +12,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.const import UnitOfLength from homeassistant.const import UnitOfLength
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IdasenDeskConfigEntry, IdasenDeskCoordinator from . import IdasenDeskConfigEntry, IdasenDeskCoordinator
@ -68,17 +67,7 @@ class IdasenDeskSensor(IdasenDeskEntity, SensorEntity):
super().__init__(f"{description.key}-{coordinator.address}", coordinator) super().__init__(f"{description.key}-{coordinator.address}", coordinator)
self.entity_description = description self.entity_description = description
async def async_added_to_hass(self) -> None: @property
"""When entity is added to hass.""" def native_value(self) -> float | None:
await super().async_added_to_hass() """Return the value reported by the sensor."""
self._update_native_value() return self.entity_description.value_fn(self.coordinator)
@callback
def _handle_coordinator_update(self, *args: Any) -> None:
"""Handle data update."""
self._update_native_value()
super()._handle_coordinator_update()
def _update_native_value(self) -> None:
"""Update the native value attribute."""
self._attr_native_value = self.entity_description.value_fn(self.coordinator)