-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
executable file
·222 lines (185 loc) · 7.59 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
"""The Daikin Altherma integration."""
from __future__ import annotations
from datetime import timedelta
import logging
from aiohttp import ClientConnectionError
from pyaltherma.comm import DaikinWSConnection
from pyaltherma.controllers import AlthermaController
from homeassistant.components.water_heater import STATE_OFF, STATE_ON, STATE_PERFORMANCE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.util import Throttle
from .const import DOMAIN
PLATFORMS = ["water_heater", "sensor", "switch", "select", "number"]
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
_LOGGER = logging.getLogger(__name__)
async def setup_api_instance(hass, host):
session = hass.helpers.aiohttp_client.async_get_clientsession()
conn = DaikinWSConnection(session, host)
device = AlthermaController(conn)
await device.discover_units()
api = AlthermaAPI(device)
await api.api_init()
return api
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Daikin Altherma from a config entry."""
conf = entry.data
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = api = await setup_api_instance(
hass, conf[CONF_HOST]
)
hass.data[DOMAIN][entry.entry_id] = api
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class AlthermaAPI:
def __init__(self, device: AlthermaController) -> None:
"""Initialize the Daikin Handle."""
self._device = device
self.host = device.ws_connection.host
self._status = None
self._info = None
self._available = True
self._hwt_device_info = None
self._space_heating_device_info = None
self._climate_control_powered = False
async def turn_on_climate_control(self):
await self._device.climate_control.turn_on()
self._climate_control_powered = True
async def turn_off_climate_control(self):
await self._device.climate_control.turn_off()
self._climate_control_powered = False
async def async_is_climate_control_on(self):
self._climate_control_powered = await self._device.climate_control.is_turned_on
return self.is_climate_control_on()
def is_climate_control_on(self):
return self._climate_control_powered
@property
def status(self):
return self._status
@property
def info(self):
return self._info
@property
def water_tank_status(self):
return self._status["function/DomesticHotWaterTank"]
@property
def space_heating_status(self):
return self._status["function/SpaceHeating"]
@property
def device(self) -> AlthermaController:
return self._device
async def api_init(self):
self._status = await self.device.get_current_state()
self._info = await self.device.device_info()
self._climate_control_powered = await self._device.climate_control.is_turned_on
await self.get_HWT_device_info()
await self.get_space_heating_device_info()
await self._device.ws_connection.close()
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self, **kwargs):
"""Pull the latest data from Daikin."""
try:
self._status = await self.device.get_current_state()
self._climate_control_powered = await self._device.climate_control.is_turned_on
await self.device.ws_connection.close()
self._available = True
except ClientConnectionError:
_LOGGER.warning("Connection failed for %s", self.ip_address)
self._available = False
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
@property
def device_info(self) -> DeviceInfo:
return DeviceInfo(
**{
"identifiers": {(DOMAIN, self._info["serial_number"])},
"name": self._info["duty"],
"manufacturer": self._info["manufacturer"],
"model": self._info["model_name"],
"sw_version": self._info["firmware"],
}
)
async def get_HWT_device_info(self) -> DeviceInfo:
if self._hwt_device_info is None:
hwt = self.device.hot_water_tank
self._hwt_device_info = DeviceInfo(
**{
"identifiers": {(DOMAIN, f"{self._info['serial_number']} Hot Water Tank")},
# "name": self._info["duty"],
"name": "Hot Water Tank",
"manufacturer": self._info["manufacturer"],
"model": await hwt.model_number,
"sw_version": f"{await hwt.indoor_software}/{await hwt.outdoor_software}",
}
)
return self._hwt_device_info
async def get_space_heating_device_info(self) -> DeviceInfo:
if self._space_heating_device_info is None:
space_heating = self.device.climate_control
self._space_heating_device_info = DeviceInfo(
**{
"identifiers": {(DOMAIN, f"{self._info['serial_number']} Space Heating")},
# "name": self._info["duty"],
"name": "Space Heating",
"manufacturer": self._info["manufacturer"],
"model": await space_heating.model_number,
"sw_version": f"{await space_heating.indoor_software}/{await space_heating.outdoor_software}",
}
)
return self._space_heating_device_info
@property
def HWT_device_info(self):
return self._hwt_device_info
@property
def space_heating_device_info(self):
return self._space_heating_device_info
@property
def water_tank_operation(self):
status = self.water_tank_status
ops = status["operations"]
is_on = ops["Power"] == "on"
# First check if it is on and if yes then check whatever it is in powerful mode
if is_on:
state = ops["powerful"]
if state == 0:
return STATE_ON
else:
return STATE_PERFORMANCE
else:
return STATE_OFF
async def async_set_water_tank_state(self, state):
"""
Sets new hot water tank state. It can be off / on / powerful
@param state: string
@return: Nothing
"""
if state == STATE_OFF:
await self.device.hot_water_tank.set_powerful(False)
await self.device.hot_water_tank.turn_off()
elif state == STATE_ON:
await self.device.hot_water_tank.turn_on()
await self.device.hot_water_tank.set_powerful(False)
else:
await self.device.hot_water_tank.turn_on()
await self.device.hot_water_tank.set_powerful(True)
await self.device.ws_connection.close()
@property
def water_tank_target_temp_config(self) -> dict:
"""
Returns the configuration values for target temperature.
Normally it should have the maximum, minimum and step numbers
@rtype: dict
"""
return self.device.hot_water_tank._unit.operation_config["TargetTemperature"][
"heating"
]