forked from pdugas/recteq
-
Notifications
You must be signed in to change notification settings - Fork 2
/
climate.py
205 lines (163 loc) · 5.09 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
"""The Recteq climate component."""
import logging
from .const import (
DOMAIN,
DPS_ACTUAL,
DPS_POWER,
DPS_TARGET,
POWER_OFF,
POWER_ON,
)
from .device import RecteqDevice
from homeassistant.components import climate
from homeassistant.components.climate.const import (
ATTR_CURRENT_TEMPERATURE,
ATTR_HVAC_MODE,
ATTR_HVAC_MODES,
ATTR_MAX_TEMP,
ATTR_MIN_TEMP,
ATTR_TARGET_TEMP_STEP,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
PRECISION_WHOLE,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.core import callback
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
_LOGGER = logging.getLogger(__name__)
ICON = 'mdi:grill'
TEMP_MIN = 200
TEMP_MAX = 500
# TODO Support "Max Smoke" Mode
# The recteq app sets the target temperature to this value when it's on
# TEMP_MIN and the user taps the "-" button one more time.
TEMP_SMOKE = 180
# TODO Support "Full" Mode
# The recteq app sets the target temperature to this value when it's on
# TEMP_MAX and the user taps the "+" button one more time.
TEMP_FULL = 600
async def async_setup_entry(hass, entry, add):
add([RecteqClimate(hass.data[DOMAIN][entry.entry_id])])
class RecteqClimate(climate.ClimateEntity):
def __init__(self, device: RecteqDevice):
super().__init__()
self._device = device
@property
def name(self):
return self._device.name
@property
def unique_id(self):
return self._device.device_id
@property
def icon(self):
return ICON
@property
def available(self):
return self._device.available
@property
def precision(self):
return PRECISION_WHOLE
@property
def temperature_unit(self):
return self._device.temperature_unit
@property
def hvac_mode(self):
if self.is_on:
return HVAC_MODE_HEAT
return HVAC_MODE_OFF
@property
def hvac_modes(self):
return [HVAC_MODE_OFF, HVAC_MODE_HEAT]
@property
def current_temperature(self):
temp = self._device.dps(DPS_ACTUAL)
if temp == None:
return None
return round(float(self._device.temperature(temp)), 1)
@property
def target_temperature(self):
temp = self._device.dps(DPS_TARGET)
if temp == None:
return None
return round(float(self._device.temperature(temp)), 1)
@property
def target_temperature_step(self):
if self.temperature_unit == TEMP_FAHRENHEIT:
return 5.0
return 2.5
@property
def target_temperature_high(self):
return self.max_temp
@property
def target_temperature_low(self):
return self.min_temp
def set_temperature(self, **kwargs):
mode = kwargs.get(ATTR_HVAC_MODE)
if mode != None:
self.set_hvac_mode(mode)
temp = kwargs.get(ATTR_TEMPERATURE)
if self._device.units.temperature_unit != TEMP_FAHRENHEIT:
if self._device.force_fahrenheit:
# undo HA's conversion
temp = METRIC_SYSTEM.temperature(temp, TEMP_FAHRENHEIT)
else:
temp = IMPERIAL_SYSTEM.temperature(temp, self._device.units.temperature_unit)
self._device.dps(DPS_TARGET, int(temp+0.5))
def set_hvac_mode(self, hvac_mode):
if hvac_mode == HVAC_MODE_HEAT:
self.turn_on()
elif hvac_mode == HVAC_MODE_OFF:
self.turn_off()
else:
raise Exception('Invalid hvac_mode; "{}"'.format(hvac_mode))
@property
def is_on(self):
return self._device.is_on
@property
def is_off(self):
return self._device.is_off
def turn_on(self):
self._device.dps(DPS_POWER, POWER_ON)
def turn_off(self):
self._device.dps(DPS_POWER, POWER_OFF)
@property
def supported_features(self):
return SUPPORT_TARGET_TEMPERATURE
@property
def min_temp(self):
return round(self._device.temperature(TEMP_MIN), 1)
@property
def max_temp(self):
return round(self._device.temperature(TEMP_MAX), 1)
@property
def state_attributes(self):
data = { ATTR_TEMPERATURE: self.target_temperature }
if self.is_on:
data[ATTR_CURRENT_TEMPERATURE] = self.current_temperature
else:
data[ATTR_CURRENT_TEMPERATURE] = STATE_UNAVAILABLE
return data
@property
def capability_attributes(self):
return {
ATTR_HVAC_MODES: self.hvac_modes,
ATTR_MIN_TEMP: self.min_temp,
ATTR_MAX_TEMP: self.max_temp,
ATTR_TARGET_TEMP_STEP: self.target_temperature_step,
}
@property
def should_poll(self):
return False
async def async_update(self):
await self._device.async_request_refresh()
async def async_added_to_hass(self):
self.async_on_remove(self._device.async_add_listener(self._update_callback))
@callback
def _update_callback(self):
self.async_write_ha_state()