2019-04-03 17:40:03 +02:00
|
|
|
"""This component provides HA sensor support for Ring Door Bell/Chimes."""
|
2017-03-05 03:03:00 -05:00
|
|
|
import logging
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2019-12-05 06:13:28 +01:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-03-05 03:03:00 -05:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-10-06 02:17:18 -04:00
|
|
|
from homeassistant.helpers.icon import icon_for_battery_level
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
from . import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
DATA_RING_CHIMES,
|
|
|
|
DATA_RING_DOORBELLS,
|
|
|
|
DATA_RING_STICKUP_CAMS,
|
2020-01-11 16:04:39 -08:00
|
|
|
DOMAIN,
|
2019-07-31 12:25:30 -07:00
|
|
|
SIGNAL_UPDATE_RING,
|
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-03-05 03:03:00 -05:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-03-31 11:53:56 -04:00
|
|
|
# Sensor types: Name, category, units, icon, kind
|
2017-03-05 03:03:00 -05:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"battery": ["Battery", ["doorbell", "stickup_cams"], "%", "battery-50", None],
|
|
|
|
"last_activity": [
|
|
|
|
"Last Activity",
|
|
|
|
["doorbell", "stickup_cams"],
|
|
|
|
None,
|
|
|
|
"history",
|
|
|
|
None,
|
|
|
|
],
|
|
|
|
"last_ding": ["Last Ding", ["doorbell"], None, "history", "ding"],
|
|
|
|
"last_motion": [
|
|
|
|
"Last Motion",
|
|
|
|
["doorbell", "stickup_cams"],
|
|
|
|
None,
|
|
|
|
"history",
|
|
|
|
"motion",
|
|
|
|
],
|
|
|
|
"volume": [
|
|
|
|
"Volume",
|
|
|
|
["chime", "doorbell", "stickup_cams"],
|
|
|
|
None,
|
|
|
|
"bell-ring",
|
|
|
|
None,
|
|
|
|
],
|
|
|
|
"wifi_signal_category": [
|
|
|
|
"WiFi Signal Category",
|
|
|
|
["chime", "doorbell", "stickup_cams"],
|
|
|
|
None,
|
|
|
|
"wifi",
|
|
|
|
None,
|
|
|
|
],
|
|
|
|
"wifi_signal_strength": [
|
|
|
|
"WiFi Signal Strength",
|
|
|
|
["chime", "doorbell", "stickup_cams"],
|
|
|
|
"dBm",
|
|
|
|
"wifi",
|
|
|
|
None,
|
|
|
|
],
|
2017-03-05 03:03:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2017-03-05 03:03:00 -05:00
|
|
|
"""Set up a sensor for a Ring device."""
|
2019-07-31 19:08:40 +01:00
|
|
|
ring_chimes = hass.data[DATA_RING_CHIMES]
|
|
|
|
ring_doorbells = hass.data[DATA_RING_DOORBELLS]
|
|
|
|
ring_stickup_cams = hass.data[DATA_RING_STICKUP_CAMS]
|
2017-03-05 03:03:00 -05:00
|
|
|
|
|
|
|
sensors = []
|
2019-07-31 19:08:40 +01:00
|
|
|
for device in ring_chimes:
|
2020-01-10 21:35:31 +01:00
|
|
|
for sensor_type in SENSOR_TYPES:
|
2019-07-31 12:25:30 -07:00
|
|
|
if "chime" in SENSOR_TYPES[sensor_type][1]:
|
2017-05-02 18:18:47 +02:00
|
|
|
sensors.append(RingSensor(hass, device, sensor_type))
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 19:08:40 +01:00
|
|
|
for device in ring_doorbells:
|
2020-01-10 21:35:31 +01:00
|
|
|
for sensor_type in SENSOR_TYPES:
|
2019-07-31 12:25:30 -07:00
|
|
|
if "doorbell" in SENSOR_TYPES[sensor_type][1]:
|
2017-05-02 18:18:47 +02:00
|
|
|
sensors.append(RingSensor(hass, device, sensor_type))
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 19:08:40 +01:00
|
|
|
for device in ring_stickup_cams:
|
2020-01-10 21:35:31 +01:00
|
|
|
for sensor_type in SENSOR_TYPES:
|
2019-07-31 12:25:30 -07:00
|
|
|
if "stickup_cams" in SENSOR_TYPES[sensor_type][1]:
|
2017-10-21 10:08:40 -04:00
|
|
|
sensors.append(RingSensor(hass, device, sensor_type))
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async_add_entities(sensors, True)
|
2017-03-05 03:03:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
class RingSensor(Entity):
|
|
|
|
"""A sensor implementation for Ring device."""
|
|
|
|
|
|
|
|
def __init__(self, hass, data, sensor_type):
|
|
|
|
"""Initialize a sensor for Ring device."""
|
2019-09-25 00:38:20 +02:00
|
|
|
super().__init__()
|
2017-03-05 03:03:00 -05:00
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._data = data
|
|
|
|
self._extra = None
|
2019-07-31 12:25:30 -07:00
|
|
|
self._icon = "mdi:{}".format(SENSOR_TYPES.get(self._sensor_type)[3])
|
2017-03-31 11:53:56 -04:00
|
|
|
self._kind = SENSOR_TYPES.get(self._sensor_type)[4]
|
2017-05-02 18:18:47 +02:00
|
|
|
self._name = "{0} {1}".format(
|
2019-07-31 12:25:30 -07:00
|
|
|
self._data.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
|
|
|
)
|
2019-01-24 08:20:20 +01:00
|
|
|
self._state = None
|
2017-03-05 03:03:00 -05:00
|
|
|
self._tz = str(hass.config.time_zone)
|
2019-09-03 21:14:39 +02:00
|
|
|
self._unique_id = f"{self._data.id}-{self._sensor_type}"
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = None
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 19:08:40 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_RING, self._update_callback
|
|
|
|
)
|
2020-01-11 16:04:39 -08:00
|
|
|
await self.hass.async_add_executor_job(self._data.update)
|
2020-01-10 21:35:31 +01:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect callbacks."""
|
|
|
|
if self._disp_disconnect:
|
|
|
|
self._disp_disconnect()
|
|
|
|
self._disp_disconnect = None
|
2019-07-31 19:08:40 +01:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2019-07-31 22:21:36 +03:00
|
|
|
"""Return False, updates are controlled via the hub."""
|
2019-07-31 19:08:40 +01:00
|
|
|
return False
|
|
|
|
|
2017-03-05 03:03:00 -05:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
2018-10-16 01:06:00 -07:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self._data.id)},
|
|
|
|
"sw_version": self._data.firmware,
|
|
|
|
"name": self._data.name,
|
|
|
|
"model": self._data.kind,
|
|
|
|
"manufacturer": "Ring",
|
|
|
|
}
|
|
|
|
|
2017-03-05 03:03:00 -05:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attrs = {}
|
|
|
|
|
2019-02-14 22:09:22 +01:00
|
|
|
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
|
2019-07-31 12:25:30 -07:00
|
|
|
attrs["timezone"] = self._data.timezone
|
|
|
|
attrs["wifi_name"] = self._data.wifi_name
|
|
|
|
|
|
|
|
if self._extra and self._sensor_type.startswith("last_"):
|
|
|
|
attrs["created_at"] = self._extra["created_at"]
|
|
|
|
attrs["answered"] = self._extra["answered"]
|
|
|
|
attrs["recording_status"] = self._extra["recording"]["status"]
|
|
|
|
attrs["category"] = self._extra["kind"]
|
2017-03-05 03:03:00 -05:00
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "battery" and self._state is not None:
|
|
|
|
return icon_for_battery_level(
|
|
|
|
battery_level=int(self._state), charging=False
|
|
|
|
)
|
2017-03-05 03:03:00 -05:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the units of measurement."""
|
|
|
|
return SENSOR_TYPES.get(self._sensor_type)[2]
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the state."""
|
2019-07-31 19:08:40 +01:00
|
|
|
_LOGGER.debug("Updating data from %s sensor", self._name)
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "volume":
|
2017-03-05 03:03:00 -05:00
|
|
|
self._state = self._data.volume
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "battery":
|
2017-03-05 03:03:00 -05:00
|
|
|
self._state = self._data.battery_life
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type.startswith("last_"):
|
|
|
|
history = self._data.history(
|
|
|
|
limit=5, timezone=self._tz, kind=self._kind, enforce_limit=True
|
|
|
|
)
|
2017-03-31 11:53:56 -04:00
|
|
|
if history:
|
|
|
|
self._extra = history[0]
|
2019-07-31 12:25:30 -07:00
|
|
|
created_at = self._extra["created_at"]
|
|
|
|
self._state = "{0:0>2}:{1:0>2}".format(
|
|
|
|
created_at.hour, created_at.minute
|
|
|
|
)
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "wifi_signal_category":
|
2017-10-21 10:08:40 -04:00
|
|
|
self._state = self._data.wifi_signal_category
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "wifi_signal_strength":
|
2017-10-21 10:08:40 -04:00
|
|
|
self._state = self._data.wifi_signal_strength
|