This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
climate.py
210 lines (161 loc) · 7.28 KB
/
climate.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
"""
Support for the Adax wifi heaters using API.
For more details about this platform, please refer to the documentation at
"""
import logging
import sys
import time
import requests
import json
import voluptuous as vol
from .connect import Adax
from .parameters import set_param
from homeassistant.components.climate import ClimateDevice, PLATFORM_SCHEMA
from homeassistant.components.climate.const import SUPPORT_TARGET_TEMPERATURE, HVAC_MODE_OFF, HVAC_MODE_HEAT
from homeassistant.const import (
TEMP_CELSIUS, ATTR_TEMPERATURE, PRECISION_WHOLE)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
DEFAULT_TEMP = 20
SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
ZONE_URL = 'https://heater.azurewebsites.net/sheater-client-api/rest/zones/list/' + set_param("account_id")
TEMP_URL = 'https://heater.azurewebsites.net/sheater-client-api/rest/zones/'
# ---------------------------------------------------------------
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Adax thermostat."""
_LOGGER.debug("Adding component: adax_climate ...")
params = {"signature": set_param("zone_signature"), "appVersion": set_param("appVersion"), "device": set_param("device"),
"os": set_param("os"), "timeOffset": set_param("timeOffset"), "timeZone": set_param("timeZone")}
devices_json = Adax.do_api_request(ZONE_URL, params)
_LOGGER.debug("Response: " + str(devices_json))
for zone in devices_json[1]:
z_id = int(zone["id"])
z_entity = "climate.adax_" + str(zone["id"])
z_name = str(zone["name"])
z_state = round(float(zone["currentTemperature"] + zone["temperatureCalibration"]) / 100, 2)
z_target = round(float(zone["targetTemperature"]) / 100, 2)
z_window = bool(zone["openWindow"])
z_maxtemp = round(float(zone["upperTemperatureLimit"]) / 100, 2)
z_mintemp = round(float(zone["lowerTemperatureLimit"]) / 100, 2)
add_entities([AdaxDevice(z_id, z_name, z_state, z_target, z_window, z_maxtemp, z_mintemp)])
_LOGGER.debug("adax: Zone " + str(z_id) + " added with name " + str(z_name) + "! Current temp is " + str(z_state))
class AdaxDevice(ClimateDevice):
"""Representation of a heater."""
def __init__(self, id, name, state, target, window, maxtemp, mintemp):
"""Initialize the heater."""
self._entity_id = "climate.adax_" + str(id)
self._current_temperature = state
self._name = name
self._id = id
self._state = state
self._target_temperature = int(target)
self._open_window = window
self._max_temp = maxtemp
self._min_temp = mintemp
self.update()
@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_TARGET_TEMPERATURE
@property
def entity_id(self):
"""Return the name of the device"""
return self._entity_id
@property
def name(self):
"""Return the name of the device, if any."""
return self._name
@property
def should_poll(self):
"""Polling is required."""
return True
@property
def hvac_mode(self):
if self._target_temperature == 0:
return HVAC_MODE_OFF
else:
return HVAC_MODE_HEAT
@property
def hvac_modes(self):
"""HVAC modes."""
return [HVAC_MODE_HEAT, HVAC_MODE_OFF]
def set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
if hvac_mode == HVAC_MODE_OFF:
temperature = int(0)
target_temp = int(0)
elif hvac_mode == HVAC_MODE_HEAT:
temperature = int(DEFAULT_TEMP)
target_temp = int(temperature * 100)
SET_URL = TEMP_URL + str(self._id) + "/target_temperature/" + str(set_param("account_id")) + "/" + str(target_temp)
_LOGGER.debug("API URL: " + SET_URL)
params = {"signature": set_param(temperature), "appVersion": set_param("appVersion"), "device": set_param("device"),
"os": set_param("os"), "timeOffset": set_param("timeOffset"), "timeZone": set_param("timeZone")}
_LOGGER.debug("API params: " + str(params))
response_json = Adax.do_api_request(SET_URL, params)
_LOGGER.debug(str(response_json))
self.schedule_update_ha_state(force_refresh=True)
@property
def temperature_unit(self):
"""Return the unit of measurement which this device uses."""
return TEMP_CELSIUS
@property
def min_temp(self):
"""Return the minimum temperature."""
return self._min_temp
@property
def max_temp(self):
"""Return the maximum temperature."""
return self._max_temp
@property
def current_temperature(self):
"""Return the current temperature."""
return self._state
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def target_temperature_step(self):
"""Return the supported step of target temperature."""
return PRECISION_WHOLE
def set_temperature(self, **kwargs):
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
return
self._set_temperature(temperature)
def _set_temperature(self, temperature, mode_int=None):
"""Set new target temperature, via API commands."""
target_temp = int(temperature * 100)
SET_URL = TEMP_URL + str(self._id) + "/target_temperature/" + str(set_param("account_id")) + "/" + str(target_temp)
_LOGGER.debug("API URL: " + SET_URL)
params = {"signature": set_param(temperature), "appVersion": set_param("appVersion"), "device": set_param("device"),
"os": set_param("os"), "timeOffset": set_param("timeOffset"), "timeZone": set_param("timeZone")}
_LOGGER.debug("API params: " + str(params))
response_json = Adax.do_api_request(SET_URL, params)
_LOGGER.debug(str(response_json))
self.schedule_update_ha_state(force_refresh=True)
@property
def device_state_attributes(self):
"""Return the attribute(s) of the device"""
return {
"window_open": self._open_window
}
def _get_data(self):
"""Get the data of the device."""
params = {"signature": set_param("zone_signature"), "appVersion": set_param("appVersion"), "device": set_param("device"),
"os": set_param("os"), "timeOffset": set_param("timeOffset"), "timeZone": set_param("timeZone")}
devices_json = Adax.do_api_request(ZONE_URL, params)
for zone in devices_json[1]:
self._name = str(zone["name"])
self._state = round(float(zone["currentTemperature"] + zone["temperatureCalibration"]) / 100, 2)
self._target_temperature = round(float(zone["targetTemperature"]) / 100, 2)
self._open_window = bool(zone["openWindow"])
self._max_temp = round(float(zone["upperTemperatureLimit"]) / 100, 2)
self._min_temp = round(float(zone["lowerTemperatureLimit"]) / 100, 2)
_LOGGER.debug("Updating adax: Current temp is " + str(self._state))
def update(self):
"""Get the latest data."""
self._get_data()
# return True