Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Axis switches."""
2 
3 from dataclasses import dataclass
4 from typing import Any
5 
6 from axis.models.event import Event, EventTopic
7 
9  SwitchDeviceClass,
10  SwitchEntity,
11  SwitchEntityDescription,
12 )
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import AxisConfigEntry
18 from .entity import AxisEventDescription, AxisEventEntity
19 from .hub import AxisHub
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Axis switch entity description."""
25 
26 
27 ENTITY_DESCRIPTIONS = (
28  AxisSwitchDescription(
29  key="Relay state control",
30  device_class=SwitchDeviceClass.OUTLET,
31  entity_category=EntityCategory.CONFIG,
32  event_topic=EventTopic.RELAY,
33  supported_fn=lambda hub, event: isinstance(int(event.id), int),
34  name_fn=lambda hub, event: hub.api.vapix.ports[event.id].name,
35  ),
36 )
37 
38 
40  hass: HomeAssistant,
41  config_entry: AxisConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Set up the Axis switch platform."""
45  config_entry.runtime_data.entity_loader.register_platform(
46  async_add_entities, AxisSwitch, ENTITY_DESCRIPTIONS
47  )
48 
49 
51  """Representation of a Axis switch."""
52 
53  entity_description: AxisSwitchDescription
54 
55  def __init__(
56  self, hub: AxisHub, description: AxisSwitchDescription, event: Event
57  ) -> None:
58  """Initialize the Axis switch."""
59  super().__init__(hub, description, event)
60 
61  self._attr_is_on_attr_is_on = event.is_tripped
62 
63  @callback
64  def async_event_callback(self, event: Event) -> None:
65  """Update light state."""
66  self._attr_is_on_attr_is_on = event.is_tripped
67  self.async_write_ha_stateasync_write_ha_state()
68 
69  async def async_turn_on(self, **kwargs: Any) -> None:
70  """Turn on switch."""
71  await self.hubhub.api.vapix.ports.close(self._event_id_event_id)
72 
73  async def async_turn_off(self, **kwargs: Any) -> None:
74  """Turn off switch."""
75  await self.hubhub.api.vapix.ports.open(self._event_id_event_id)
None async_turn_on(self, **Any kwargs)
Definition: switch.py:69
None __init__(self, AxisHub hub, AxisSwitchDescription description, Event event)
Definition: switch.py:57
None async_event_callback(self, Event event)
Definition: switch.py:64
None async_turn_off(self, **Any kwargs)
Definition: switch.py:73
None async_setup_entry(HomeAssistant hass, AxisConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:43