2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera control."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-08-11 20:41:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-06-01 18:06:53 +00:00
|
|
|
import logging
|
|
|
|
|
2020-05-26 07:38:41 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-09 08:13:50 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-05-26 07:38:41 +00:00
|
|
|
BinarySensorEntity,
|
2021-08-11 20:41:51 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-05-26 07:38:41 +00:00
|
|
|
)
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2023-10-23 13:34:28 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-10-23 13:34:28 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2021-12-21 11:06:08 +00:00
|
|
|
from .const import (
|
|
|
|
DEFAULT_BRAND,
|
|
|
|
DOMAIN,
|
|
|
|
TYPE_BATTERY,
|
|
|
|
TYPE_CAMERA_ARMED,
|
|
|
|
TYPE_MOTION_DETECTED,
|
|
|
|
)
|
2023-10-23 13:34:28 +00:00
|
|
|
from .coordinator import BlinkUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2023-06-01 18:06:53 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-11 20:41:51 +00:00
|
|
|
BINARY_SENSORS_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key=TYPE_BATTERY,
|
2021-12-09 08:13:50 +00:00
|
|
|
device_class=BinarySensorDeviceClass.BATTERY,
|
2021-12-21 11:06:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-11 20:41:51 +00:00
|
|
|
),
|
2023-12-18 13:04:19 +00:00
|
|
|
# Camera Armed sensor is deprecated covered by switch and will be removed in 2023.6.
|
2021-08-11 20:41:51 +00:00
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key=TYPE_CAMERA_ARMED,
|
2023-06-26 16:29:33 +00:00
|
|
|
translation_key="camera_armed",
|
2023-11-24 16:02:19 +00:00
|
|
|
entity_registry_enabled_default=False,
|
2021-08-11 20:41:51 +00:00
|
|
|
),
|
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key=TYPE_MOTION_DETECTED,
|
2021-12-09 08:13:50 +00:00
|
|
|
device_class=BinarySensorDeviceClass.MOTION,
|
2021-08-11 20:41:51 +00:00
|
|
|
),
|
|
|
|
)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
|
2022-01-03 12:10:41 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2017-03-26 13:50:40 +00:00
|
|
|
"""Set up the blink binary sensors."""
|
2023-11-04 15:21:10 +00:00
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
coordinator: BlinkUpdateCoordinator = hass.data[DOMAIN][config.entry_id]
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2021-08-11 20:41:51 +00:00
|
|
|
entities = [
|
2023-10-23 13:34:28 +00:00
|
|
|
BlinkBinarySensor(coordinator, camera, description)
|
|
|
|
for camera in coordinator.api.cameras
|
2021-08-11 20:41:51 +00:00
|
|
|
for description in BINARY_SENSORS_TYPES
|
|
|
|
]
|
2023-10-16 15:41:56 +00:00
|
|
|
async_add_entities(entities)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
class BlinkBinarySensor(CoordinatorEntity[BlinkUpdateCoordinator], BinarySensorEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a Blink binary sensor."""
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2023-07-11 18:12:16 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2023-02-06 10:37:25 +00:00
|
|
|
def __init__(
|
2023-10-23 13:34:28 +00:00
|
|
|
self,
|
|
|
|
coordinator: BlinkUpdateCoordinator,
|
|
|
|
camera,
|
|
|
|
description: BinarySensorEntityDescription,
|
2023-02-06 10:37:25 +00:00
|
|
|
) -> None:
|
2017-03-07 22:26:53 +00:00
|
|
|
"""Initialize the sensor."""
|
2023-10-23 13:34:28 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-11 20:41:51 +00:00
|
|
|
self.entity_description = description
|
2023-10-23 13:34:28 +00:00
|
|
|
self._camera = coordinator.api.cameras[camera]
|
2023-10-24 09:38:54 +00:00
|
|
|
serial = self._camera.serial
|
|
|
|
self._attr_unique_id = f"{serial}-{description.key}"
|
2021-12-21 11:06:08 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
2023-10-24 09:38:54 +00:00
|
|
|
identifiers={(DOMAIN, serial)},
|
|
|
|
serial_number=serial,
|
2021-12-21 11:06:08 +00:00
|
|
|
name=camera,
|
|
|
|
manufacturer=DEFAULT_BRAND,
|
|
|
|
model=self._camera.camera_type,
|
|
|
|
)
|
2023-10-23 13:34:28 +00:00
|
|
|
self._update_attrs()
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2023-10-23 13:34:28 +00:00
|
|
|
@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."""
|
2023-10-16 11:41:45 +00:00
|
|
|
is_on = self._camera.attributes[self.entity_description.key]
|
2023-06-01 18:06:53 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"'%s' %s = %s",
|
|
|
|
self._camera.attributes["name"],
|
|
|
|
self.entity_description.key,
|
2023-10-16 11:41:45 +00:00
|
|
|
is_on,
|
2023-06-01 18:06:53 +00:00
|
|
|
)
|
2021-08-11 20:41:51 +00:00
|
|
|
if self.entity_description.key == TYPE_BATTERY:
|
2023-10-16 11:41:45 +00:00
|
|
|
is_on = is_on != "ok"
|
2023-10-23 13:34:28 +00:00
|
|
|
self._attr_is_on = is_on
|