2023-03-03 10:26:13 +00:00
|
|
|
"""Component providing HA switch support for Ring Door Bell/Chimes."""
|
2024-03-08 14:05:07 +00:00
|
|
|
|
2019-09-19 06:39:09 +00:00
|
|
|
from datetime import timedelta
|
2024-04-11 08:10:56 +00:00
|
|
|
from enum import StrEnum, auto
|
2019-12-05 05:13:28 +00:00
|
|
|
import logging
|
2022-07-31 11:53:22 +00:00
|
|
|
from typing import Any
|
2019-12-05 05:13:28 +00:00
|
|
|
|
2024-04-06 09:49:30 +00:00
|
|
|
from ring_doorbell import RingStickUpCam
|
2020-01-19 21:39:16 +00:00
|
|
|
|
2022-04-27 13:42:01 +00:00
|
|
|
from homeassistant.components.light import ColorMode, LightEntity
|
2022-01-03 14:11:59 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-09-19 06:39:09 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-08-06 21:55:54 +00:00
|
|
|
|
2024-04-11 08:10:56 +00:00
|
|
|
from . import RingData
|
|
|
|
from .const import DOMAIN
|
2024-01-31 09:37:55 +00:00
|
|
|
from .coordinator import RingDataCoordinator
|
2024-03-11 19:23:49 +00:00
|
|
|
from .entity import RingEntity, exception_wrap
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# It takes a few seconds for the API to correctly return an update indicating
|
|
|
|
# that the changes have been made. Once we request a change (i.e. a light
|
|
|
|
# being turned on) we simply wait for this time delta before we allow
|
|
|
|
# updates to take place.
|
|
|
|
|
|
|
|
SKIP_UPDATES_DELAY = timedelta(seconds=5)
|
|
|
|
|
2024-04-11 08:10:56 +00:00
|
|
|
|
|
|
|
class OnOffState(StrEnum):
|
|
|
|
"""Enum for allowed on off states."""
|
|
|
|
|
|
|
|
ON = auto()
|
|
|
|
OFF = auto()
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
|
2022-01-03 14:11:59 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Create the lights for the Ring devices."""
|
2024-04-11 08:10:56 +00:00
|
|
|
ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
devices_coordinator = ring_data.devices_coordinator
|
2019-08-06 21:55:54 +00:00
|
|
|
|
2024-03-13 20:55:00 +00:00
|
|
|
async_add_entities(
|
|
|
|
RingLight(device, devices_coordinator)
|
2024-04-11 08:10:56 +00:00
|
|
|
for device in ring_data.devices.stickup_cams
|
2024-03-13 20:55:00 +00:00
|
|
|
if device.has_capability("light")
|
|
|
|
)
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
|
2024-04-11 09:31:37 +00:00
|
|
|
class RingLight(RingEntity[RingStickUpCam], LightEntity):
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Creates a switch to turn the ring cameras light on and off."""
|
|
|
|
|
2022-04-27 13:42:01 +00:00
|
|
|
_attr_color_mode = ColorMode.ONOFF
|
|
|
|
_attr_supported_color_modes = {ColorMode.ONOFF}
|
2023-07-09 17:55:10 +00:00
|
|
|
_attr_translation_key = "light"
|
2022-04-27 13:42:01 +00:00
|
|
|
|
2024-04-11 08:10:56 +00:00
|
|
|
def __init__(
|
|
|
|
self, device: RingStickUpCam, coordinator: RingDataCoordinator
|
|
|
|
) -> None:
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Initialize the light."""
|
2024-01-31 09:37:55 +00:00
|
|
|
super().__init__(device, coordinator)
|
2024-04-09 09:08:46 +00:00
|
|
|
self._attr_unique_id = str(device.id)
|
2024-04-11 08:10:56 +00:00
|
|
|
self._attr_is_on = device.lights == OnOffState.ON
|
2019-09-19 06:39:09 +00:00
|
|
|
self._no_updates_until = dt_util.utcnow()
|
2019-08-06 21:55:54 +00:00
|
|
|
|
|
|
|
@callback
|
2024-04-11 08:10:56 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Call update method."""
|
2020-01-15 16:10:42 +00:00
|
|
|
if self._no_updates_until > dt_util.utcnow():
|
|
|
|
return
|
2024-04-11 08:10:56 +00:00
|
|
|
device = self._get_coordinator_data().get_stickup_cam(
|
|
|
|
self._device.device_api_id
|
|
|
|
)
|
|
|
|
self._attr_is_on = device.lights == OnOffState.ON
|
2024-01-31 09:37:55 +00:00
|
|
|
super()._handle_coordinator_update()
|
2020-01-14 20:54:45 +00:00
|
|
|
|
2024-03-11 19:23:49 +00:00
|
|
|
@exception_wrap
|
2024-04-11 08:10:56 +00:00
|
|
|
def _set_light(self, new_state: OnOffState) -> None:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Update light state, and causes Home Assistant to correctly update."""
|
2024-03-11 19:23:49 +00:00
|
|
|
self._device.lights = new_state
|
2020-01-19 21:39:16 +00:00
|
|
|
|
2024-04-11 08:10:56 +00:00
|
|
|
self._attr_is_on = new_state == OnOffState.ON
|
2019-09-19 06:39:09 +00:00
|
|
|
self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY
|
2024-01-31 09:37:55 +00:00
|
|
|
self.schedule_update_ha_state()
|
2019-08-06 21:55:54 +00:00
|
|
|
|
2022-07-31 11:53:22 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Turn the light on for 30 seconds."""
|
2024-04-11 08:10:56 +00:00
|
|
|
self._set_light(OnOffState.ON)
|
2019-08-06 21:55:54 +00:00
|
|
|
|
2022-07-31 11:53:22 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2019-08-06 21:55:54 +00:00
|
|
|
"""Turn the light off."""
|
2024-04-11 08:10:56 +00:00
|
|
|
self._set_light(OnOffState.OFF)
|