mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Add base class to gardena bluetooth entities (#96775)
Add helper base class for gardena entities
This commit is contained in:
parent
36cb3f7278
commit
d80b7d0145
@ -15,7 +15,7 @@ from gardena_bluetooth.parse import Characteristic, CharacteristicType
|
|||||||
from homeassistant.components import bluetooth
|
from homeassistant.components import bluetooth
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
@ -119,3 +119,15 @@ class GardenaBluetoothEntity(CoordinatorEntity[Coordinator]):
|
|||||||
return super().available and bluetooth.async_address_present(
|
return super().available and bluetooth.async_address_present(
|
||||||
self.hass, self.coordinator.address, True
|
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
|
||||||
|
@ -21,7 +21,11 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import Coordinator, GardenaBluetoothEntity
|
from .coordinator import (
|
||||||
|
Coordinator,
|
||||||
|
GardenaBluetoothDescriptorEntity,
|
||||||
|
GardenaBluetoothEntity,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -95,24 +99,14 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class GardenaBluetoothNumber(GardenaBluetoothEntity, NumberEntity):
|
class GardenaBluetoothNumber(GardenaBluetoothDescriptorEntity, NumberEntity):
|
||||||
"""Representation of a number."""
|
"""Representation of a number."""
|
||||||
|
|
||||||
entity_description: GardenaBluetoothNumberEntityDescription
|
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:
|
def _handle_coordinator_update(self) -> None:
|
||||||
if data := self.coordinator.data.get(self.entity_description.char.uuid):
|
if data := self.coordinator.get_cached(self.entity_description.char):
|
||||||
self._attr_native_value = float(self.entity_description.char.decode(data))
|
self._attr_native_value = float(data)
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
super()._handle_coordinator_update()
|
super()._handle_coordinator_update()
|
||||||
|
@ -20,7 +20,11 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import Coordinator, GardenaBluetoothEntity
|
from .coordinator import (
|
||||||
|
Coordinator,
|
||||||
|
GardenaBluetoothDescriptorEntity,
|
||||||
|
GardenaBluetoothEntity,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -65,22 +69,11 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class GardenaBluetoothSensor(GardenaBluetoothEntity, SensorEntity):
|
class GardenaBluetoothSensor(GardenaBluetoothDescriptorEntity, SensorEntity):
|
||||||
"""Representation of a sensor."""
|
"""Representation of a sensor."""
|
||||||
|
|
||||||
entity_description: GardenaBluetoothSensorEntityDescription
|
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:
|
def _handle_coordinator_update(self) -> None:
|
||||||
value = self.coordinator.get_cached(self.entity_description.char)
|
value = self.coordinator.get_cached(self.entity_description.char)
|
||||||
if isinstance(value, datetime):
|
if isinstance(value, datetime):
|
||||||
|
@ -51,10 +51,7 @@ class GardenaBluetoothValveSwitch(GardenaBluetoothEntity, SwitchEntity):
|
|||||||
self._attr_is_on = None
|
self._attr_is_on = None
|
||||||
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
if data := self.coordinator.data.get(Valve.state.uuid):
|
self._attr_is_on = self.coordinator.get_cached(Valve.state)
|
||||||
self._attr_is_on = Valve.state.decode(data)
|
|
||||||
else:
|
|
||||||
self._attr_is_on = None
|
|
||||||
super()._handle_coordinator_update()
|
super()._handle_coordinator_update()
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user