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

@ -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