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