Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Axis lights."""
2 
3 from dataclasses import dataclass
4 from typing import Any
5 
6 from axis.models.event import Event, EventTopic
7 
9  ATTR_BRIGHTNESS,
10  ColorMode,
11  LightEntity,
12  LightEntityDescription,
13 )
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import AxisConfigEntry
18 from .entity import TOPIC_TO_EVENT_TYPE, AxisEventDescription, AxisEventEntity
19 from .hub import AxisHub
20 
21 
22 @callback
23 def light_name_fn(hub: AxisHub, event: Event) -> str:
24  """Provide Axis light entity name."""
25  event_type = TOPIC_TO_EVENT_TYPE[event.topic_base]
26  light_id = f"led{event.id}"
27  light_type = hub.api.vapix.light_control[light_id].light_type
28  return f"{light_type} {event_type} {event.id}"
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Axis light entity description."""
34 
35 
36 ENTITY_DESCRIPTIONS = (
37  AxisLightDescription(
38  key="Light state control",
39  event_topic=EventTopic.LIGHT_STATUS,
40  name_fn=light_name_fn,
41  supported_fn=lambda hub, event: len(hub.api.vapix.light_control) > 0,
42  ),
43 )
44 
45 
47  hass: HomeAssistant,
48  config_entry: AxisConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up the Axis light platform."""
52  config_entry.runtime_data.entity_loader.register_platform(
53  async_add_entities, AxisLight, ENTITY_DESCRIPTIONS
54  )
55 
56 
58  """Representation of an Axis light."""
59 
60  entity_description: AxisLightDescription
61 
62  _attr_should_poll = True
63  _attr_color_mode = ColorMode.BRIGHTNESS
64  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
65 
66  def __init__(
67  self, hub: AxisHub, description: AxisLightDescription, event: Event
68  ) -> None:
69  """Initialize the Axis light."""
70  super().__init__(hub, description, event)
71 
72  self._attr_is_on_attr_is_on = event.is_tripped
73  self._light_id_light_id = f"led{event.id}"
74  self.current_intensitycurrent_intensity = 0
75  self.max_intensitymax_intensity = 0
76 
77  async def async_added_to_hass(self) -> None:
78  """Subscribe lights events."""
79  await super().async_added_to_hass()
80  self.current_intensitycurrent_intensity = (
81  await self.hubhub.api.vapix.light_control.get_current_intensity(self._light_id_light_id)
82  )
83  self.max_intensitymax_intensity = (
84  await self.hubhub.api.vapix.light_control.get_valid_intensity(self._light_id_light_id)
85  ).high
86 
87  @callback
88  def async_event_callback(self, event: Event) -> None:
89  """Update light state."""
90  self._attr_is_on_attr_is_on = event.is_tripped
91  self.async_write_ha_stateasync_write_ha_state()
92 
93  @property
94  def brightness(self) -> int:
95  """Return the brightness of this light between 0..255."""
96  return int((self.current_intensitycurrent_intensity / self.max_intensitymax_intensity) * 255)
97 
98  async def async_turn_on(self, **kwargs: Any) -> None:
99  """Turn on light."""
100  if not self.is_onis_on:
101  await self.hubhub.api.vapix.light_control.activate_light(self._light_id_light_id)
102 
103  if ATTR_BRIGHTNESS in kwargs:
104  intensity = int((kwargs[ATTR_BRIGHTNESS] / 255) * self.max_intensitymax_intensity)
105  await self.hubhub.api.vapix.light_control.set_manual_intensity(
106  self._light_id_light_id, intensity
107  )
108 
109  async def async_turn_off(self, **kwargs: Any) -> None:
110  """Turn off light."""
111  if self.is_onis_on:
112  await self.hubhub.api.vapix.light_control.deactivate_light(self._light_id_light_id)
113 
114  async def async_update(self) -> None:
115  """Update brightness."""
116  self.current_intensitycurrent_intensity = (
117  await self.hubhub.api.vapix.light_control.get_current_intensity(self._light_id_light_id)
118  )
None __init__(self, AxisHub hub, AxisLightDescription description, Event event)
Definition: light.py:68
None async_turn_off(self, **Any kwargs)
Definition: light.py:109
None async_event_callback(self, Event event)
Definition: light.py:88
None async_turn_on(self, **Any kwargs)
Definition: light.py:98
None async_setup_entry(HomeAssistant hass, AxisConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:50
str light_name_fn(AxisHub hub, Event event)
Definition: light.py:23