Use shorthand attributes in Ring (#99829)

This commit is contained in:
Joost Lekkerkerker 2023-09-07 19:51:35 +02:00 committed by GitHub
parent a00cbe2677
commit 02e077daab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 54 deletions

View File

@ -60,6 +60,7 @@ class RingCam(RingEntityMixin, Camera):
self._video_url = None
self._image = None
self._expires_at = dt_util.utcnow() - FORCE_REFRESH_INTERVAL
self._attr_unique_id = device.id
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@ -91,11 +92,6 @@ class RingCam(RingEntityMixin, Camera):
self._expires_at = dt_util.utcnow()
self.async_write_ha_state()
@property
def unique_id(self):
"""Return a unique ID."""
return self._device.id
@property
def extra_state_attributes(self):
"""Return the state attributes."""

View File

@ -19,6 +19,12 @@ class RingEntityMixin(Entity):
self._config_entry_id = config_entry_id
self._device = device
self._attr_extra_state_attributes = {}
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device.device_id)},
manufacturer="Ring",
model=device.model,
name=device.name,
)
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@ -37,13 +43,3 @@ class RingEntityMixin(Entity):
def ring_objects(self):
"""Return the Ring API objects."""
return self.hass.data[DOMAIN][self._config_entry_id]
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device.device_id)},
manufacturer="Ring",
model=self._device.model,
name=self._device.name,
)

View File

@ -55,8 +55,8 @@ class RingLight(RingEntityMixin, LightEntity):
def __init__(self, config_entry_id, device):
"""Initialize the light."""
super().__init__(config_entry_id, device)
self._unique_id = device.id
self._light_on = device.lights == ON_STATE
self._attr_unique_id = device.id
self._attr_is_on = device.lights == ON_STATE
self._no_updates_until = dt_util.utcnow()
@callback
@ -65,19 +65,9 @@ class RingLight(RingEntityMixin, LightEntity):
if self._no_updates_until > dt_util.utcnow():
return
self._light_on = self._device.lights == ON_STATE
self._attr_is_on = self._device.lights == ON_STATE
self.async_write_ha_state()
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
@property
def is_on(self):
"""If the switch is currently on or off."""
return self._light_on
def _set_light(self, new_state):
"""Update light state, and causes Home Assistant to correctly update."""
try:
@ -86,7 +76,7 @@ class RingLight(RingEntityMixin, LightEntity):
_LOGGER.error("Time out setting %s light to %s", self.entity_id, new_state)
return
self._light_on = new_state == ON_STATE
self._attr_is_on = new_state == ON_STATE
self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
self.async_write_ha_state()

View File

@ -68,6 +68,9 @@ class RingSensor(RingEntityMixin, SensorEntity):
class HealthDataRingSensor(RingSensor):
"""Ring sensor that relies on health data."""
# These sensors are data hungry and not useful. Disable by default.
_attr_entity_registry_enabled_default = False
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()
@ -89,12 +92,6 @@ class HealthDataRingSensor(RingSensor):
"""Call update method."""
self.async_write_ha_state()
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
# These sensors are data hungry and not useful. Disable by default.
return False
@property
def native_value(self):
"""Return the state of the sensor."""

View File

@ -50,24 +50,20 @@ class BaseRingSwitch(RingEntityMixin, SwitchEntity):
"""Initialize the switch."""
super().__init__(config_entry_id, device)
self._device_type = device_type
self._unique_id = f"{self._device.id}-{self._device_type}"
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
self._attr_unique_id = f"{self._device.id}-{self._device_type}"
class SirenSwitch(BaseRingSwitch):
"""Creates a switch to turn the ring cameras siren on and off."""
_attr_translation_key = "siren"
_attr_icon = SIREN_ICON
def __init__(self, config_entry_id, device):
"""Initialize the switch for a device with a siren."""
super().__init__(config_entry_id, device, "siren")
self._no_updates_until = dt_util.utcnow()
self._siren_on = device.siren > 0
self._attr_is_on = device.siren > 0
@callback
def _update_callback(self):
@ -75,7 +71,7 @@ class SirenSwitch(BaseRingSwitch):
if self._no_updates_until > dt_util.utcnow():
return
self._siren_on = self._device.siren > 0
self._attr_is_on = self._device.siren > 0
self.async_write_ha_state()
def _set_switch(self, new_state):
@ -86,15 +82,10 @@ class SirenSwitch(BaseRingSwitch):
_LOGGER.error("Time out setting %s siren to %s", self.entity_id, new_state)
return
self._siren_on = new_state > 0
self._attr_is_on = new_state > 0
self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
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: Any) -> None:
"""Turn the siren on for 30 seconds."""
self._set_switch(1)
@ -102,8 +93,3 @@ class SirenSwitch(BaseRingSwitch):
def turn_off(self, **kwargs: Any) -> None:
"""Turn the siren off."""
self._set_switch(0)
@property
def icon(self):
"""Return the icon."""
return SIREN_ICON