forked from filipvh/hass-nhc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.py
111 lines (89 loc) · 3.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
"""Support for NHC2 lights."""
import logging
from typing import List
from homeassistant.components.light import Light
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(Light):
"""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
nhc2light.on_change = self._on_change
def _on_change(self):
self._is_on = self._nhc2light.is_on
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()
if self._optimistic:
self._is_on = True
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 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),
}