2022-01-01 21:23:10 +00:00
|
|
|
"""This component provides Switches for UniFi Protect."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import logging
|
2022-04-26 17:15:47 +00:00
|
|
|
from typing import Any
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
from pyunifiprotect.data import Camera, RecordingMode, VideoMode
|
|
|
|
from pyunifiprotect.data.base import ProtectAdoptableDeviceModel
|
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .data import ProtectData
|
|
|
|
from .entity import ProtectDeviceEntity, async_all_device_entities
|
2022-01-17 20:51:55 +00:00
|
|
|
from .models import ProtectSetableKeysMixin, T
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2022-01-17 20:51:55 +00:00
|
|
|
class ProtectSwitchEntityDescription(
|
2022-04-26 17:15:47 +00:00
|
|
|
ProtectSetableKeysMixin[T], SwitchEntityDescription
|
2022-01-17 20:51:55 +00:00
|
|
|
):
|
2022-01-01 21:23:10 +00:00
|
|
|
"""Describes UniFi Protect Switch entity."""
|
|
|
|
|
|
|
|
|
|
|
|
_KEY_PRIVACY_MODE = "privacy_mode"
|
2022-01-10 04:37:24 +00:00
|
|
|
|
|
|
|
|
2022-01-17 20:51:55 +00:00
|
|
|
def _get_is_highfps(obj: Camera) -> bool:
|
2022-01-10 04:37:24 +00:00
|
|
|
return bool(obj.video_mode == VideoMode.HIGH_FPS)
|
|
|
|
|
|
|
|
|
2022-01-17 20:51:55 +00:00
|
|
|
async def _set_highfps(obj: Camera, value: bool) -> None:
|
2022-01-10 04:37:24 +00:00
|
|
|
if value:
|
|
|
|
await obj.set_video_mode(VideoMode.HIGH_FPS)
|
|
|
|
else:
|
|
|
|
await obj.set_video_mode(VideoMode.DEFAULT)
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
ALL_DEVICES_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = (
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="ssh",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="SSH Enabled",
|
|
|
|
icon="mdi:lock",
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="is_ssh_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_ssh",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
CAMERA_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = (
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="status_light",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Status Light On",
|
|
|
|
icon="mdi:led-on",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_required_field="feature_flags.has_led_status",
|
|
|
|
ufp_value="led_settings.is_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_status_light",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="hdr_mode",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="HDR Mode",
|
|
|
|
icon="mdi:brightness-7",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_required_field="feature_flags.has_hdr",
|
|
|
|
ufp_value="hdr_mode",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_hdr",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
2022-01-17 20:51:55 +00:00
|
|
|
ProtectSwitchEntityDescription[Camera](
|
2022-01-10 04:37:24 +00:00
|
|
|
key="high_fps",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="High FPS",
|
|
|
|
icon="mdi:video-high-definition",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_required_field="feature_flags.has_highfps",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_value_fn=_get_is_highfps,
|
|
|
|
ufp_set_method_fn=_set_highfps,
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key=_KEY_PRIVACY_MODE,
|
|
|
|
name="Privacy Mode",
|
|
|
|
icon="mdi:eye-settings",
|
2022-03-31 09:06:07 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
2022-01-01 21:23:10 +00:00
|
|
|
ufp_required_field="feature_flags.has_privacy_mask",
|
|
|
|
ufp_value="is_privacy_on",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="system_sounds",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="System Sounds",
|
|
|
|
icon="mdi:speaker",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_required_field="feature_flags.has_speaker",
|
|
|
|
ufp_value="speaker_settings.are_system_sounds_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_system_sounds",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="osd_name",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Overlay: Show Name",
|
|
|
|
icon="mdi:fullscreen",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="osd_settings.is_name_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_osd_name",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="osd_date",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Overlay: Show Date",
|
|
|
|
icon="mdi:fullscreen",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="osd_settings.is_date_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_osd_date",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="osd_logo",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Overlay: Show Logo",
|
|
|
|
icon="mdi:fullscreen",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="osd_settings.is_logo_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_osd_logo",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="osd_bitrate",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Overlay: Show Bitrate",
|
|
|
|
icon="mdi:fullscreen",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="osd_settings.is_debug_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_osd_bitrate",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="smart_person",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Detections: Person",
|
|
|
|
icon="mdi:walk",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
2022-04-09 21:34:48 +00:00
|
|
|
ufp_required_field="can_detect_person",
|
2022-01-01 21:23:10 +00:00
|
|
|
ufp_value="is_person_detection_on",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_person_detection",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="smart_vehicle",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Detections: Vehicle",
|
|
|
|
icon="mdi:car",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
2022-04-09 21:34:48 +00:00
|
|
|
ufp_required_field="can_detect_vehicle",
|
2022-01-01 21:23:10 +00:00
|
|
|
ufp_value="is_vehicle_detection_on",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_vehicle_detection",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
2022-04-09 21:34:48 +00:00
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="smart_face",
|
|
|
|
name="Detections: Face",
|
|
|
|
icon="mdi:human-greeting",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_required_field="can_detect_face",
|
|
|
|
ufp_value="is_face_detection_on",
|
|
|
|
ufp_set_method="set_face_detection",
|
|
|
|
),
|
2022-01-01 21:23:10 +00:00
|
|
|
)
|
|
|
|
|
2022-01-13 03:54:22 +00:00
|
|
|
SENSE_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = (
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="status_light",
|
|
|
|
name="Status Light On",
|
|
|
|
icon="mdi:led-on",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="led_settings.is_enabled",
|
|
|
|
ufp_set_method="set_status_light",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="motion",
|
|
|
|
name="Motion Detection",
|
|
|
|
icon="mdi:walk",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="motion_settings.is_enabled",
|
|
|
|
ufp_set_method="set_motion_status",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="temperature",
|
|
|
|
name="Temperature Sensor",
|
|
|
|
icon="mdi:thermometer",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="temperature_settings.is_enabled",
|
|
|
|
ufp_set_method="set_temperature_status",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="humidity",
|
|
|
|
name="Humidity Sensor",
|
|
|
|
icon="mdi:water-percent",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="humidity_settings.is_enabled",
|
|
|
|
ufp_set_method="set_humidity_status",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="light",
|
|
|
|
name="Light Sensor",
|
|
|
|
icon="mdi:brightness-5",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="light_settings.is_enabled",
|
|
|
|
ufp_set_method="set_light_status",
|
|
|
|
),
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="alarm",
|
|
|
|
name="Alarm Sound Detection",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="alarm_settings.is_enabled",
|
|
|
|
ufp_set_method="set_alarm_status",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
LIGHT_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = (
|
|
|
|
ProtectSwitchEntityDescription(
|
2022-01-10 04:37:24 +00:00
|
|
|
key="status_light",
|
2022-01-01 21:23:10 +00:00
|
|
|
name="Status Light On",
|
|
|
|
icon="mdi:led-on",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="light_device_settings.is_indicator_enabled",
|
2022-01-10 04:37:24 +00:00
|
|
|
ufp_set_method="set_status_light",
|
2022-01-01 21:23:10 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-25 00:22:52 +00:00
|
|
|
DOORLOCK_SWITCHES: tuple[ProtectSwitchEntityDescription, ...] = (
|
|
|
|
ProtectSwitchEntityDescription(
|
|
|
|
key="status_light",
|
|
|
|
name="Status Light On",
|
|
|
|
icon="mdi:led-on",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
ufp_value="led_settings.is_enabled",
|
|
|
|
ufp_set_method="set_status_light",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up sensors for UniFi Protect integration."""
|
|
|
|
data: ProtectData = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
entities: list[ProtectDeviceEntity] = async_all_device_entities(
|
|
|
|
data,
|
|
|
|
ProtectSwitch,
|
|
|
|
all_descs=ALL_DEVICES_SWITCHES,
|
|
|
|
camera_descs=CAMERA_SWITCHES,
|
|
|
|
light_descs=LIGHT_SWITCHES,
|
2022-01-13 03:54:22 +00:00
|
|
|
sense_descs=SENSE_SWITCHES,
|
2022-01-25 00:22:52 +00:00
|
|
|
lock_descs=DOORLOCK_SWITCHES,
|
2022-01-01 21:23:10 +00:00
|
|
|
)
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class ProtectSwitch(ProtectDeviceEntity, SwitchEntity):
|
|
|
|
"""A UniFi Protect Switch."""
|
|
|
|
|
2022-01-10 04:37:24 +00:00
|
|
|
entity_description: ProtectSwitchEntityDescription
|
|
|
|
|
2022-01-01 21:23:10 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: ProtectData,
|
|
|
|
device: ProtectAdoptableDeviceModel,
|
|
|
|
description: ProtectSwitchEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize an UniFi Protect Switch."""
|
2022-01-10 04:37:24 +00:00
|
|
|
super().__init__(data, device, description)
|
2022-01-01 21:23:10 +00:00
|
|
|
self._attr_name = f"{self.device.name} {self.entity_description.name}"
|
|
|
|
self._switch_type = self.entity_description.key
|
|
|
|
|
|
|
|
if not isinstance(self.device, Camera):
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.entity_description.key == _KEY_PRIVACY_MODE:
|
|
|
|
if self.device.is_privacy_on:
|
|
|
|
self._previous_mic_level = 100
|
|
|
|
self._previous_record_mode = RecordingMode.ALWAYS
|
|
|
|
else:
|
|
|
|
self._previous_mic_level = self.device.mic_volume
|
|
|
|
self._previous_record_mode = self.device.recording_settings.mode
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if device is on."""
|
2022-01-10 04:37:24 +00:00
|
|
|
return self.entity_description.get_ufp_value(self.device) is True
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the device on."""
|
|
|
|
if self._switch_type == _KEY_PRIVACY_MODE:
|
2022-01-10 04:37:24 +00:00
|
|
|
assert isinstance(self.device, Camera)
|
2022-01-01 21:23:10 +00:00
|
|
|
self._previous_mic_level = self.device.mic_volume
|
|
|
|
self._previous_record_mode = self.device.recording_settings.mode
|
|
|
|
await self.device.set_privacy(True, 0, RecordingMode.NEVER)
|
2022-01-10 04:37:24 +00:00
|
|
|
else:
|
|
|
|
await self.entity_description.ufp_set(self.device, True)
|
2022-01-01 21:23:10 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the device off."""
|
|
|
|
|
2022-01-10 04:37:24 +00:00
|
|
|
if self._switch_type == _KEY_PRIVACY_MODE:
|
|
|
|
assert isinstance(self.device, Camera)
|
|
|
|
_LOGGER.debug("Setting Privacy Mode to false for %s", self.device.name)
|
2022-01-01 21:23:10 +00:00
|
|
|
await self.device.set_privacy(
|
|
|
|
False, self._previous_mic_level, self._previous_record_mode
|
|
|
|
)
|
2022-01-10 04:37:24 +00:00
|
|
|
else:
|
|
|
|
await self.entity_description.ufp_set(self.device, False)
|