2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera sensors."""
|
2021-08-11 20:41:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
import logging
|
|
|
|
|
2021-12-09 08:13:50 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2020-05-26 07:38:41 +00:00
|
|
|
)
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-09 08:13:50 +00:00
|
|
|
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, TEMP_FAHRENHEIT
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-21 11:06:08 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-12-21 11:06:08 +00:00
|
|
|
from .const import DEFAULT_BRAND, DOMAIN, TYPE_TEMPERATURE, TYPE_WIFI_STRENGTH
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2017-03-26 13:50:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-11 20:41:51 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_TEMPERATURE,
|
|
|
|
name="Temperature",
|
|
|
|
native_unit_of_measurement=TEMP_FAHRENHEIT,
|
2021-12-09 08:13:50 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-12-21 11:06:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-08-11 20:41:51 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_WIFI_STRENGTH,
|
|
|
|
name="Wifi Signal",
|
|
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
2021-12-09 08:13:50 +00:00
|
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
2021-12-21 11:06:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
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 18:18:00 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-05-13 13:50:29 +00:00
|
|
|
"""Initialize a Blink sensor."""
|
|
|
|
data = hass.data[DOMAIN][config.entry_id]
|
2021-08-11 20:41:51 +00:00
|
|
|
entities = [
|
|
|
|
BlinkSensor(data, camera, description)
|
|
|
|
for camera in data.cameras
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
]
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
async_add_entities(entities)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
class BlinkSensor(SensorEntity):
|
2017-03-07 22:26:53 +00:00
|
|
|
"""A Blink camera sensor."""
|
|
|
|
|
2021-08-11 20:41:51 +00:00
|
|
|
def __init__(self, data, camera, description: SensorEntityDescription):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Initialize sensors from Blink camera."""
|
2021-08-11 20:41:51 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{DOMAIN} {camera} {description.name}"
|
2017-03-07 22:26:53 +00:00
|
|
|
self.data = data
|
2018-12-03 20:45:12 +00:00
|
|
|
self._camera = data.cameras[camera]
|
2021-08-11 20:41:51 +00:00
|
|
|
self._attr_unique_id = f"{self._camera.serial}-{description.key}"
|
2021-07-18 21:21:12 +00:00
|
|
|
self._sensor_key = (
|
2021-08-11 20:41:51 +00:00
|
|
|
"temperature_calibrated"
|
|
|
|
if description.key == "temperature"
|
|
|
|
else description.key
|
2021-07-18 21:21:12 +00:00
|
|
|
)
|
2021-12-21 11:06:08 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self._camera.serial)},
|
|
|
|
name=camera,
|
|
|
|
manufacturer=DEFAULT_BRAND,
|
|
|
|
model=self._camera.camera_type,
|
|
|
|
)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
def update(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Retrieve sensor data from the camera."""
|
2018-10-03 02:17:14 +00:00
|
|
|
self.data.refresh()
|
|
|
|
try:
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_value = self._camera.attributes[self._sensor_key]
|
2018-10-03 02:17:14 +00:00
|
|
|
except KeyError:
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_value = None
|
2018-10-03 02:17:14 +00:00
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"%s not a valid camera attribute. Did the API change?", self._sensor_key
|
|
|
|
)
|