forked from filipvh/hass-nhc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
light.py
136 lines (110 loc) · 4.1 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
"""Support for NHC2 lights."""
import logging
from typing import List
from homeassistant.components.light import LightEntity, SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS
from .helpers import nhc2_entity_processor
from nhc2_coco import CoCoLight, CoCo
from .const import DOMAIN, KEY_GATEWAY, BRAND, LIGHT
KEY_GATEWAY = KEY_GATEWAY
KEY_ENTITY = 'nhc2_lights'
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Load NHC2 lights based on a config entry."""
hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = []
gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id]
_LOGGER.debug('Platform is starting')
gateway.get_lights(
nhc2_entity_processor(hass, config_entry, async_add_entities,
KEY_ENTITY, lambda x: NHC2HassLight(x))
)
class NHC2HassLight(LightEntity):
"""Representation of an NHC2 Light."""
def __init__(self, nhc2light: CoCoLight, optimistic=True):
"""Initialize a light."""
self._nhc2light = nhc2light
self._optimistic = optimistic
self._is_on = nhc2light.is_on
if self._nhc2light.support_brightness:
self._brightness = int((self._nhc2light.brightness + 1) * 2.54)
else:
self._brightness = None
nhc2light.on_change = self._on_change
def _on_change(self):
self._is_on = self._nhc2light.is_on
if self._nhc2light.support_brightness:
self._brightness = int((self._nhc2light.brightness + 1) * 2.54)
self.schedule_update_ha_state()
def turn_off(self, **kwargs) -> None:
"""Pass - not in use."""
pass
def turn_on(self, **kwargs) -> None:
"""Pass - not in use."""
pass
async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on."""
self._nhc2light.turn_on()
brightness = kwargs.get(ATTR_BRIGHTNESS)
if self._nhc2light.support_brightness and brightness is not None:
self._nhc2light.brightness(int((brightness/2.54)-1))
if self._optimistic:
self._is_on = True
if self._nhc2light.support_brightness and brightness is not None:
self._brightness = int((int((brightness/2.54)-1) + 1) * 2.54)
self.schedule_update_ha_state()
async def async_turn_off(self, **kwargs):
"""Instruct the light to turn off."""
self._nhc2light.turn_off()
if self._optimistic:
self._is_on = False
self.schedule_update_ha_state()
def nhc2_update(self, nhc2light: CoCoLight):
"""Update the NHC2 light with a new object."""
self._nhc2light = nhc2light
nhc2light.on_change = self._on_change
self.schedule_update_ha_state()
@property
def unique_id(self):
"""Return the lights UUID."""
return self._nhc2light.uuid
@property
def uuid(self):
"""Return the lights UUID."""
return self._nhc2light.uuid
@property
def should_poll(self):
"""Return false, since the light will push state."""
return False
@property
def name(self):
"""Return the lights name."""
return self._nhc2light.name
@property
def available(self):
"""Return true if the light is online."""
return self._nhc2light.online
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def is_on(self):
"""Return true if the light is on."""
return self._is_on
@property
def device_info(self):
"""Return the device info."""
return {
'identifiers': {
(DOMAIN, self.unique_id)
},
'name': self.name,
'manufacturer': BRAND,
'model': LIGHT,
'via_hub': (DOMAIN, self._nhc2light.profile_creation_id),
}
@property
def supported_features(self):
"""Return supported features."""
if self._nhc2light.support_brightness:
return SUPPORT_BRIGHTNESS
return 0