Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Android IP Webcam settings."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Coroutine
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from pydroid_ipcam import PyDroidIPCam
10 
11 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .coordinator import AndroidIPCamConfigEntry, AndroidIPCamDataUpdateCoordinator
17 from .entity import AndroidIPCamBaseEntity
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Entity description class for Android IP Webcam switches."""
23 
24  on_func: Callable[[PyDroidIPCam], Coroutine[Any, Any, bool]]
25  off_func: Callable[[PyDroidIPCam], Coroutine[Any, Any, bool]]
26 
27 
28 SWITCH_TYPES: tuple[AndroidIPWebcamSwitchEntityDescription, ...] = (
30  key="exposure_lock",
31  translation_key="exposure_lock",
32  name="Exposure lock",
33  entity_category=EntityCategory.CONFIG,
34  on_func=lambda ipcam: ipcam.change_setting("exposure_lock", True),
35  off_func=lambda ipcam: ipcam.change_setting("exposure_lock", False),
36  ),
38  key="ffc",
39  translation_key="ffc",
40  name="Front-facing camera",
41  entity_category=EntityCategory.CONFIG,
42  on_func=lambda ipcam: ipcam.change_setting("ffc", True),
43  off_func=lambda ipcam: ipcam.change_setting("ffc", False),
44  ),
46  key="focus",
47  translation_key="focus",
48  name="Focus",
49  entity_category=EntityCategory.CONFIG,
50  on_func=lambda ipcam: ipcam.focus(activate=True),
51  off_func=lambda ipcam: ipcam.focus(activate=False),
52  ),
54  key="gps_active",
55  translation_key="gps_active",
56  name="GPS active",
57  entity_category=EntityCategory.CONFIG,
58  on_func=lambda ipcam: ipcam.change_setting("gps_active", True),
59  off_func=lambda ipcam: ipcam.change_setting("gps_active", False),
60  ),
62  key="motion_detect",
63  translation_key="motion_detect",
64  name="Motion detection",
65  entity_category=EntityCategory.CONFIG,
66  on_func=lambda ipcam: ipcam.change_setting("motion_detect", True),
67  off_func=lambda ipcam: ipcam.change_setting("motion_detect", False),
68  ),
70  key="night_vision",
71  translation_key="night_vision",
72  name="Night vision",
73  entity_category=EntityCategory.CONFIG,
74  on_func=lambda ipcam: ipcam.change_setting("night_vision", True),
75  off_func=lambda ipcam: ipcam.change_setting("night_vision", False),
76  ),
78  key="overlay",
79  translation_key="overlay",
80  name="Overlay",
81  entity_category=EntityCategory.CONFIG,
82  on_func=lambda ipcam: ipcam.change_setting("overlay", True),
83  off_func=lambda ipcam: ipcam.change_setting("overlay", False),
84  ),
86  key="torch",
87  translation_key="torch",
88  name="Torch",
89  entity_category=EntityCategory.CONFIG,
90  on_func=lambda ipcam: ipcam.torch(activate=True),
91  off_func=lambda ipcam: ipcam.torch(activate=False),
92  ),
94  key="whitebalance_lock",
95  translation_key="whitebalance_lock",
96  name="White balance lock",
97  entity_category=EntityCategory.CONFIG,
98  on_func=lambda ipcam: ipcam.change_setting("whitebalance_lock", True),
99  off_func=lambda ipcam: ipcam.change_setting("whitebalance_lock", False),
100  ),
102  key="video_recording",
103  translation_key="video_recording",
104  name="Video recording",
105  entity_category=EntityCategory.CONFIG,
106  on_func=lambda ipcam: ipcam.record(record=True),
107  off_func=lambda ipcam: ipcam.record(record=False),
108  ),
109 )
110 
111 
113  hass: HomeAssistant,
114  config_entry: AndroidIPCamConfigEntry,
115  async_add_entities: AddEntitiesCallback,
116 ) -> None:
117  """Set up the IP Webcam switches from config entry."""
118 
119  coordinator = config_entry.runtime_data
120  switch_types = [
121  switch
122  for switch in SWITCH_TYPES
123  if switch.key in coordinator.cam.enabled_settings
124  ]
126  [
127  IPWebcamSettingSwitch(coordinator, description)
128  for description in switch_types
129  ]
130  )
131 
132 
134  """Representation of a IP Webcam setting."""
135 
136  entity_description: AndroidIPWebcamSwitchEntityDescription
137 
138  def __init__(
139  self,
140  coordinator: AndroidIPCamDataUpdateCoordinator,
141  description: AndroidIPWebcamSwitchEntityDescription,
142  ) -> None:
143  """Initialize the sensor."""
144  self.entity_descriptionentity_description = description
145  super().__init__(coordinator)
146  self._attr_unique_id_attr_unique_id = f"{coordinator.config_entry.entry_id}-{description.key}"
147 
148  @property
149  def is_on(self) -> bool:
150  """Return if settings is on or off."""
151  return bool(self.camcamcam.current_settings.get(self.entity_descriptionentity_description.key))
152 
153  async def async_turn_on(self, **kwargs: Any) -> None:
154  """Turn device on."""
155  await self.entity_descriptionentity_description.on_func(self.camcamcam)
156  await self.coordinator.async_request_refresh()
157 
158  async def async_turn_off(self, **kwargs: Any) -> None:
159  """Turn device off."""
160  await self.entity_descriptionentity_description.off_func(self.camcamcam)
161  await self.coordinator.async_request_refresh()
None __init__(self, AndroidIPCamDataUpdateCoordinator coordinator, AndroidIPWebcamSwitchEntityDescription description)
Definition: switch.py:142
None async_setup_entry(HomeAssistant hass, AndroidIPCamConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:116