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,
|
2020-01-14 12:54:45 -08:00
|
|
|
DATA_HEALTH_DATA_TRACKER,
|
|
|
|
DATA_HISTORY,
|
2020-01-11 16:04:39 -08:00
|
|
|
DOMAIN,
|
2020-01-14 12:54:45 -08:00
|
|
|
SIGNAL_UPDATE_HEALTH_RING,
|
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__)
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
# Sensor types: Name, category, units, icon, kind, device_class
|
2017-03-05 03:03:00 -05:00
|
|
|
SENSOR_TYPES = {
|
2020-01-14 12:54:45 -08:00
|
|
|
"battery": [
|
|
|
|
"Battery",
|
|
|
|
["doorbots", "authorized_doorbots", "stickup_cams"],
|
|
|
|
"%",
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
"battery",
|
|
|
|
],
|
2019-07-31 12:25:30 -07:00
|
|
|
"last_activity": [
|
|
|
|
"Last Activity",
|
2020-01-14 12:54:45 -08:00
|
|
|
["doorbots", "authorized_doorbots", "stickup_cams"],
|
2019-07-31 12:25:30 -07:00
|
|
|
None,
|
|
|
|
"history",
|
|
|
|
None,
|
2020-01-14 12:54:45 -08:00
|
|
|
"timestamp",
|
|
|
|
],
|
|
|
|
"last_ding": [
|
|
|
|
"Last Ding",
|
|
|
|
["doorbots", "authorized_doorbots"],
|
|
|
|
None,
|
|
|
|
"history",
|
|
|
|
"ding",
|
|
|
|
"timestamp",
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
|
|
|
"last_motion": [
|
|
|
|
"Last Motion",
|
2020-01-14 12:54:45 -08:00
|
|
|
["doorbots", "authorized_doorbots", "stickup_cams"],
|
2019-07-31 12:25:30 -07:00
|
|
|
None,
|
|
|
|
"history",
|
|
|
|
"motion",
|
2020-01-14 12:54:45 -08:00
|
|
|
"timestamp",
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
|
|
|
"volume": [
|
|
|
|
"Volume",
|
2020-01-14 12:54:45 -08:00
|
|
|
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
2019-07-31 12:25:30 -07:00
|
|
|
None,
|
|
|
|
"bell-ring",
|
|
|
|
None,
|
2020-01-14 12:54:45 -08:00
|
|
|
None,
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
|
|
|
"wifi_signal_category": [
|
|
|
|
"WiFi Signal Category",
|
2020-01-14 12:54:45 -08:00
|
|
|
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
2019-07-31 12:25:30 -07:00
|
|
|
None,
|
|
|
|
"wifi",
|
|
|
|
None,
|
2020-01-14 12:54:45 -08:00
|
|
|
None,
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
|
|
|
"wifi_signal_strength": [
|
|
|
|
"WiFi Signal Strength",
|
2020-01-14 12:54:45 -08:00
|
|
|
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
2019-07-31 12:25:30 -07:00
|
|
|
"dBm",
|
|
|
|
"wifi",
|
|
|
|
None,
|
2020-01-14 12:54:45 -08:00
|
|
|
"signal_strength",
|
2019-07-31 12:25:30 -07:00
|
|
|
],
|
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."""
|
2020-01-14 12:54:45 -08:00
|
|
|
ring = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
devices = ring.devices()
|
|
|
|
# Makes a ton of requests. We will make this a config entry option in the future
|
|
|
|
wifi_enabled = False
|
2017-03-05 03:03:00 -05:00
|
|
|
|
|
|
|
sensors = []
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
for device_type in ("chimes", "doorbots", "authorized_doorbots", "stickup_cams"):
|
2020-01-10 21:35:31 +01:00
|
|
|
for sensor_type in SENSOR_TYPES:
|
2020-01-14 12:54:45 -08:00
|
|
|
if device_type not in SENSOR_TYPES[sensor_type][1]:
|
2020-01-13 09:13:46 -08:00
|
|
|
continue
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
if not wifi_enabled and sensor_type.startswith("wifi_"):
|
2020-01-13 09:13:46 -08:00
|
|
|
continue
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
for device in devices[device_type]:
|
|
|
|
if device_type == "battery" and device.battery_life is None:
|
|
|
|
continue
|
2020-01-13 09:13:46 -08:00
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
sensors.append(RingSensor(config_entry.entry_id, device, sensor_type))
|
2017-10-21 10:08:40 -04:00
|
|
|
|
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."""
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
def __init__(self, config_entry_id, device, sensor_type):
|
2017-03-05 03:03:00 -05:00
|
|
|
"""Initialize a sensor for Ring device."""
|
2020-01-14 12:54:45 -08:00
|
|
|
self._config_entry_id = config_entry_id
|
2017-03-05 03:03:00 -05:00
|
|
|
self._sensor_type = sensor_type
|
2020-01-14 12:54:45 -08:00
|
|
|
self._device = device
|
2017-03-05 03:03:00 -05:00
|
|
|
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(
|
2020-01-14 12:54:45 -08:00
|
|
|
self._device.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-01-24 08:20:20 +01:00
|
|
|
self._state = None
|
2020-01-14 12:54:45 -08:00
|
|
|
self._unique_id = f"{self._device.id}-{self._sensor_type}"
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = None
|
2020-01-14 12:54:45 -08:00
|
|
|
self._disp_disconnect_health = 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-14 12:54:45 -08:00
|
|
|
if self._sensor_type not in ("wifi_signal_category", "wifi_signal_strength"):
|
|
|
|
return
|
|
|
|
|
|
|
|
self._disp_disconnect_health = async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_HEALTH_RING, self._update_callback
|
|
|
|
)
|
|
|
|
await self.hass.data[DATA_HEALTH_DATA_TRACKER].track_device(
|
|
|
|
self._config_entry_id, self._device
|
|
|
|
)
|
|
|
|
# Write the state, it was not available when doing initial update.
|
|
|
|
if self._sensor_type == "wifi_signal_category":
|
|
|
|
self._state = self._device.wifi_signal_category
|
|
|
|
|
|
|
|
if self._sensor_type == "wifi_signal_strength":
|
|
|
|
self._state = self._device.wifi_signal_strength
|
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
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
if self._disp_disconnect_health:
|
|
|
|
self._disp_disconnect_health()
|
|
|
|
self._disp_disconnect_health = None
|
|
|
|
|
|
|
|
if self._sensor_type not in ("wifi_signal_category", "wifi_signal_strength"):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.hass.data[DATA_HEALTH_DATA_TRACKER].untrack_device(
|
|
|
|
self._config_entry_id, self._device
|
|
|
|
)
|
|
|
|
|
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-14 12:54:45 -08:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return sensor device class."""
|
|
|
|
return SENSOR_TYPES[self._sensor_type][5]
|
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
2020-01-14 12:54:45 -08:00
|
|
|
"identifiers": {(DOMAIN, self._device.device_id)},
|
|
|
|
"name": self._device.name,
|
|
|
|
"model": self._device.model,
|
2020-01-11 16:04:39 -08:00
|
|
|
"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
|
|
|
|
|
|
|
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]
|
|
|
|
|
2020-01-14 12:54:45 -08:00
|
|
|
async def async_update(self):
|
2017-03-05 03:03:00 -05:00
|
|
|
"""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":
|
2020-01-14 12:54:45 -08:00
|
|
|
self._state = self._device.volume
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "battery":
|
2020-01-14 12:54:45 -08:00
|
|
|
self._state = self._device.battery_life
|
2017-03-05 03:03:00 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type.startswith("last_"):
|
2020-01-14 12:54:45 -08:00
|
|
|
history = await self.hass.data[DATA_HISTORY].async_get_history(
|
|
|
|
self._config_entry_id, self._device
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2020-01-14 12:54:45 -08:00
|
|
|
|
|
|
|
found = None
|
|
|
|
for entry in history:
|
|
|
|
if entry["kind"] == self._kind:
|
|
|
|
found = entry
|
|
|
|
break
|
|
|
|
|
|
|
|
if found:
|
|
|
|
self._extra = found
|
|
|
|
created_at = found["created_at"]
|
|
|
|
self._state = created_at.isoformat()
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "wifi_signal_category":
|
2020-01-14 12:54:45 -08:00
|
|
|
self._state = self._device.wifi_signal_category
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == "wifi_signal_strength":
|
2020-01-14 12:54:45 -08:00
|
|
|
self._state = self._device.wifi_signal_strength
|