Refactor Ring data handling (#30777)

* Refactor Ring data handling

* Add async_ to methods
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-15 08:10:42 -08:00
1 parent de26108b23
commit 1e82813c3b
9 files changed
+514 -482

No files matched your search

+20 -53
View File
@@ -4,10 +4,10 @@ import logging
from homeassistant.components.switch import SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.util.dt as dt_util
from . import DOMAIN, SIGNAL_UPDATE_RING
from . import DOMAIN
from .entity import RingEntityMixin
_LOGGER = logging.getLogger(__name__)
@@ -24,49 +24,24 @@ SKIP_UPDATES_DELAY = timedelta(seconds=5)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Create the switches for the Ring devices."""
ring = hass.data[DOMAIN][config_entry.entry_id]
devices = ring.devices()
devices = hass.data[DOMAIN][config_entry.entry_id]["devices"]
switches = []
for device in devices["stickup_cams"]:
if device.has_capability("siren"):
switches.append(SirenSwitch(device))
switches.append(SirenSwitch(config_entry.entry_id, device))
async_add_entities(switches, True)
async_add_entities(switches)
class BaseRingSwitch(SwitchDevice):
class BaseRingSwitch(RingEntityMixin, SwitchDevice):
"""Represents a switch for controlling an aspect of a ring device."""
def __init__(self, device, device_type):
def __init__(self, config_entry_id, device, device_type):
"""Initialize the switch."""
self._device = device
super().__init__(config_entry_id, device)
self._device_type = device_type
self._unique_id = f"{self._device.id}-{self._device_type}"
self._disp_disconnect = None
async def async_added_to_hass(self):
"""Register callbacks."""
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
@callback
def _update_callback(self):
"""Call update method."""
_LOGGER.debug("Updating Ring switch %s (callback)", self.name)
self.async_schedule_update_ha_state(True)
@property
def should_poll(self):
"""Update controlled via the hub."""
return False
@property
def name(self):
@@ -78,25 +53,24 @@ class BaseRingSwitch(SwitchDevice):
"""Return a unique ID."""
return self._unique_id
@property
def device_info(self):
"""Return device info."""
return {
"identifiers": {(DOMAIN, self._device.device_id)},
"name": self._device.name,
"model": self._device.model,
"manufacturer": "Ring",
}
class SirenSwitch(BaseRingSwitch):
"""Creates a switch to turn the ring cameras siren on and off."""
def __init__(self, device):
def __init__(self, config_entry_id, device):
"""Initialize the switch for a device with a siren."""
super().__init__(device, "siren")
super().__init__(config_entry_id, device, "siren")
self._no_updates_until = dt_util.utcnow()
self._siren_on = False
self._siren_on = device.siren > 0
@callback
def _update_callback(self):
"""Call update method."""
if self._no_updates_until > dt_util.utcnow():
return
self._siren_on = self._device.siren > 0
self.async_write_ha_state()
def _set_switch(self, new_state):
"""Update switch state, and causes Home Assistant to correctly update."""
@@ -122,10 +96,3 @@ class SirenSwitch(BaseRingSwitch):
def icon(self):
"""Return the icon."""
return SIREN_ICON
async def async_update(self):
"""Update state of the siren."""
if self._no_updates_until > dt_util.utcnow():
_LOGGER.debug("Skipping update...")
return
self._siren_on = self._device.siren > 0