2019-08-06 13:39:07 +01:00
|
|
|
"""This component provides HA switch support for Ring Door Bell/Chimes."""
|
2019-09-19 09:39:09 +03:00
|
|
|
from datetime import timedelta
|
2019-12-05 06:13:28 +01:00
|
|
|
import logging
|
|
|
|
|
2019-08-06 13:39:07 +01:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
from homeassistant.core import callback
|
2019-12-05 06:13:28 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-09-19 09:39:09 +03:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-08-06 13:39:07 +01:00
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
from . import DATA_RING_STICKUP_CAMS, DOMAIN, SIGNAL_UPDATE_RING
|
2019-08-06 13:39:07 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SIREN_ICON = "mdi:alarm-bell"
|
|
|
|
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2019-08-06 13:39:07 +01:00
|
|
|
"""Create the switches for the Ring devices."""
|
|
|
|
cameras = hass.data[DATA_RING_STICKUP_CAMS]
|
|
|
|
switches = []
|
|
|
|
for device in cameras:
|
|
|
|
if device.has_capability("siren"):
|
|
|
|
switches.append(SirenSwitch(device))
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async_add_entities(switches, True)
|
2019-08-06 13:39:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BaseRingSwitch(SwitchDevice):
|
|
|
|
"""Represents a switch for controlling an aspect of a ring device."""
|
|
|
|
|
|
|
|
def __init__(self, device, device_type):
|
|
|
|
"""Initialize the switch."""
|
|
|
|
self._device = device
|
|
|
|
self._device_type = device_type
|
2019-09-03 21:14:39 +02:00
|
|
|
self._unique_id = f"{self._device.id}-{self._device_type}"
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = None
|
2019-08-06 13:39:07 +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
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect callbacks."""
|
|
|
|
if self._disp_disconnect:
|
|
|
|
self._disp_disconnect()
|
|
|
|
self._disp_disconnect = None
|
2019-08-06 13:39:07 +01:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
_LOGGER.debug("Updating Ring sensor %s (callback)", self.name)
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Name of the device."""
|
2019-09-03 21:14:39 +02:00
|
|
|
return f"{self._device.name} {self._device_type}"
|
2019-08-06 13:39:07 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Update controlled via the hub."""
|
|
|
|
return False
|
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self._device.id)},
|
|
|
|
"sw_version": self._device.firmware,
|
|
|
|
"name": self._device.name,
|
|
|
|
"model": self._device.kind,
|
|
|
|
"manufacturer": "Ring",
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:39:07 +01:00
|
|
|
|
|
|
|
class SirenSwitch(BaseRingSwitch):
|
|
|
|
"""Creates a switch to turn the ring cameras siren on and off."""
|
|
|
|
|
|
|
|
def __init__(self, device):
|
|
|
|
"""Initialize the switch for a device with a siren."""
|
|
|
|
super().__init__(device, "siren")
|
2019-09-19 09:39:09 +03:00
|
|
|
self._no_updates_until = dt_util.utcnow()
|
2019-08-06 13:39:07 +01:00
|
|
|
self._siren_on = False
|
|
|
|
|
|
|
|
def _set_switch(self, new_state):
|
2020-01-05 14:09:17 +02:00
|
|
|
"""Update switch state, and causes Home Assistant to correctly update."""
|
2019-08-06 13:39:07 +01:00
|
|
|
self._device.siren = new_state
|
|
|
|
self._siren_on = new_state > 0
|
2019-09-19 09:39:09 +03:00
|
|
|
self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
|
2019-08-06 13:39:07 +01:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""If the switch is currently on or off."""
|
|
|
|
return self._siren_on
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the siren on for 30 seconds."""
|
|
|
|
self._set_switch(1)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the siren off."""
|
|
|
|
self._set_switch(0)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return SIREN_ICON
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update state of the siren."""
|
2019-09-19 09:39:09 +03:00
|
|
|
if self._no_updates_until > dt_util.utcnow():
|
2019-08-06 13:39:07 +01:00
|
|
|
_LOGGER.debug("Skipping update...")
|
|
|
|
return
|
|
|
|
self._siren_on = self._device.siren > 0
|