Refactor Ring data handling (#30777)
* Refactor Ring data handling * Add async_ to methods
This commit is contained in:
1 parent
de26108b23
commit
1e82813c3b
9 files changed
+514
-482
No files matched your search
@@ -12,10 +12,10 @@ from homeassistant.components.ffmpeg import DATA_FFMPEG
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import ATTRIBUTION, DATA_HISTORY, DOMAIN, SIGNAL_UPDATE_RING
|
||||
from . import ATTRIBUTION, DOMAIN
|
||||
from .entity import RingEntityMixin
|
||||
|
||||
FORCE_REFRESH_INTERVAL = timedelta(minutes=45)
|
||||
|
||||
@@ -24,8 +24,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up a Ring Door Bell and StickUp Camera."""
|
||||
ring = hass.data[DOMAIN][config_entry.entry_id]
|
||||
devices = ring.devices()
|
||||
devices = hass.data[DOMAIN][config_entry.entry_id]["devices"]
|
||||
|
||||
cams = []
|
||||
for camera in chain(
|
||||
@@ -36,42 +35,52 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
||||
cams.append(RingCam(config_entry.entry_id, hass.data[DATA_FFMPEG], camera))
|
||||
|
||||
async_add_entities(cams, True)
|
||||
async_add_entities(cams)
|
||||
|
||||
|
||||
class RingCam(Camera):
|
||||
class RingCam(RingEntityMixin, Camera):
|
||||
"""An implementation of a Ring Door Bell camera."""
|
||||
|
||||
def __init__(self, config_entry_id, ffmpeg, device):
|
||||
"""Initialize a Ring Door Bell camera."""
|
||||
super().__init__()
|
||||
self._config_entry_id = config_entry_id
|
||||
self._device = device
|
||||
super().__init__(config_entry_id, device)
|
||||
|
||||
self._name = self._device.name
|
||||
self._ffmpeg = ffmpeg
|
||||
self._last_event = None
|
||||
self._last_video_id = None
|
||||
self._video_url = None
|
||||
self._utcnow = dt_util.utcnow()
|
||||
self._expires_at = self._utcnow - FORCE_REFRESH_INTERVAL
|
||||
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
|
||||
await super().async_added_to_hass()
|
||||
|
||||
await self.ring_objects["history_data"].async_track_device(
|
||||
self._device, self._history_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["history_data"].async_untrack_device(
|
||||
self._device, self._history_update_callback
|
||||
)
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
def _history_update_callback(self, history_data):
|
||||
"""Call update method."""
|
||||
self.async_schedule_update_ha_state(True)
|
||||
_LOGGER.debug("Updating Ring camera %s (callback)", self.name)
|
||||
if history_data:
|
||||
self._last_event = history_data[0]
|
||||
self.async_schedule_update_ha_state(True)
|
||||
else:
|
||||
self._last_event = None
|
||||
self._last_video_id = None
|
||||
self._video_url = None
|
||||
self._expires_at = self._utcnow
|
||||
self.async_write_ha_state()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -83,16 +92,6 @@ class RingCam(Camera):
|
||||
"""Return a unique ID."""
|
||||
return self._device.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."""
|
||||
@@ -104,7 +103,6 @@ class RingCam(Camera):
|
||||
|
||||
async def async_camera_image(self):
|
||||
"""Return a still image response from the camera."""
|
||||
|
||||
ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
|
||||
|
||||
if self._video_url is None:
|
||||
@@ -136,33 +134,23 @@ class RingCam(Camera):
|
||||
|
||||
async def async_update(self):
|
||||
"""Update camera entity and refresh attributes."""
|
||||
_LOGGER.debug("Checking if Ring DoorBell needs to refresh video_url")
|
||||
|
||||
self._utcnow = dt_util.utcnow()
|
||||
|
||||
data = await self.hass.data[DATA_HISTORY].async_get_history(
|
||||
self._config_entry_id, self._device
|
||||
)
|
||||
|
||||
if not data:
|
||||
if self._last_event is None:
|
||||
return
|
||||
|
||||
last_event = data[0]
|
||||
last_recording_id = last_event["id"]
|
||||
video_status = last_event["recording"]["status"]
|
||||
if self._last_event["recording"]["status"] != "ready":
|
||||
return
|
||||
|
||||
if video_status == "ready" and (
|
||||
self._last_video_id != last_recording_id or self._utcnow >= self._expires_at
|
||||
if (
|
||||
self._last_video_id == self._last_event["id"]
|
||||
and self._utcnow <= self._expires_at
|
||||
):
|
||||
return
|
||||
|
||||
video_url = await self.hass.async_add_executor_job(
|
||||
self._device.recording_url, last_recording_id
|
||||
)
|
||||
video_url = await self.hass.async_add_executor_job(
|
||||
self._device.recording_url, self._last_event["id"]
|
||||
)
|
||||
|
||||
if video_url:
|
||||
_LOGGER.debug("Ring DoorBell properties refreshed")
|
||||
|
||||
# update attributes if new video or if URL has expired
|
||||
self._last_video_id = last_recording_id
|
||||
self._video_url = video_url
|
||||
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow
|
||||
if video_url:
|
||||
self._last_video_id = self._last_event["id"]
|
||||
self._video_url = video_url
|
||||
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow
|
||||
Reference in new issue
Block a user