mirror of https://github.com/home-assistant/core
Use shorthand attributes in Ring (#99829)
This commit is contained in:
parent
a00cbe2677
commit
02e077daab
|
@ -60,6 +60,7 @@ class RingCam(RingEntityMixin, Camera):
|
||||||
self._video_url = None
|
self._video_url = None
|
||||||
self._image = None
|
self._image = None
|
||||||
self._expires_at = dt_util.utcnow() - FORCE_REFRESH_INTERVAL
|
self._expires_at = dt_util.utcnow() - FORCE_REFRESH_INTERVAL
|
||||||
|
self._attr_unique_id = device.id
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
@ -91,11 +92,6 @@ class RingCam(RingEntityMixin, Camera):
|
||||||
self._expires_at = dt_util.utcnow()
|
self._expires_at = dt_util.utcnow()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return self._device.id
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
|
|
|
@ -19,6 +19,12 @@ class RingEntityMixin(Entity):
|
||||||
self._config_entry_id = config_entry_id
|
self._config_entry_id = config_entry_id
|
||||||
self._device = device
|
self._device = device
|
||||||
self._attr_extra_state_attributes = {}
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
@ -37,13 +43,3 @@ class RingEntityMixin(Entity):
|
||||||
def ring_objects(self):
|
def ring_objects(self):
|
||||||
"""Return the Ring API objects."""
|
"""Return the Ring API objects."""
|
||||||
return self.hass.data[DOMAIN][self._config_entry_id]
|
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,
|
|
||||||
)
|
|
||||||
|
|
|
@ -55,8 +55,8 @@ class RingLight(RingEntityMixin, LightEntity):
|
||||||
def __init__(self, config_entry_id, device):
|
def __init__(self, config_entry_id, device):
|
||||||
"""Initialize the light."""
|
"""Initialize the light."""
|
||||||
super().__init__(config_entry_id, device)
|
super().__init__(config_entry_id, device)
|
||||||
self._unique_id = device.id
|
self._attr_unique_id = device.id
|
||||||
self._light_on = device.lights == ON_STATE
|
self._attr_is_on = device.lights == ON_STATE
|
||||||
self._no_updates_until = dt_util.utcnow()
|
self._no_updates_until = dt_util.utcnow()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -65,19 +65,9 @@ class RingLight(RingEntityMixin, LightEntity):
|
||||||
if self._no_updates_until > dt_util.utcnow():
|
if self._no_updates_until > dt_util.utcnow():
|
||||||
return
|
return
|
||||||
|
|
||||||
self._light_on = self._device.lights == ON_STATE
|
self._attr_is_on = self._device.lights == ON_STATE
|
||||||
self.async_write_ha_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):
|
def _set_light(self, new_state):
|
||||||
"""Update light state, and causes Home Assistant to correctly update."""
|
"""Update light state, and causes Home Assistant to correctly update."""
|
||||||
try:
|
try:
|
||||||
|
@ -86,7 +76,7 @@ class RingLight(RingEntityMixin, LightEntity):
|
||||||
_LOGGER.error("Time out setting %s light to %s", self.entity_id, new_state)
|
_LOGGER.error("Time out setting %s light to %s", self.entity_id, new_state)
|
||||||
return
|
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._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,9 @@ class RingSensor(RingEntityMixin, SensorEntity):
|
||||||
class HealthDataRingSensor(RingSensor):
|
class HealthDataRingSensor(RingSensor):
|
||||||
"""Ring sensor that relies on health data."""
|
"""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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
|
@ -89,12 +92,6 @@ class HealthDataRingSensor(RingSensor):
|
||||||
"""Call update method."""
|
"""Call update method."""
|
||||||
self.async_write_ha_state()
|
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
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
|
|
|
@ -50,24 +50,20 @@ class BaseRingSwitch(RingEntityMixin, SwitchEntity):
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(config_entry_id, device)
|
super().__init__(config_entry_id, device)
|
||||||
self._device_type = device_type
|
self._device_type = device_type
|
||||||
self._unique_id = f"{self._device.id}-{self._device_type}"
|
self._attr_unique_id = f"{self._device.id}-{self._device_type}"
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
|
|
||||||
class SirenSwitch(BaseRingSwitch):
|
class SirenSwitch(BaseRingSwitch):
|
||||||
"""Creates a switch to turn the ring cameras siren on and off."""
|
"""Creates a switch to turn the ring cameras siren on and off."""
|
||||||
|
|
||||||
_attr_translation_key = "siren"
|
_attr_translation_key = "siren"
|
||||||
|
_attr_icon = SIREN_ICON
|
||||||
|
|
||||||
def __init__(self, config_entry_id, device):
|
def __init__(self, config_entry_id, device):
|
||||||
"""Initialize the switch for a device with a siren."""
|
"""Initialize the switch for a device with a siren."""
|
||||||
super().__init__(config_entry_id, device, "siren")
|
super().__init__(config_entry_id, device, "siren")
|
||||||
self._no_updates_until = dt_util.utcnow()
|
self._no_updates_until = dt_util.utcnow()
|
||||||
self._siren_on = device.siren > 0
|
self._attr_is_on = device.siren > 0
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _update_callback(self):
|
def _update_callback(self):
|
||||||
|
@ -75,7 +71,7 @@ class SirenSwitch(BaseRingSwitch):
|
||||||
if self._no_updates_until > dt_util.utcnow():
|
if self._no_updates_until > dt_util.utcnow():
|
||||||
return
|
return
|
||||||
|
|
||||||
self._siren_on = self._device.siren > 0
|
self._attr_is_on = self._device.siren > 0
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
def _set_switch(self, new_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)
|
_LOGGER.error("Time out setting %s siren to %s", self.entity_id, new_state)
|
||||||
return
|
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._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
|
||||||
self.schedule_update_ha_state()
|
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:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the siren on for 30 seconds."""
|
"""Turn the siren on for 30 seconds."""
|
||||||
self._set_switch(1)
|
self._set_switch(1)
|
||||||
|
@ -102,8 +93,3 @@ class SirenSwitch(BaseRingSwitch):
|
||||||
def turn_off(self, **kwargs: Any) -> None:
|
def turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the siren off."""
|
"""Turn the siren off."""
|
||||||
self._set_switch(0)
|
self._set_switch(0)
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon."""
|
|
||||||
return SIREN_ICON
|
|
||||||
|
|
Loading…
Reference in New Issue