Files
homeassistant-core/homeassistant/components/blink/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.3 KiB
Python
Raw Normal View History

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