Add coordinator to Blink (#102536)
This commit is contained in:
parent
5b39a08feb
commit
40ccae3d07
7 changed files with 173 additions and 113 deletions
|
@ -10,9 +10,10 @@ from homeassistant.components.binary_sensor import (
|
|||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
DEFAULT_BRAND,
|
||||
|
@ -21,6 +22,7 @@ from .const import (
|
|||
TYPE_CAMERA_ARMED,
|
||||
TYPE_MOTION_DETECTED,
|
||||
)
|
||||
from .coordinator import BlinkUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -45,28 +47,31 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the blink binary sensors."""
|
||||
data = hass.data[DOMAIN][config.entry_id]
|
||||
coordinator: BlinkUpdateCoordinator = hass.data[DOMAIN][config.entry_id]
|
||||
|
||||
entities = [
|
||||
BlinkBinarySensor(data, camera, description)
|
||||
for camera in data.cameras
|
||||
BlinkBinarySensor(coordinator, camera, description)
|
||||
for camera in coordinator.api.cameras
|
||||
for description in BINARY_SENSORS_TYPES
|
||||
]
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class BlinkBinarySensor(BinarySensorEntity):
|
||||
class BlinkBinarySensor(CoordinatorEntity[BlinkUpdateCoordinator], BinarySensorEntity):
|
||||
"""Representation of a Blink binary sensor."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self, data, camera, description: BinarySensorEntityDescription
|
||||
self,
|
||||
coordinator: BlinkUpdateCoordinator,
|
||||
camera,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self.data = data
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._camera = data.cameras[camera]
|
||||
self._camera = coordinator.api.cameras[camera]
|
||||
self._attr_unique_id = f"{self._camera.serial}-{description.key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self._camera.serial)},
|
||||
|
@ -74,10 +79,17 @@ class BlinkBinarySensor(BinarySensorEntity):
|
|||
manufacturer=DEFAULT_BRAND,
|
||||
model=self._camera.camera_type,
|
||||
)
|
||||
self._update_attrs()
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Update sensor state."""
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle update from data coordinator."""
|
||||
self._update_attrs()
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
@callback
|
||||
def _update_attrs(self) -> None:
|
||||
"""Update attributes for binary sensor."""
|
||||
is_on = self._camera.attributes[self.entity_description.key]
|
||||
_LOGGER.debug(
|
||||
"'%s' %s = %s",
|
||||
|
@ -87,4 +99,4 @@ class BlinkBinarySensor(BinarySensorEntity):
|
|||
)
|
||||
if self.entity_description.key == TYPE_BATTERY:
|
||||
is_on = is_on != "ok"
|
||||
return is_on
|
||||
self._attr_is_on = is_on
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue