2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera sensors."""
|
2017-03-07 22:26:53 +00:00
|
|
|
import logging
|
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from . import BLINK_DATA, SENSORS
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2017-03-26 13:50:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-03-26 13:50:40 +00:00
|
|
|
"""Set up a Blink sensor."""
|
2017-03-07 22:26:53 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2018-10-03 02:17:14 +00:00
|
|
|
data = hass.data[BLINK_DATA]
|
|
|
|
devs = []
|
2018-12-03 20:45:12 +00:00
|
|
|
for camera in data.cameras:
|
2018-10-03 02:17:14 +00:00
|
|
|
for sensor_type in discovery_info[CONF_MONITORED_CONDITIONS]:
|
|
|
|
devs.append(BlinkSensor(data, camera, sensor_type))
|
2017-03-07 22:26:53 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devs, True)
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BlinkSensor(Entity):
|
|
|
|
"""A Blink camera sensor."""
|
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
def __init__(self, data, camera, sensor_type):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Initialize sensors from Blink camera."""
|
2018-10-03 02:17:14 +00:00
|
|
|
name, units, icon = SENSORS[sensor_type]
|
2019-09-03 15:09:59 +00:00
|
|
|
self._name = f"{BLINK_DATA} {camera} {name}"
|
2017-03-07 22:26:53 +00:00
|
|
|
self._camera_name = name
|
|
|
|
self._type = sensor_type
|
|
|
|
self.data = data
|
2018-12-03 20:45:12 +00:00
|
|
|
self._camera = data.cameras[camera]
|
2017-03-07 22:26:53 +00:00
|
|
|
self._state = None
|
2018-10-03 02:17:14 +00:00
|
|
|
self._unit_of_measurement = units
|
|
|
|
self._icon = icon
|
2019-09-03 15:09:59 +00:00
|
|
|
self._unique_id = f"{self._camera.serial}-{self._type}"
|
2019-01-11 01:24:35 +00:00
|
|
|
self._sensor_key = self._type
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._type == "temperature":
|
|
|
|
self._sensor_key = "temperature_calibrated"
|
2017-03-07 22:26:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the name of the camera."""
|
2017-03-07 22:26:53 +00:00
|
|
|
return self._name
|
|
|
|
|
2018-10-17 06:38:03 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id for the camera sensor."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2018-10-03 02:17:14 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon of the sensor."""
|
|
|
|
return self._icon
|
|
|
|
|
2017-03-07 22:26:53 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the camera's current state."""
|
2017-03-07 22:26:53 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the unit of measurement."""
|
2017-03-07 22:26:53 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
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:
|
2019-01-11 01:24:35 +00:00
|
|
|
self._state = self._camera.attributes[self._sensor_key]
|
2018-10-03 02:17:14 +00:00
|
|
|
except KeyError:
|
2017-03-07 22:26:53 +00:00
|
|
|
self._state = 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
|
|
|
|
)
|