From 02e077daab5c7c01548d4db590889f5baa04e47a Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 7 Sep 2023 19:51:35 +0200 Subject: [PATCH] Use shorthand attributes in Ring (#99829) --- homeassistant/components/ring/camera.py | 6 +----- homeassistant/components/ring/entity.py | 16 ++++++---------- homeassistant/components/ring/light.py | 18 ++++-------------- homeassistant/components/ring/sensor.py | 9 +++------ homeassistant/components/ring/switch.py | 24 +++++------------------- 5 files changed, 19 insertions(+), 54 deletions(-) diff --git a/homeassistant/components/ring/camera.py b/homeassistant/components/ring/camera.py index 0b3f1509b18..7f897d17203 100644 --- a/homeassistant/components/ring/camera.py +++ b/homeassistant/components/ring/camera.py @@ -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.""" diff --git a/homeassistant/components/ring/entity.py b/homeassistant/components/ring/entity.py index 2b345b3b703..7160d2ef725 100644 --- a/homeassistant/components/ring/entity.py +++ b/homeassistant/components/ring/entity.py @@ -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, - ) diff --git a/homeassistant/components/ring/light.py b/homeassistant/components/ring/light.py index 2604e557b79..93640e2764e 100644 --- a/homeassistant/components/ring/light.py +++ b/homeassistant/components/ring/light.py @@ -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() diff --git a/homeassistant/components/ring/sensor.py b/homeassistant/components/ring/sensor.py index fbaeb8a4b5b..af23af07eba 100644 --- a/homeassistant/components/ring/sensor.py +++ b/homeassistant/components/ring/sensor.py @@ -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.""" diff --git a/homeassistant/components/ring/switch.py b/homeassistant/components/ring/switch.py index 43bd303577a..7069acd5f0f 100644 --- a/homeassistant/components/ring/switch.py +++ b/homeassistant/components/ring/switch.py @@ -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