2019-08-06 21:55:54 +00:00
|
|
|
"""This component provides HA switch support for Ring Door Bell/Chimes."""
|
2019-09-19 06:39:09 +00:00
|
|
|
from datetime import timedelta
|
2019-12-05 05:13:28 +00:00
|
|
|
import logging
|
|
|
|
|
2019-08-06 21:55:54 +00:00
|
|
|
from homeassistant.components.light import Light
|
|
|
|
from homeassistant.core import callback
|
2019-12-05 05:13:28 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-09-19 06:39:09 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-08-06 21:55:54 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
from . import DOMAIN, SIGNAL_UPDATE_RING
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# It takes a few seconds for the API to correctly return an update indicating
|
|
|
|
# that the changes have been made. Once we request a change (i.e. a light
|
|
|
|
# being turned on) we simply wait for this time delta before we allow
|
|
|
|
# updates to take place.
|
|
|
|
|
|
|
|
SKIP_UPDATES_DELAY = timedelta(seconds=5)
|
|
|
|
|
|
|
|
ON_STATE = "on"
|
|
|
|
OFF_STATE = "off"
|
|
|
|
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Create the lights for the Ring devices."""
|
2020-01-14 20:54:45 +00:00
|
|
|
ring = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
devices = ring.devices()
|
2019-08-06 21:55:54 +00:00
|
|
|
lights = []
|
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
for device in devices["stickup_cams"]:
|
2019-08-06 21:55:54 +00:00
|
|
|
if device.has_capability("light"):
|
|
|
|
lights.append(RingLight(device))
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
async_add_entities(lights, True)
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RingLight(Light):
|
|
|
|
"""Creates a switch to turn the ring cameras light on and off."""
|
|
|
|
|
|
|
|
def __init__(self, device):
|
|
|
|
"""Initialize the light."""
|
|
|
|
self._device = device
|
|
|
|
self._unique_id = self._device.id
|
|
|
|
self._light_on = False
|
2019-09-19 06:39:09 +00:00
|
|
|
self._no_updates_until = dt_util.utcnow()
|
2020-01-10 20:35:31 +00:00
|
|
|
self._disp_disconnect = None
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-01-10 20:35:31 +00:00
|
|
|
self._disp_disconnect = async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_RING, self._update_callback
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect callbacks."""
|
|
|
|
if self._disp_disconnect:
|
|
|
|
self._disp_disconnect()
|
|
|
|
self._disp_disconnect = None
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
_LOGGER.debug("Updating Ring light %s (callback)", self.name)
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Update controlled via the hub."""
|
|
|
|
return False
|
|
|
|
|
2019-08-06 21:55:54 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Name of the light."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self._device.name} light"
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""If the switch is currently on or off."""
|
|
|
|
return self._light_on
|
|
|
|
|
2020-01-12 00:04:39 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
2020-01-14 20:54:45 +00:00
|
|
|
"identifiers": {(DOMAIN, self._device.device_id)},
|
2020-01-12 00:04:39 +00:00
|
|
|
"name": self._device.name,
|
2020-01-14 20:54:45 +00:00
|
|
|
"model": self._device.model,
|
2020-01-12 00:04:39 +00:00
|
|
|
"manufacturer": "Ring",
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:55:54 +00:00
|
|
|
def _set_light(self, new_state):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Update light state, and causes Home Assistant to correctly update."""
|
2019-08-06 21:55:54 +00:00
|
|
|
self._device.lights = new_state
|
|
|
|
self._light_on = new_state == ON_STATE
|
2019-09-19 06:39:09 +00:00
|
|
|
self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
|
2019-08-06 21:55:54 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on for 30 seconds."""
|
|
|
|
self._set_light(ON_STATE)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
|
|
|
self._set_light(OFF_STATE)
|
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
async def async_update(self):
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Update current state of the light."""
|
2019-09-19 06:39:09 +00:00
|
|
|
if self._no_updates_until > dt_util.utcnow():
|
2019-08-06 21:55:54 +00:00
|
|
|
_LOGGER.debug("Skipping update...")
|
|
|
|
return
|
|
|
|
|
|
|
|
self._light_on = self._device.lights == ON_STATE
|