Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for ZoneMinder switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 from zoneminder.monitor import Monitor, MonitorState
10 from zoneminder.zm import ZoneMinder
11 
13  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
14  SwitchEntity,
15 )
16 from homeassistant.const import CONF_COMMAND_OFF, CONF_COMMAND_ON
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import PlatformNotReady
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 from . import DOMAIN as ZONEMINDER_DOMAIN
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
28  {
29  vol.Required(CONF_COMMAND_ON): cv.string,
30  vol.Required(CONF_COMMAND_OFF): cv.string,
31  }
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  add_entities: AddEntitiesCallback,
39  discovery_info: DiscoveryInfoType | None = None,
40 ) -> None:
41  """Set up the ZoneMinder switch platform."""
42 
43  on_state = MonitorState(config.get(CONF_COMMAND_ON))
44  off_state = MonitorState(config.get(CONF_COMMAND_OFF))
45 
46  switches: list[ZMSwitchMonitors] = []
47  zm_client: ZoneMinder
48  for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
49  if not (monitors := zm_client.get_monitors()):
50  raise PlatformNotReady(
51  "Switch could not fetch any monitors from ZoneMinder"
52  )
53  switches.extend(
54  ZMSwitchMonitors(monitor, on_state, off_state) for monitor in monitors
55  )
56  add_entities(switches)
57 
58 
60  """Representation of a ZoneMinder switch."""
61 
62  icon = "mdi:record-rec"
63 
64  def __init__(self, monitor: Monitor, on_state: str, off_state: str) -> None:
65  """Initialize the switch."""
66  self._monitor_monitor = monitor
67  self._on_state_on_state = on_state
68  self._off_state_off_state = off_state
69  self._state_state: bool | None = None
70  self._attr_name_attr_name = f"{monitor.name} State"
71 
72  def update(self) -> None:
73  """Update the switch value."""
74  self._state_state = self._monitor_monitor.function == self._on_state_on_state
75 
76  @property
77  def is_on(self) -> bool | None:
78  """Return True if entity is on."""
79  return self._state_state
80 
81  def turn_on(self, **kwargs: Any) -> None:
82  """Turn the entity on."""
83  self._monitor_monitor.function = self._on_state_on_state
84 
85  def turn_off(self, **kwargs: Any) -> None:
86  """Turn the entity off."""
87  self._monitor_monitor.function = self._off_state_off_state
None __init__(self, Monitor monitor, str on_state, str off_state)
Definition: switch.py:64
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:40