2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera control."""
|
2020-05-26 07:38:41 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_BATTERY,
|
|
|
|
DEVICE_CLASS_MOTION,
|
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-26 07:38:41 +00:00
|
|
|
from .const import DOMAIN, TYPE_BATTERY, TYPE_CAMERA_ARMED, TYPE_MOTION_DETECTED
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
BINARY_SENSORS = {
|
2020-05-26 07:38:41 +00:00
|
|
|
TYPE_BATTERY: ["Battery", DEVICE_CLASS_BATTERY],
|
|
|
|
TYPE_CAMERA_ARMED: ["Camera Armed", None],
|
|
|
|
TYPE_MOTION_DETECTED: ["Motion Detected", DEVICE_CLASS_MOTION],
|
2020-05-13 13:50:29 +00:00
|
|
|
}
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, config, async_add_entities):
|
2017-03-26 13:50:40 +00:00
|
|
|
"""Set up the blink binary sensors."""
|
2020-05-13 13:50:29 +00:00
|
|
|
data = hass.data[DOMAIN][config.entry_id]
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
entities = []
|
2018-12-03 20:45:12 +00:00
|
|
|
for camera in data.cameras:
|
2020-05-13 13:50:29 +00:00
|
|
|
for sensor_type in BINARY_SENSORS:
|
|
|
|
entities.append(BlinkBinarySensor(data, camera, sensor_type))
|
|
|
|
async_add_entities(entities)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class BlinkBinarySensor(BinarySensorEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a Blink binary sensor."""
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
def __init__(self, data, camera, sensor_type):
|
2017-03-07 22:26:53 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.data = data
|
2018-10-03 02:17:14 +00:00
|
|
|
self._type = sensor_type
|
2020-05-26 07:38:41 +00:00
|
|
|
name, device_class = BINARY_SENSORS[sensor_type]
|
2020-05-13 13:50:29 +00:00
|
|
|
self._name = f"{DOMAIN} {camera} {name}"
|
2020-05-26 07:38:41 +00:00
|
|
|
self._device_class = device_class
|
2018-12-03 20:45:12 +00:00
|
|
|
self._camera = data.cameras[camera]
|
2018-10-03 02:17:14 +00:00
|
|
|
self._state = None
|
2019-09-03 15:09:59 +00:00
|
|
|
self._unique_id = f"{self._camera.serial}-{self._type}"
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the blink sensor."""
|
|
|
|
return self._name
|
|
|
|
|
2020-12-05 10:32:49 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id of the sensor."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2020-05-26 07:38:41 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device."""
|
|
|
|
return self._device_class
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update sensor state."""
|
|
|
|
self.data.refresh()
|
2020-05-26 07:38:41 +00:00
|
|
|
state = self._camera.attributes[self._type]
|
|
|
|
if self._type == TYPE_BATTERY:
|
|
|
|
state = state != "ok"
|
|
|
|
self._state = state
|