Add base class to gardena bluetooth entities (#96775)

Add helper base class for gardena entities
This commit is contained in:
Joakim Plate 2023-07-17 21:12:41 +02:00 committed by GitHub
parent 36cb3f7278
commit d80b7d0145
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 32 deletions

View file

@ -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

View file

@ -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()

View file

@ -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):

View file

@ -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: