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

View File

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