forked from marthoc/homeseer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
266 lines (213 loc) · 7.58 KB
/
__init__.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Custom component for interacting with a HomeSeer HomeTroller or HS3 software installation.
For more details about this custom component, please refer to the documentation at
https://github.com/marthoc/homeseer
"""
import asyncio
from homeassistant import data_entry_flow
from homeassistant.config_entries import ConfigEntry, PATH_CONFIG, SOURCE_IMPORT
import voluptuous as vol
from .pyhs3 import HomeTroller, STATE_LISTENING
from .pyhs3.device import GenericEvent
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_EVENT,
CONF_HOST,
CONF_ID,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
)
from homeassistant.core import EventOrigin, HomeAssistant
from homeassistant.helpers import aiohttp_client, discovery
from homeassistant.helpers.template import Template
from .const import (
DATA_CLIENT,
DEFAULT_NAME,
_LOGGER,
CONF_ALLOW_EVENTS,
CONF_ASCII_PORT,
CONF_HTTP_PORT,
CONF_NAME_TEMPLATE,
CONF_NAMESPACE,
DEFAULT_ALLOW_EVENTS,
DEFAULT_ASCII_PORT,
DEFAULT_HTTP_PORT,
DEFAULT_PASSWORD,
DEFAULT_USERNAME,
DEFAULT_NAME_TEMPLATE,
DOMAIN,
HOMESEER_PLATFORMS,
)
REQUIREMENTS = []
async def async_setup(hass, config):
hass.data[DOMAIN] = {}
hass.data[DOMAIN][DATA_CLIENT] = {}
if DOMAIN not in config:
return True
conf = config[DOMAIN]
# Store config for use during entry setup:
hass.data[DOMAIN][PATH_CONFIG] = conf
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={
CONF_NAME: DEFAULT_NAME,
CONF_HOST: conf[CONF_HOST],
CONF_NAMESPACE: conf[CONF_NAMESPACE],
CONF_USERNAME: conf[CONF_USERNAME],
CONF_PASSWORD: conf[CONF_PASSWORD],
CONF_HTTP_PORT: conf[CONF_HTTP_PORT],
CONF_ASCII_PORT: conf[CONF_ASCII_PORT],
CONF_NAME_TEMPLATE: conf[CONF_NAME_TEMPLATE],
CONF_ALLOW_EVENTS: conf[CONF_ALLOW_EVENTS],
},
)
)
return True
async def async_migrate_to_options(
configOption, hass: HomeAssistant, config_entry: ConfigEntry
):
# migrate CONF_NAME_TEMPLATE to options for some of the config
if configOption not in config_entry.options and configOption in config_entry.data:
options = {
**config_entry.options,
configOption: config_entry.data[configOption],
}
data = config_entry.data.copy()
data.pop(configOption)
hass.config_entries.async_update_entry(config_entry, data=data, options=options)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
if not config_entry.unique_id:
hass.config_entries.async_update_entry(
config_entry, unique_id=config_entry.data[CONF_NAMESPACE]
)
# migrate CONF_NAME_TEMPLATE to options for some of the config
if (
CONF_NAME_TEMPLATE not in config_entry.options
and CONF_NAME_TEMPLATE in config_entry.data
):
options = {
**config_entry.options,
CONF_NAME_TEMPLATE: config_entry.data[CONF_NAME_TEMPLATE],
}
data = config_entry.data.copy()
data.pop(CONF_NAME_TEMPLATE)
hass.config_entries.async_update_entry(config_entry, data=data, options=options)
await async_migrate_to_options(CONF_ALLOW_EVENTS, hass, config_entry)
# Set up the HomeSeer component
# config = config.get(DOMAIN)
host = config_entry.data.get(CONF_HOST)
namespace = config_entry.data.get(CONF_NAMESPACE)
username = config_entry.data.get(CONF_USERNAME)
password = config_entry.data.get(CONF_PASSWORD)
http_port = config_entry.data.get(CONF_HTTP_PORT)
ascii_port = config_entry.data.get(CONF_ASCII_PORT)
name_template = config_entry.options.get(CONF_NAME_TEMPLATE)
name_template = Template(name_template)
name_template.hass = hass
homeseer = HSConnection(
hass, host, username, password, http_port, ascii_port, namespace, name_template
)
await homeseer.api.initialize()
if len(homeseer.devices) == 0 and len(homeseer.events) == 0:
_LOGGER.error("No supported HomeSeer devices found, aborting component setup.")
return False
await homeseer.start()
i = 0
while homeseer.api.state != STATE_LISTENING:
if i < 3:
i += 1
await asyncio.sleep(1)
elif i == 3:
_LOGGER.error(
"Failed to connect to HomeSeer ASCII server, aborting component setup."
)
await homeseer.stop()
return False
_LOGGER.info(f"Connected to HomeSeer ASCII server at {host}:{ascii_port}")
homeseer.add_remotes()
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = homeseer
for component in HOMESEER_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
)
hass.bus.async_listen_once("homeassistant_stop", homeseer.stop)
return True
class HSConnection:
"""Manages a connection between HomeSeer and Home Assistant."""
def __init__(
self,
hass,
host,
username,
password,
http_port,
ascii_port,
namespace,
name_template,
):
self._hass = hass
self._session = aiohttp_client.async_get_clientsession(self._hass)
self.api = HomeTroller(
host,
self._session,
username=username,
password=password,
http_port=http_port,
ascii_port=ascii_port,
)
self._namespace = namespace
self._name_template = name_template
self.remotes = []
@property
def devices(self):
return self.api.devices.values()
@property
def events(self):
return self.api.events
@property
def namespace(self):
return self._namespace
@property
def name_template(self):
return self._name_template
async def start(self):
await self.api.start_listener()
async def stop(self, *args):
await self.api.stop_listener()
def add_remotes(self):
for device in self.devices:
if issubclass(type(device), GenericEvent):
self.remotes.append(HSRemote(self._hass, device))
_LOGGER.info(
f"Added HomeSeer remote-type device: {device.name} (Ref: {device.ref})"
)
class HSRemote:
"""Link remote-type devices that should fire events rather than create entities to Home Assistant."""
def __init__(self, hass, device):
self._hass = hass
self._device = device
self._device.register_update_callback(
self.update_callback, suppress_on_reconnect=True
)
self._event = f"homeseer_{CONF_EVENT}"
def update_callback(self):
"""Fire the event."""
data = {CONF_ID: self._device.ref, CONF_EVENT: self._device.value}
self._hass.bus.async_fire(self._event, data, EventOrigin.remote)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
homeseer = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
await homeseer.stop()
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in HOMESEER_PLATFORMS
]
)
)
return unload_ok