Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for the Netatmo camera lights."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyatmo import modules as NaModules
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import (
17  CONF_URL_CONTROL,
18  CONF_URL_SECURITY,
19  DOMAIN,
20  EVENT_TYPE_LIGHT_MODE,
21  NETATMO_CREATE_CAMERA_LIGHT,
22  NETATMO_CREATE_LIGHT,
23  WEBHOOK_LIGHT_MODE,
24  WEBHOOK_PUSH_TYPE,
25 )
26 from .data_handler import HOME, SIGNAL_NAME, NetatmoDevice
27 from .entity import NetatmoModuleEntity
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 
33  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
34 ) -> None:
35  """Set up the Netatmo camera light platform."""
36 
37  @callback
38  def _create_camera_light_entity(netatmo_device: NetatmoDevice) -> None:
39  if not hasattr(netatmo_device.device, "floodlight"):
40  return
41 
42  entity = NetatmoCameraLight(netatmo_device)
43  async_add_entities([entity])
44 
45  entry.async_on_unload(
47  hass, NETATMO_CREATE_CAMERA_LIGHT, _create_camera_light_entity
48  )
49  )
50 
51  @callback
52  def _create_entity(netatmo_device: NetatmoDevice) -> None:
53  if not hasattr(netatmo_device.device, "brightness"):
54  return
55 
56  entity = NetatmoLight(netatmo_device)
57  _LOGGER.debug("Adding light %s", entity)
58  async_add_entities([entity])
59 
60  entry.async_on_unload(
61  async_dispatcher_connect(hass, NETATMO_CREATE_LIGHT, _create_entity)
62  )
63 
64 
66  """Representation of a Netatmo Presence camera light."""
67 
68  device: NaModules.NOC
69  _attr_is_on = False
70  _attr_name = None
71  _attr_configuration_url = CONF_URL_SECURITY
72  _attr_color_mode = ColorMode.ONOFF
73  _attr_has_entity_name = True
74  _attr_supported_color_modes = {ColorMode.ONOFF}
75 
76  def __init__(self, netatmo_device: NetatmoDevice) -> None:
77  """Initialize a Netatmo Presence camera light."""
78  super().__init__(netatmo_device)
79  self._attr_unique_id_attr_unique_id = f"{self.device.entity_id}-light"
80 
81  self._signal_name_signal_name = f"{HOME}-{self.home.entity_id}"
82  self._publishers.extend(
83  [
84  {
85  "name": HOME,
86  "home_id": self.homehome.entity_id,
87  SIGNAL_NAME: self._signal_name_signal_name,
88  },
89  ]
90  )
91 
92  async def async_added_to_hass(self) -> None:
93  """Entity created."""
94  await super().async_added_to_hass()
95 
96  self.async_on_removeasync_on_remove(
98  self.hasshass,
99  f"signal-{DOMAIN}-webhook-{EVENT_TYPE_LIGHT_MODE}",
100  self.handle_eventhandle_event,
101  )
102  )
103 
104  @callback
105  def handle_event(self, event: dict) -> None:
106  """Handle webhook events."""
107  data = event["data"]
108 
109  if not data.get("camera_id"):
110  return
111 
112  if (
113  data["home_id"] == self.homehome.entity_id
114  and data["camera_id"] == self.devicedevice.entity_id
115  and data[WEBHOOK_PUSH_TYPE] == WEBHOOK_LIGHT_MODE
116  ):
117  self._attr_is_on_attr_is_on_attr_is_on = bool(data["sub_type"] == "on")
118 
119  self.async_write_ha_stateasync_write_ha_state()
120  return
121 
122  @property
123  def available(self) -> bool:
124  """If the webhook is not established, mark as unavailable."""
125  return bool(self.data_handlerdata_handler.webhook)
126 
127  async def async_turn_on(self, **kwargs: Any) -> None:
128  """Turn camera floodlight on."""
129  _LOGGER.debug("Turn camera '%s' on", self.namename)
130  await self.devicedevice.async_floodlight_on()
131 
132  async def async_turn_off(self, **kwargs: Any) -> None:
133  """Turn camera floodlight into auto mode."""
134  _LOGGER.debug("Turn camera '%s' to auto mode", self.namename)
135  await self.devicedevice.async_floodlight_auto()
136 
137  @callback
138  def async_update_callback(self) -> None:
139  """Update the entity's state."""
140  self._attr_is_on_attr_is_on_attr_is_on = bool(self.devicedevice.floodlight == "on")
141 
142 
144  """Representation of a dimmable light by Legrand/BTicino."""
145 
146  _attr_name = None
147  _attr_configuration_url = CONF_URL_CONTROL
148  _attr_brightness: int | None = 0
149  device: NaModules.NLFN
150 
151  def __init__(self, netatmo_device: NetatmoDevice) -> None:
152  """Initialize a Netatmo light."""
153  super().__init__(netatmo_device)
154  self._attr_unique_id_attr_unique_id = f"{self.device.entity_id}-light"
155 
156  if self.devicedevice.brightness is not None:
157  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
158  else:
159  self._attr_color_mode_attr_color_mode = ColorMode.ONOFF
160  self._attr_supported_color_modes_attr_supported_color_modes = {self._attr_color_mode_attr_color_mode}
161 
162  self._signal_name_signal_name = f"{HOME}-{self.home.entity_id}"
163  self._publishers.extend(
164  [
165  {
166  "name": HOME,
167  "home_id": self.homehome.entity_id,
168  SIGNAL_NAME: self._signal_name_signal_name,
169  },
170  ]
171  )
172 
173  async def async_turn_on(self, **kwargs: Any) -> None:
174  """Turn light on."""
175  if ATTR_BRIGHTNESS in kwargs:
176  await self.devicedevice.async_set_brightness(
177  round(kwargs[ATTR_BRIGHTNESS] / 2.55)
178  )
179 
180  else:
181  await self.devicedevice.async_on()
182 
183  self._attr_is_on_attr_is_on = True
184  self.async_write_ha_stateasync_write_ha_state()
185 
186  async def async_turn_off(self, **kwargs: Any) -> None:
187  """Turn light off."""
188  await self.devicedevice.async_off()
189  self._attr_is_on_attr_is_on = False
190  self.async_write_ha_stateasync_write_ha_state()
191 
192  @callback
193  def async_update_callback(self) -> None:
194  """Update the entity's state."""
195  self._attr_is_on_attr_is_on = self.devicedevice.on is True
196 
197  if (brightness := self.devicedevice.brightness) is not None:
198  # Netatmo uses a range of [0, 100] to control brightness
199  self._attr_brightness_attr_brightness = round(brightness * 2.55)
200  else:
201  self._attr_brightness_attr_brightness = None
None __init__(self, NetatmoDevice netatmo_device)
Definition: light.py:76
None __init__(self, NetatmoDevice netatmo_device)
Definition: light.py:151
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
str|UndefinedType|None name(self)
Definition: entity.py:738
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:34
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103