2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Blink system camera sensors."""
|
2017-03-07 22:26:53 +00:00
|
|
|
import logging
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
from homeassistant.const import TEMP_FAHRENHEIT
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
from .const import DOMAIN, TYPE_BATTERY, 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__)
|
|
|
|
|
2020-05-13 13:50:29 +00:00
|
|
|
SENSORS = {
|
|
|
|
TYPE_TEMPERATURE: ["Temperature", TEMP_FAHRENHEIT, "mdi:thermometer"],
|
|
|
|
TYPE_BATTERY: ["Battery", "", "mdi:battery-80"],
|
|
|
|
TYPE_WIFI_STRENGTH: ["Wifi Signal", "dBm", "mdi:wifi-strength-2"],
|
|
|
|
}
|
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):
|
|
|
|
"""Initialize a Blink sensor."""
|
|
|
|
data = hass.data[DOMAIN][config.entry_id]
|
|
|
|
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 SENSORS:
|
|
|
|
entities.append(BlinkSensor(data, camera, sensor_type))
|
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
|
|
|
|
|
|
|
|
|
|
|
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]
|
2020-05-13 13:50:29 +00:00
|
|
|
self._name = f"{DOMAIN} {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
|
|
|
|
)
|