2019-04-03 17:40:03 +02:00
|
|
|
"""This component provides HA sensor support for Ring Door Bell/Chimes."""
|
2017-03-31 11:53:56 -04:00
|
|
|
from datetime import timedelta
|
2019-03-20 22:56:46 -07:00
|
|
|
import logging
|
2017-03-31 11:53:56 -04:00
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
from . import ATTRIBUTION, DATA_RING_DOORBELLS, DATA_RING_STICKUP_CAMS, DOMAIN
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-07-11 03:27:22 -04:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
# Sensor types: Name, category, device_class
|
|
|
|
SENSOR_TYPES = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"ding": ["Ding", ["doorbell"], "occupancy"],
|
|
|
|
"motion": ["Motion", ["doorbell", "stickup_cams"], "motion"],
|
2017-03-31 11:53:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Ring binary sensors from a config entry."""
|
2019-07-31 19:08:40 +01:00
|
|
|
ring_doorbells = hass.data[DATA_RING_DOORBELLS]
|
|
|
|
ring_stickup_cams = hass.data[DATA_RING_STICKUP_CAMS]
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
sensors = []
|
2019-07-31 19:08:40 +01:00
|
|
|
for device in ring_doorbells: # ring.doorbells is doing I/O
|
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]:
|
2019-02-14 22:09:22 +01:00
|
|
|
sensors.append(RingBinarySensor(hass, device, sensor_type))
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 19:08:40 +01:00
|
|
|
for device in ring_stickup_cams: # ring.stickup_cams is doing I/O
|
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]:
|
2019-02-14 22:09:22 +01:00
|
|
|
sensors.append(RingBinarySensor(hass, device, sensor_type))
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async_add_entities(sensors, True)
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RingBinarySensor(BinarySensorDevice):
|
|
|
|
"""A binary 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-31 11:53:56 -04:00
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._data = data
|
2019-02-14 22:09:22 +01:00
|
|
|
self._name = "{0} {1}".format(
|
2019-07-31 12:25:30 -07:00
|
|
|
self._data.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
|
|
|
)
|
2017-03-31 11:53:56 -04:00
|
|
|
self._device_class = SENSOR_TYPES.get(self._sensor_type)[2]
|
|
|
|
self._state = None
|
2019-09-03 21:14:39 +02:00
|
|
|
self._unique_id = f"{self._data.id}-{self._sensor_type}"
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if the binary sensor is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of the binary sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
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-31 11:53:56 -04:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attrs = {}
|
2019-02-14 22:09:22 +01:00
|
|
|
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
|
2017-03-31 11:53:56 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
attrs["timezone"] = self._data.timezone
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
if self._data.alert and self._data.alert_expires_at:
|
2019-07-31 12:25:30 -07:00
|
|
|
attrs["expires_at"] = self._data.alert_expires_at
|
|
|
|
attrs["state"] = self._data.alert.get("state")
|
2017-03-31 11:53:56 -04:00
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the state."""
|
|
|
|
self._data.check_alerts()
|
|
|
|
|
|
|
|
if self._data.alert:
|
2019-07-31 12:25:30 -07:00
|
|
|
if self._sensor_type == self._data.alert.get(
|
|
|
|
"kind"
|
|
|
|
) and self._data.account_id == self._data.alert.get("doorbot_id"):
|
2017-09-01 03:14:16 -04:00
|
|
|
self._state = True
|
2017-03-31 11:53:56 -04:00
|
|
|
else:
|
|
|
|
self._state = False
|