100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
|
"""Support for Blink Motion detection switches."""
|
||
|
from __future__ import annotations
|
||
|
|
||
|
import asyncio
|
||
|
from typing import Any
|
||
|
|
||
|
from homeassistant.components.switch import (
|
||
|
SwitchDeviceClass,
|
||
|
SwitchEntity,
|
||
|
SwitchEntityDescription,
|
||
|
)
|
||
|
from homeassistant.config_entries import ConfigEntry
|
||
|
from homeassistant.core import HomeAssistant
|
||
|
from homeassistant.exceptions import HomeAssistantError
|
||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||
|
|
||
|
from .const import DEFAULT_BRAND, DOMAIN, TYPE_CAMERA_ARMED
|
||
|
from .coordinator import BlinkUpdateCoordinator
|
||
|
|
||
|
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
|
||
|
SwitchEntityDescription(
|
||
|
key=TYPE_CAMERA_ARMED,
|
||
|
icon="mdi:motion-sensor",
|
||
|
translation_key="camera_motion",
|
||
|
device_class=SwitchDeviceClass.SWITCH,
|
||
|
),
|
||
|
)
|
||
|
|
||
|
|
||
|
async def async_setup_entry(
|
||
|
hass: HomeAssistant,
|
||
|
config: ConfigEntry,
|
||
|
async_add_entities: AddEntitiesCallback,
|
||
|
) -> None:
|
||
|
"""Set up the Blink switches."""
|
||
|
coordinator: BlinkUpdateCoordinator = hass.data[DOMAIN][config.entry_id]
|
||
|
|
||
|
async_add_entities(
|
||
|
BlinkSwitch(coordinator, camera, description)
|
||
|
for camera in coordinator.api.cameras
|
||
|
for description in SWITCH_TYPES
|
||
|
)
|
||
|
|
||
|
|
||
|
class BlinkSwitch(CoordinatorEntity[BlinkUpdateCoordinator], SwitchEntity):
|
||
|
"""Representation of a Blink motion detection switch."""
|
||
|
|
||
|
_attr_has_entity_name = True
|
||
|
|
||
|
def __init__(
|
||
|
self,
|
||
|
coordinator: BlinkUpdateCoordinator,
|
||
|
camera,
|
||
|
description: SwitchEntityDescription,
|
||
|
) -> None:
|
||
|
"""Initialize the switch."""
|
||
|
super().__init__(coordinator)
|
||
|
self._camera = coordinator.api.cameras[camera]
|
||
|
self.entity_description = description
|
||
|
serial = self._camera.serial
|
||
|
self._attr_unique_id = f"{serial}-{description.key}"
|
||
|
self._attr_device_info = DeviceInfo(
|
||
|
identifiers={(DOMAIN, serial)},
|
||
|
serial_number=serial,
|
||
|
name=camera,
|
||
|
manufacturer=DEFAULT_BRAND,
|
||
|
model=self._camera.camera_type,
|
||
|
)
|
||
|
|
||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||
|
"""Turn the switch on."""
|
||
|
try:
|
||
|
await self._camera.async_arm(True)
|
||
|
|
||
|
except asyncio.TimeoutError as er:
|
||
|
raise HomeAssistantError(
|
||
|
"Blink failed to arm camera motion detection"
|
||
|
) from er
|
||
|
|
||
|
await self.coordinator.async_refresh()
|
||
|
|
||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||
|
"""Turn the switch off."""
|
||
|
try:
|
||
|
await self._camera.async_arm(False)
|
||
|
|
||
|
except asyncio.TimeoutError as er:
|
||
|
raise HomeAssistantError(
|
||
|
"Blink failed to dis-arm camera motion detection"
|
||
|
) from er
|
||
|
|
||
|
await self.coordinator.async_refresh()
|
||
|
|
||
|
@property
|
||
|
def is_on(self) -> bool:
|
||
|
"""Return if Camera Motion is enabled."""
|
||
|
return self._camera.motion_enabled
|