Update Ring to 0.6.0 (#30748)
* Update Ring to 0.6.0 * Update sensor tests * update -> async_update * Delete temp files * Address comments * Final tweaks * Remove stale print
This commit is contained in:
parent
ba3c3057da
commit
c4673ddee1
19 changed files with 417 additions and 418 deletions
|
@ -9,93 +9,98 @@ from homeassistant.helpers.icon import icon_for_battery_level
|
|||
|
||||
from . import (
|
||||
ATTRIBUTION,
|
||||
DATA_RING_CHIMES,
|
||||
DATA_RING_DOORBELLS,
|
||||
DATA_RING_STICKUP_CAMS,
|
||||
DATA_HEALTH_DATA_TRACKER,
|
||||
DATA_HISTORY,
|
||||
DOMAIN,
|
||||
SIGNAL_UPDATE_HEALTH_RING,
|
||||
SIGNAL_UPDATE_RING,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Sensor types: Name, category, units, icon, kind
|
||||
# Sensor types: Name, category, units, icon, kind, device_class
|
||||
SENSOR_TYPES = {
|
||||
"battery": ["Battery", ["doorbell", "stickup_cams"], "%", "battery-50", None],
|
||||
"battery": [
|
||||
"Battery",
|
||||
["doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
"%",
|
||||
None,
|
||||
None,
|
||||
"battery",
|
||||
],
|
||||
"last_activity": [
|
||||
"Last Activity",
|
||||
["doorbell", "stickup_cams"],
|
||||
["doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
None,
|
||||
"history",
|
||||
None,
|
||||
"timestamp",
|
||||
],
|
||||
"last_ding": [
|
||||
"Last Ding",
|
||||
["doorbots", "authorized_doorbots"],
|
||||
None,
|
||||
"history",
|
||||
"ding",
|
||||
"timestamp",
|
||||
],
|
||||
"last_ding": ["Last Ding", ["doorbell"], None, "history", "ding"],
|
||||
"last_motion": [
|
||||
"Last Motion",
|
||||
["doorbell", "stickup_cams"],
|
||||
["doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
None,
|
||||
"history",
|
||||
"motion",
|
||||
"timestamp",
|
||||
],
|
||||
"volume": [
|
||||
"Volume",
|
||||
["chime", "doorbell", "stickup_cams"],
|
||||
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
None,
|
||||
"bell-ring",
|
||||
None,
|
||||
None,
|
||||
],
|
||||
"wifi_signal_category": [
|
||||
"WiFi Signal Category",
|
||||
["chime", "doorbell", "stickup_cams"],
|
||||
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
None,
|
||||
"wifi",
|
||||
None,
|
||||
None,
|
||||
],
|
||||
"wifi_signal_strength": [
|
||||
"WiFi Signal Strength",
|
||||
["chime", "doorbell", "stickup_cams"],
|
||||
["chimes", "doorbots", "authorized_doorbots", "stickup_cams"],
|
||||
"dBm",
|
||||
"wifi",
|
||||
None,
|
||||
"signal_strength",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up a sensor for a Ring device."""
|
||||
ring_chimes = hass.data[DATA_RING_CHIMES]
|
||||
ring_doorbells = hass.data[DATA_RING_DOORBELLS]
|
||||
ring_stickup_cams = hass.data[DATA_RING_STICKUP_CAMS]
|
||||
ring = hass.data[DOMAIN][config_entry.entry_id]
|
||||
devices = ring.devices()
|
||||
# Makes a ton of requests. We will make this a config entry option in the future
|
||||
wifi_enabled = False
|
||||
|
||||
sensors = []
|
||||
for device in ring_chimes:
|
||||
|
||||
for device_type in ("chimes", "doorbots", "authorized_doorbots", "stickup_cams"):
|
||||
for sensor_type in SENSOR_TYPES:
|
||||
if "chime" not in SENSOR_TYPES[sensor_type][1]:
|
||||
if device_type not in SENSOR_TYPES[sensor_type][1]:
|
||||
continue
|
||||
|
||||
if sensor_type in ("wifi_signal_category", "wifi_signal_strength"):
|
||||
await hass.async_add_executor_job(device.update)
|
||||
|
||||
sensors.append(RingSensor(hass, device, sensor_type))
|
||||
|
||||
for device in ring_doorbells:
|
||||
for sensor_type in SENSOR_TYPES:
|
||||
if "doorbell" not in SENSOR_TYPES[sensor_type][1]:
|
||||
if not wifi_enabled and sensor_type.startswith("wifi_"):
|
||||
continue
|
||||
|
||||
if sensor_type in ("wifi_signal_category", "wifi_signal_strength"):
|
||||
await hass.async_add_executor_job(device.update)
|
||||
for device in devices[device_type]:
|
||||
if device_type == "battery" and device.battery_life is None:
|
||||
continue
|
||||
|
||||
sensors.append(RingSensor(hass, device, sensor_type))
|
||||
|
||||
for device in ring_stickup_cams:
|
||||
for sensor_type in SENSOR_TYPES:
|
||||
if "stickup_cams" not in SENSOR_TYPES[sensor_type][1]:
|
||||
continue
|
||||
|
||||
if sensor_type in ("wifi_signal_category", "wifi_signal_strength"):
|
||||
await hass.async_add_executor_job(device.update)
|
||||
|
||||
sensors.append(RingSensor(hass, device, sensor_type))
|
||||
sensors.append(RingSensor(config_entry.entry_id, device, sensor_type))
|
||||
|
||||
async_add_entities(sensors, True)
|
||||
|
||||
|
@ -103,28 +108,42 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
class RingSensor(Entity):
|
||||
"""A sensor implementation for Ring device."""
|
||||
|
||||
def __init__(self, hass, data, sensor_type):
|
||||
def __init__(self, config_entry_id, device, sensor_type):
|
||||
"""Initialize a sensor for Ring device."""
|
||||
super().__init__()
|
||||
self._config_entry_id = config_entry_id
|
||||
self._sensor_type = sensor_type
|
||||
self._data = data
|
||||
self._device = device
|
||||
self._extra = None
|
||||
self._icon = "mdi:{}".format(SENSOR_TYPES.get(self._sensor_type)[3])
|
||||
self._kind = SENSOR_TYPES.get(self._sensor_type)[4]
|
||||
self._name = "{0} {1}".format(
|
||||
self._data.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
||||
self._device.name, SENSOR_TYPES.get(self._sensor_type)[0]
|
||||
)
|
||||
self._state = None
|
||||
self._tz = str(hass.config.time_zone)
|
||||
self._unique_id = f"{self._data.id}-{self._sensor_type}"
|
||||
self._unique_id = f"{self._device.id}-{self._sensor_type}"
|
||||
self._disp_disconnect = None
|
||||
self._disp_disconnect_health = 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 self.hass.async_add_executor_job(self._data.update)
|
||||
if self._sensor_type not in ("wifi_signal_category", "wifi_signal_strength"):
|
||||
return
|
||||
|
||||
self._disp_disconnect_health = async_dispatcher_connect(
|
||||
self.hass, SIGNAL_UPDATE_HEALTH_RING, self._update_callback
|
||||
)
|
||||
await self.hass.data[DATA_HEALTH_DATA_TRACKER].track_device(
|
||||
self._config_entry_id, self._device
|
||||
)
|
||||
# Write the state, it was not available when doing initial update.
|
||||
if self._sensor_type == "wifi_signal_category":
|
||||
self._state = self._device.wifi_signal_category
|
||||
|
||||
if self._sensor_type == "wifi_signal_strength":
|
||||
self._state = self._device.wifi_signal_strength
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Disconnect callbacks."""
|
||||
|
@ -132,6 +151,17 @@ class RingSensor(Entity):
|
|||
self._disp_disconnect()
|
||||
self._disp_disconnect = None
|
||||
|
||||
if self._disp_disconnect_health:
|
||||
self._disp_disconnect_health()
|
||||
self._disp_disconnect_health = None
|
||||
|
||||
if self._sensor_type not in ("wifi_signal_category", "wifi_signal_strength"):
|
||||
return
|
||||
|
||||
self.hass.data[DATA_HEALTH_DATA_TRACKER].untrack_device(
|
||||
self._config_entry_id, self._device
|
||||
)
|
||||
|
||||
@callback
|
||||
def _update_callback(self):
|
||||
"""Call update method."""
|
||||
|
@ -157,14 +187,18 @@ class RingSensor(Entity):
|
|||
"""Return a unique ID."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return sensor device class."""
|
||||
return SENSOR_TYPES[self._sensor_type][5]
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return device info."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._data.id)},
|
||||
"sw_version": self._data.firmware,
|
||||
"name": self._data.name,
|
||||
"model": self._data.kind,
|
||||
"identifiers": {(DOMAIN, self._device.device_id)},
|
||||
"name": self._device.name,
|
||||
"model": self._device.model,
|
||||
"manufacturer": "Ring",
|
||||
}
|
||||
|
||||
|
@ -174,8 +208,6 @@ class RingSensor(Entity):
|
|||
attrs = {}
|
||||
|
||||
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
|
||||
attrs["timezone"] = self._data.timezone
|
||||
attrs["wifi_name"] = self._data.wifi_name
|
||||
|
||||
if self._extra and self._sensor_type.startswith("last_"):
|
||||
attrs["created_at"] = self._extra["created_at"]
|
||||
|
@ -199,29 +231,34 @@ class RingSensor(Entity):
|
|||
"""Return the units of measurement."""
|
||||
return SENSOR_TYPES.get(self._sensor_type)[2]
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Get the latest data and updates the state."""
|
||||
_LOGGER.debug("Updating data from %s sensor", self._name)
|
||||
|
||||
if self._sensor_type == "volume":
|
||||
self._state = self._data.volume
|
||||
self._state = self._device.volume
|
||||
|
||||
if self._sensor_type == "battery":
|
||||
self._state = self._data.battery_life
|
||||
self._state = self._device.battery_life
|
||||
|
||||
if self._sensor_type.startswith("last_"):
|
||||
history = self._data.history(
|
||||
limit=5, timezone=self._tz, kind=self._kind, enforce_limit=True
|
||||
history = await self.hass.data[DATA_HISTORY].async_get_history(
|
||||
self._config_entry_id, self._device
|
||||
)
|
||||
if history:
|
||||
self._extra = history[0]
|
||||
created_at = self._extra["created_at"]
|
||||
self._state = "{0:0>2}:{1:0>2}".format(
|
||||
created_at.hour, created_at.minute
|
||||
)
|
||||
|
||||
found = None
|
||||
for entry in history:
|
||||
if entry["kind"] == self._kind:
|
||||
found = entry
|
||||
break
|
||||
|
||||
if found:
|
||||
self._extra = found
|
||||
created_at = found["created_at"]
|
||||
self._state = created_at.isoformat()
|
||||
|
||||
if self._sensor_type == "wifi_signal_category":
|
||||
self._state = self._data.wifi_signal_category
|
||||
self._state = self._device.wifi_signal_category
|
||||
|
||||
if self._sensor_type == "wifi_signal_strength":
|
||||
self._state = self._data.wifi_signal_strength
|
||||
self._state = self._device.wifi_signal_strength
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue