diff --git a/homeassistant/components/gardena_bluetooth/coordinator.py b/homeassistant/components/gardena_bluetooth/coordinator.py index 997c78d0f00..9f5dc3223b5 100644 --- a/homeassistant/components/gardena_bluetooth/coordinator.py +++ b/homeassistant/components/gardena_bluetooth/coordinator.py @@ -15,7 +15,7 @@ from gardena_bluetooth.parse import Characteristic, CharacteristicType from homeassistant.components import bluetooth from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers.entity import DeviceInfo +from homeassistant.helpers.entity import DeviceInfo, EntityDescription from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, @@ -119,3 +119,15 @@ class GardenaBluetoothEntity(CoordinatorEntity[Coordinator]): return super().available and bluetooth.async_address_present( self.hass, self.coordinator.address, True ) + + +class GardenaBluetoothDescriptorEntity(GardenaBluetoothEntity): + """Coordinator entity for entities with entity description.""" + + def __init__( + self, coordinator: Coordinator, description: EntityDescription + ) -> None: + """Initialize description entity.""" + super().__init__(coordinator, {description.key}) + self._attr_unique_id = f"{coordinator.address}-{description.key}" + self.entity_description = description diff --git a/homeassistant/components/gardena_bluetooth/number.py b/homeassistant/components/gardena_bluetooth/number.py index 5f7f870302b..ec7ae513a3e 100644 --- a/homeassistant/components/gardena_bluetooth/number.py +++ b/homeassistant/components/gardena_bluetooth/number.py @@ -21,7 +21,11 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN -from .coordinator import Coordinator, GardenaBluetoothEntity +from .coordinator import ( + Coordinator, + GardenaBluetoothDescriptorEntity, + GardenaBluetoothEntity, +) @dataclass @@ -95,24 +99,14 @@ async def async_setup_entry( async_add_entities(entities) -class GardenaBluetoothNumber(GardenaBluetoothEntity, NumberEntity): +class GardenaBluetoothNumber(GardenaBluetoothDescriptorEntity, NumberEntity): """Representation of a number.""" entity_description: GardenaBluetoothNumberEntityDescription - def __init__( - self, - coordinator: Coordinator, - description: GardenaBluetoothNumberEntityDescription, - ) -> None: - """Initialize the number entity.""" - super().__init__(coordinator, {description.key}) - self._attr_unique_id = f"{coordinator.address}-{description.key}" - self.entity_description = description - def _handle_coordinator_update(self) -> None: - if data := self.coordinator.data.get(self.entity_description.char.uuid): - self._attr_native_value = float(self.entity_description.char.decode(data)) + if data := self.coordinator.get_cached(self.entity_description.char): + self._attr_native_value = float(data) else: self._attr_native_value = None super()._handle_coordinator_update() diff --git a/homeassistant/components/gardena_bluetooth/sensor.py b/homeassistant/components/gardena_bluetooth/sensor.py index d7cf914b9df..eaa44d9d4fb 100644 --- a/homeassistant/components/gardena_bluetooth/sensor.py +++ b/homeassistant/components/gardena_bluetooth/sensor.py @@ -20,7 +20,11 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util from .const import DOMAIN -from .coordinator import Coordinator, GardenaBluetoothEntity +from .coordinator import ( + Coordinator, + GardenaBluetoothDescriptorEntity, + GardenaBluetoothEntity, +) @dataclass @@ -65,22 +69,11 @@ async def async_setup_entry( async_add_entities(entities) -class GardenaBluetoothSensor(GardenaBluetoothEntity, SensorEntity): +class GardenaBluetoothSensor(GardenaBluetoothDescriptorEntity, SensorEntity): """Representation of a sensor.""" entity_description: GardenaBluetoothSensorEntityDescription - def __init__( - self, - coordinator: Coordinator, - description: GardenaBluetoothSensorEntityDescription, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator, {description.key}) - self._attr_native_value = None - self._attr_unique_id = f"{coordinator.address}-{description.key}" - self.entity_description = description - def _handle_coordinator_update(self) -> None: value = self.coordinator.get_cached(self.entity_description.char) if isinstance(value, datetime): diff --git a/homeassistant/components/gardena_bluetooth/switch.py b/homeassistant/components/gardena_bluetooth/switch.py index e3fcc8978c7..adb23c74c1d 100644 --- a/homeassistant/components/gardena_bluetooth/switch.py +++ b/homeassistant/components/gardena_bluetooth/switch.py @@ -51,10 +51,7 @@ class GardenaBluetoothValveSwitch(GardenaBluetoothEntity, SwitchEntity): self._attr_is_on = None def _handle_coordinator_update(self) -> None: - if data := self.coordinator.data.get(Valve.state.uuid): - self._attr_is_on = Valve.state.decode(data) - else: - self._attr_is_on = None + self._attr_is_on = self.coordinator.get_cached(Valve.state) super()._handle_coordinator_update() async def async_turn_on(self, **kwargs: Any) -> None: