-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
126 lines (96 loc) · 3.29 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
"""Support for controlling an Autelis pool controller (from www.autelis.com which no longer exists) to control one of several different types of pool control systems"""
import asyncio
from datetime import timedelta
import logging
import time
from homeassistant.const import (
CONF_PASSWORD,
CONF_HOST,
CONF_NAME,
STATE_ON,
STATE_OFF
)
from homeassistant.util import Throttle
from .const import (
DOMAIN,
AUTELIS_PLATFORMS,
TEMP_SENSORS,
STATE_AUTO,
STATE_SERVICE
)
from .api import AutelisPoolAPI
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Pool Control"
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
def setup(hass, config):
"""Set up the Autelis component."""
return True
async def async_setup_entry(hass, entry):
"""Set up autelis via a config entry."""
host = entry.data[CONF_HOST]
password = entry.data[CONF_PASSWORD]
data = AutelisData(hass, entry, host=host, password=password)
await data.update()
if data.sensors is None:
_LOGGER.error("Unable to connect to Autelis device")
return False
hass.data[DOMAIN] = data
for component in AUTELIS_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)
return True
async def async_unload_entry(hass, entry):
"""Unload the config entry and platforms."""
hass.data.pop(DOMAIN)
tasks = []
for platform in AUTELIS_PLATFORMS:
tasks.append(
hass.config_entries.async_forward_entry_unload(entry, platform)
)
return all(await asyncio.gather(*tasks))
class AutelisData:
"""
Handle getting the latest data from autelis so platforms can use it.
"""
def __init__(self, hass, entry, host, password):
"""Initialize the Autelis data object."""
self._hass = hass
self._entry = entry
self.host = host
self.password = password
self.api = AutelisPoolAPI(hass, f"http://{host}/", password)
self.sensors = { }
self.equipment = { }
self.mode = ""
self.names = { }
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update(self):
"""Get the latest data from autelis controller"""
if self.names is None or len(self.names) == 0:
self.names = await self.api.get_names()
_LOGGER.debug("Updating autelis")
status = await self.api.get("status.xml")
temps = status.find("temp")
if temps is not None:
for child in temps:
value = child.text
self.sensors[child.tag] = value
else:
_LOGGER.error("temps is None")
equip = status.find("equipment")
if equip is not None:
for child in equip:
value = child.text
self.equipment[child.tag] = value
else:
_LOGGER.error("equip is None")
opmode = status.find(".//opmode")
# freeze = status.find("freeze")
if opmode is not None:
self.mode = STATE_AUTO if opmode.text == "0" else STATE_SERVICE
else:
self.mode = STATE_SERVICE
_LOGGER.error("opmode is None")
# self.freeze = STATE_ON if freeze && freeze.text == "1" else STATE_OFF
return