Refactor Ring data handling (#30777)
* Refactor Ring data handling * Add async_ to methods
This commit is contained in:
1 parent
1c70435df6
commit
3fd14ca3cf
9 files changed
+514
-482
No files matched your search
@@ -1,13 +1,12 @@
|
||||
"""This component provides HA sensor support for Ring Door Bell/Chimes."""
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from . import ATTRIBUTION, DOMAIN, SIGNAL_UPDATE_RING
|
||||
from . import DOMAIN
|
||||
from .entity import RingEntityMixin
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,8 +21,8 @@ SENSOR_TYPES = {
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up the Ring binary sensors from a config entry."""
|
||||
ring = hass.data[DOMAIN][config_entry.entry_id]
|
||||
devices = ring.devices()
|
||||
ring = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
||||
devices = hass.data[DOMAIN][config_entry.entry_id]["devices"]
|
||||
|
||||
sensors = []
|
||||
|
||||
@@ -33,49 +32,62 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
continue
|
||||
|
||||
for device in devices[device_type]:
|
||||
sensors.append(RingBinarySensor(ring, device, sensor_type))
|
||||
sensors.append(
|
||||
RingBinarySensor(config_entry.entry_id, ring, device, sensor_type)
|
||||
)
|
||||
|
||||
async_add_entities(sensors, True)
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class RingBinarySensor(BinarySensorDevice):
|
||||
class RingBinarySensor(RingEntityMixin, BinarySensorDevice):
|
||||
"""A binary sensor implementation for Ring device."""
|
||||
|
||||
def __init__(self, ring, device, sensor_type):
|
||||
_active_alert = None
|
||||
|
||||
def __init__(self, config_entry_id, ring, device, sensor_type):
|
||||
"""Initialize a sensor for Ring device."""
|
||||
self._sensor_type = sensor_type
|
||||
super().__init__(config_entry_id, device)
|
||||
self._ring = ring
|
||||
self._device = device
|
||||
self._sensor_type = sensor_type
|
||||
self._name = "{0} {1}".format(
|
||||
self._device.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
||||
self._device.name, SENSOR_TYPES.get(sensor_type)[0]
|
||||
)
|
||||
self._device_class = SENSOR_TYPES.get(self._sensor_type)[2]
|
||||
self._device_class = SENSOR_TYPES.get(sensor_type)[2]
|
||||
self._state = None
|
||||
self._unique_id = f"{self._device.id}-{self._sensor_type}"
|
||||
self._disp_disconnect = None
|
||||
self._unique_id = f"{device.id}-{sensor_type}"
|
||||
self._update_alert()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Register callbacks."""
|
||||
self._disp_disconnect = async_dispatcher_connect(
|
||||
self.hass, SIGNAL_UPDATE_RING, self._update_callback
|
||||
)
|
||||
await super().async_added_to_hass()
|
||||
self.ring_objects["dings_data"].async_add_listener(self._dings_update_callback)
|
||||
self._dings_update_callback()
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Disconnect callbacks."""
|
||||
if self._disp_disconnect:
|
||||
self._disp_disconnect()
|
||||
self._disp_disconnect = None
|
||||
await super().async_will_remove_from_hass()
|
||||
self.ring_objects["dings_data"].async_remove_listener(
|
||||
self._dings_update_callback
|
||||
)
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
def _dings_update_callback(self):
|
||||
"""Call update method."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
_LOGGER.debug("Updating Ring binary sensor %s (callback)", self.name)
|
||||
self._update_alert()
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return False, updates are controlled via the hub."""
|
||||
return False
|
||||
@callback
|
||||
def _update_alert(self):
|
||||
"""Update active alert."""
|
||||
self._active_alert = next(
|
||||
(
|
||||
alert
|
||||
for alert in self._ring.active_alerts()
|
||||
if alert["kind"] == self._sensor_type
|
||||
and alert["doorbot_id"] == self._device.id
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -85,7 +97,7 @@ class RingBinarySensor(BinarySensorDevice):
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self._state
|
||||
return self._active_alert is not None
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
@@ -97,32 +109,17 @@ class RingBinarySensor(BinarySensorDevice):
|
||||
"""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",
|
||||
}
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
attrs = {}
|
||||
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
|
||||
attrs = super().device_state_attributes
|
||||
|
||||
if self._device.alert and self._device.alert_expires_at:
|
||||
attrs["expires_at"] = self._device.alert_expires_at
|
||||
attrs["state"] = self._device.alert.get("state")
|
||||
if self._active_alert is None:
|
||||
return attrs
|
||||
|
||||
attrs["state"] = self._active_alert["state"]
|
||||
attrs["expires_at"] = datetime.fromtimestamp(
|
||||
self._active_alert.get("now") + self._active_alert.get("expires_in")
|
||||
).isoformat()
|
||||
|
||||
return attrs
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest data and updates the state."""
|
||||
self._state = any(
|
||||
alert["kind"] == self._sensor_type
|
||||
and alert["doorbot_id"] == self._device.id
|
||||
for alert in self._ring.active_alerts()
|
||||
)
|
||||
Reference in new issue
Block a user