forked from marconfus/ha-nefit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnefit.py
251 lines (192 loc) · 8.24 KB
/
nefit.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
"""
Nefit thermostat.
Tested with Junkers CT100
Based on nefit-client-python
https://github.com/patvdleer/nefit-client-python
"""
REQUIREMENTS = ['nefit-client']
import logging
from datetime import timedelta
import math
#import asyncio
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.climate import ( ClimateDevice, PLATFORM_SCHEMA,
STATE_AUTO, STATE_HEAT, STATE_IDLE, STATE_COOL,
SUPPORT_TARGET_TEMPERATURE, SUPPORT_OPERATION_MODE, SUPPORT_AWAY_MODE )
from homeassistant.const import TEMP_CELSIUS, ATTR_TEMPERATURE
from homeassistant.const import STATE_UNKNOWN, EVENT_HOMEASSISTANT_STOP
from nefit import NefitClient
_LOGGER = logging.getLogger(__name__)
SUPPORT_FLAGS = (SUPPORT_TARGET_TEMPERATURE | SUPPORT_OPERATION_MODE | SUPPORT_AWAY_MODE)
CONF_NAME = "name"
CONF_SERIAL = "serial"
CONF_ACCESSKEY = "accesskey"
CONF_PASSWORD = "password"
STATE_HOTWATER = "hot water"
STATE_MANUAL = "manual"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_SERIAL): cv.string,
vol.Required(CONF_ACCESSKEY): cv.string,
vol.Required(CONF_PASSWORD): cv.string
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Nefit thermostat."""
name = config.get(CONF_NAME)
serial = config.get(CONF_SERIAL)
accesskey = config.get(CONF_ACCESSKEY)
password = config.get(CONF_PASSWORD)
add_devices([NefitThermostat(
hass, name, serial, accesskey, password)], True)
class NefitThermostat(ClimateDevice):
"""Representation of a NefitThermostat device."""
def __init__(self, hass, name, serial, accesskey, password):
"""Initialize the thermostat."""
self.hass = hass
self._name = name
self._serial = serial
self._accesskey = accesskey
self._password = password
self._unit_of_measurement = TEMP_CELSIUS
self._data = {}
self._attributes = {}
self._attributes["connection_error_count"] = 0
self._override_target_temp = False
self._new_target_temp = 0
self._away = None
_LOGGER.debug("Constructor for {} called.".format(self._name))
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
self._shutdown)
self._client = NefitClient(serial_number=self._serial,
access_key=self._accesskey,
password=self._password)
self._client.connect()
@property
def name(self):
"""Return the name of the ClimateDevice."""
return self._name
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return self._unit_of_measurement
@property
def operation_list(self):
"""List of available operation modes."""
return [STATE_AUTO, STATE_MANUAL]
def update(self):
"""Get latest data"""
_LOGGER.debug("update called.")
errors = []
try:
data = self._client.get_status()
_LOGGER.debug("update finished. result={}".format(data))
if type(data) is dict and "user mode" in data:
self._attributes["connection_state"] = "ok"
else:
self._attributes["connection_state"] = "error"
self._attributes["connection_error_count"] += self._attributes["connection_error_count"]
if data:
self._data = data
self._attributes["boiler_indicator"] = self._data.get("boiler indicator")
self._attributes["control"] = self._data.get("control")
r = self._client.get_year_total()
self._attributes["year_total"] = r.get("value")
self._attributes["year_total_unit_of_measure"] = r.get("unitOfMeasure")
r = self._client.get("/ecus/rrc/userprogram/activeprogram")
self._attributes["active_program"] = r.get("value")
r = self._client.get("/ecus/rrc/dayassunday/day10/active")
self._attributes["today_as_sunday"] = (r.get("value") == "on")
r = self._client.get("/ecus/rrc/dayassunday/day11/active")
self._attributes["tomorrow_as_sunday"] = (r.get("value") == "on")
r = self._client.get("/system/appliance/systemPressure")
self._attributes["system_pressure"] = r.get("value")
r = self._client.get("/heatingCircuits/hc1/actualSupplyTemperature")
self._attributes["supply_temp"] = r.get("value")
r = self._client.get("/system/sensors/temperatures/outdoor_t1")
self._attributes["outside_temp"] = r.get("value")
except:
_LOGGER.warning('Nefit api returned invalid data')
@property
def current_temperature(self):
"""Return the current temperature."""
_LOGGER.debug("current_temperature called.")
return self._data.get('in house temp', None)
@property
def current_operation(self):
state = self._data.get("boiler indicator")
if state == 'central heating':
return STATE_HEAT
elif state == 'hot water':
return STATE_HOTWATER
elif state == 'off':
return STATE_ECO
else:
return None
@property
def target_temperature(self):
#update happens too fast after setting new target, so value is not changed on server yet.
#assume for this first update that the set target was succesful
if self._override_target_temp:
self._target_temperature = self._new_target_temp
self._override_target_temp = False
else:
self._target_temperature = self._data.get('temp setpoint', None)
return self._target_temperature
def set_temperature(self, **kwargs):
"""Set new target temperature."""
try:
temperature = kwargs.get(ATTR_TEMPERATURE)
_LOGGER.debug("set_temperature called (temperature={}).".format(temperature))
if temperature is None:
return None
self._client.set_temperature(temperature)
self._override_target_temp = True
self._new_target_temp = temperature
except:
_LOGGER.error("Error setting target temperature")
def set_operation_mode(self, operation_mode):
"""Set new target operation mode."""
_LOGGER.debug("set_operation_mode called mode={}.".format(operation_mode))
if operation_mode == "manual":
self._client.put('/heatingCircuits/hc1/usermode', {'value': 'manual'})
else:
self._client.put('/heatingCircuits/hc1/usermode', {'value': 'clock'})
@property
def is_away_mode_on(self):
"""Return true if away mode is on."""
endpoint = 'dhwOperationClockMode' if self._data.get("user mode") == 'clock' else 'dhwOperationManualMode'
try:
r = self._client.get('/dhwCircuits/dhwA/' + endpoint)
#_LOGGER.debug("Getting hot water state: " + r.get("value"))
return r.get("value") == "off" # if hot water is off, away mode is on
except:
_LOGGER.error("Error getting away mode state")
return self._away
def turn_away_mode_on(self):
"""Turn away on."""
endpoint = 'dhwOperationClockMode' if self._data.get("user mode") == 'clock' else 'dhwOperationManualMode'
try:
self._client.put('/dhwCircuits/dhwA/' + endpoint, {"value": "off"})
self._away = True
except:
_LOGGER.error("Error setting away mode on")
def turn_away_mode_off(self):
"""Turn away off."""
endpoint = 'dhwOperationClockMode' if self._data.get("user mode") == 'clock' else 'dhwOperationManualMode'
try:
self._client.put('/dhwCircuits/dhwA/' + endpoint, {"value": "on"})
self._away = False
except:
_LOGGER.error("Error setting away mode off")
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
return self._attributes
@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_FLAGS
def _shutdown(self, event):
_LOGGER.debug("shutdown")
self._client.disconnect()