Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch platform for motionEye."""
2 
3 from __future__ import annotations
4 
5 from types import MappingProxyType
6 from typing import Any
7 
8 from motioneye_client.client import MotionEyeClient
9 from motioneye_client.const import (
10  KEY_MOTION_DETECTION,
11  KEY_MOVIES,
12  KEY_STILL_IMAGES,
13  KEY_TEXT_OVERLAY,
14  KEY_UPLOAD_ENABLED,
15  KEY_VIDEO_STREAMING,
16 )
17 
18 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
19 from homeassistant.config_entries import ConfigEntry
20 from homeassistant.const import EntityCategory
21 from homeassistant.core import HomeAssistant, callback
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
24 
25 from . import get_camera_from_cameras, listen_for_new_cameras
26 from .const import CONF_CLIENT, CONF_COORDINATOR, DOMAIN, TYPE_MOTIONEYE_SWITCH_BASE
27 from .entity import MotionEyeEntity
28 
29 MOTIONEYE_SWITCHES = [
31  key=KEY_MOTION_DETECTION,
32  translation_key="motion_detection",
33  entity_registry_enabled_default=True,
34  entity_category=EntityCategory.CONFIG,
35  ),
37  key=KEY_TEXT_OVERLAY,
38  translation_key="text_overlay",
39  entity_registry_enabled_default=False,
40  entity_category=EntityCategory.CONFIG,
41  ),
43  key=KEY_VIDEO_STREAMING,
44  translation_key="video_streaming",
45  entity_registry_enabled_default=False,
46  entity_category=EntityCategory.CONFIG,
47  ),
49  key=KEY_STILL_IMAGES,
50  translation_key="still_images",
51  entity_registry_enabled_default=True,
52  entity_category=EntityCategory.CONFIG,
53  ),
55  key=KEY_MOVIES,
56  translation_key="movies",
57  entity_registry_enabled_default=True,
58  entity_category=EntityCategory.CONFIG,
59  ),
61  key=KEY_UPLOAD_ENABLED,
62  translation_key="upload_enabled",
63  entity_registry_enabled_default=False,
64  entity_category=EntityCategory.CONFIG,
65  ),
66 ]
67 
68 
70  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
71 ) -> None:
72  """Set up motionEye from a config entry."""
73  entry_data = hass.data[DOMAIN][entry.entry_id]
74 
75  @callback
76  def camera_add(camera: dict[str, Any]) -> None:
77  """Add a new motionEye camera."""
79  [
81  entry.entry_id,
82  camera,
83  entry_data[CONF_CLIENT],
84  entry_data[CONF_COORDINATOR],
85  entry.options,
86  entity_description,
87  )
88  for entity_description in MOTIONEYE_SWITCHES
89  ]
90  )
91 
92  listen_for_new_cameras(hass, entry, camera_add)
93 
94 
96  """MotionEyeSwitch switch class."""
97 
98  def __init__(
99  self,
100  config_entry_id: str,
101  camera: dict[str, Any],
102  client: MotionEyeClient,
103  coordinator: DataUpdateCoordinator,
104  options: MappingProxyType[str, str],
105  entity_description: SwitchEntityDescription,
106  ) -> None:
107  """Initialize the switch."""
108  super().__init__(
109  config_entry_id,
110  f"{TYPE_MOTIONEYE_SWITCH_BASE}_{entity_description.key}",
111  camera,
112  client,
113  coordinator,
114  options,
115  entity_description,
116  )
117 
118  @property
119  def is_on(self) -> bool:
120  """Return true if the switch is on."""
121  return bool(
122  self._camera_camera and self._camera_camera.get(self.entity_descriptionentity_description.key, False)
123  )
124 
125  async def _async_send_set_camera(self, value: bool) -> None:
126  """Set a switch value."""
127 
128  # Fetch the very latest camera config to reduce the risk of updating with a
129  # stale configuration.
130  camera = await self._client_client.async_get_camera(self._camera_id_camera_id)
131  if camera:
132  camera[self.entity_descriptionentity_description.key] = value
133  await self._client_client.async_set_camera(self._camera_id_camera_id, camera)
134 
135  async def async_turn_on(self, **kwargs: Any) -> None:
136  """Turn on the switch."""
137  await self._async_send_set_camera_async_send_set_camera(True)
138 
139  async def async_turn_off(self, **kwargs: Any) -> None:
140  """Turn off the switch."""
141  await self._async_send_set_camera_async_send_set_camera(False)
142 
143  @callback
144  def _handle_coordinator_update(self) -> None:
145  """Handle updated data from the coordinator."""
146  self._camera_camera = get_camera_from_cameras(self._camera_id_camera_id, self.coordinator.data)
None __init__(self, str config_entry_id, dict[str, Any] camera, MotionEyeClient client, DataUpdateCoordinator coordinator, MappingProxyType[str, str] options, SwitchEntityDescription entity_description)
Definition: switch.py:106
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:71
None listen_for_new_cameras(HomeAssistant hass, ConfigEntry entry, Callable add_func)
Definition: __init__.py:142
dict[str, Any]|None get_camera_from_cameras(int camera_id, dict[str, Any]|None data)
Definition: __init__.py:123