forked from filipvh/hass-nhc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
115 lines (92 loc) · 3.53 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
"""Support for Niko Home Control II - CoCo."""
import logging
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_USERNAME, \
CONF_PASSWORD, CONF_ADDRESS
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from .config_flow import Nhc2FlowHandler # noqa pylint_disable=unused-import
from .const import DOMAIN, KEY_GATEWAY, CONF_SWITCHES_AS_LIGHTS
from .helpers import extract_versions
REQUIREMENTS = ['nhc2-coco==0.1.4']
_LOGGER = logging.getLogger(__name__)
DOMAIN = DOMAIN
KEY_GATEWAY = KEY_GATEWAY
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_ADDRESS): cv.string,
vol.Optional(CONF_SWITCHES_AS_LIGHTS, default=False): bool
})
}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass, config):
"""Set up the NHC2 CoCo component."""
conf = config.get(DOMAIN)
if conf is None:
return True
host = conf.get(CONF_HOST)
username = conf.get(CONF_USERNAME)
password = conf.get(CONF_PASSWORD)
address = conf.get(CONF_ADDRESS)
switches_as_lights = conf.get(CONF_SWITCHES_AS_LIGHTS)
hass.async_create_task(hass.config_entries.flow.async_init(
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
data={
CONF_HOST: host,
CONF_USERNAME: username,
CONF_PASSWORD: password,
CONF_ADDRESS: address,
CONF_SWITCHES_AS_LIGHTS: switches_as_lights
}
))
return True
async def async_setup_entry(hass, entry):
"""Create a NHC2 gateway."""
from nhc2_coco import CoCo
coco = CoCo(
entry.data[CONF_HOST],
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
switches_as_lights=entry.data[CONF_SWITCHES_AS_LIGHTS]
)
async def on_hass_stop(event):
"""Close connection when hass stops."""
coco.disconnect()
def get_process_sysinfo(dev_reg):
def process_sysinfo(nhc2_sysinfo):
coco_image, nhc_version = extract_versions(nhc2_sysinfo)
_LOGGER.debug('Sysinfo: NhcVersion %s - CocoImage %s',
nhc_version,
coco_image)
dev_reg.async_get_or_create(
config_entry_id=entry.entry_id,
connections=set(),
identifiers={
(DOMAIN, entry.data[CONF_USERNAME])
},
manufacturer='Niko',
name='Home Control II',
model='Connected controller',
sw_version=nhc_version + ' - CoCo Image: ' + coco_image,
)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'light')
)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'switch')
)
return process_sysinfo
hass.data.setdefault(KEY_GATEWAY, {})[entry.entry_id] = coco
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)
_LOGGER.debug('Connecting to %s with %s',
entry.data[CONF_HOST], entry.data[CONF_USERNAME]
)
coco.connect()
dev_reg = await hass.helpers.device_registry.async_get_registry()
coco.get_systeminfo(get_process_sysinfo(dev_reg))
return True