forked from mariusz-ostoja-swierczynski/tech-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
climate.py
153 lines (129 loc) · 4.81 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
"""Support for Tech HVAC system."""
import logging
import json
from typing import List, Optional
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
HVAC_MODE_HEAT,
HVAC_MODE_COOL,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_COOL,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
SUPPORT_HVAC = [HVAC_MODE_HEAT, HVAC_MODE_OFF]
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry."""
_LOGGER.debug("Setting up entry, module udid: " + config_entry.data["udid"])
api = hass.data[DOMAIN][config_entry.entry_id]
zones = await api.get_module_zones(config_entry.data["udid"])
async_add_entities(
[
TechThermostat(
zones[zone],
api,
config_entry,
)
for zone in zones
],
True,
)
class TechThermostat(ClimateEntity):
"""Representation of a Tech climate."""
def __init__(self, device, api, config_entry):
"""Initialize the Tech device."""
_LOGGER.debug("Init TechThermostat...")
self._config_entry = config_entry
self._api = api
self._id = device["zone"]["id"]
self.update_properties(device)
def update_properties(self, device):
self._name = device["description"]["name"]
if device["zone"]["setTemperature"] is not None:
self._target_temperature = device["zone"]["setTemperature"] / 10
else:
self._target_temperature = None
if device["zone"]["currentTemperature"] is not None:
self._temperature = device["zone"]["currentTemperature"] / 10
else:
self._temperature = None
state = device["zone"]["flags"]["relayState"]
if state == "on":
self._state = CURRENT_HVAC_HEAT
elif state == "off":
self._state = CURRENT_HVAC_IDLE
else:
self._state = CURRENT_HVAC_OFF
mode = device["zone"]["zoneState"]
if mode == "zoneOn" or mode == "noAlarm":
self._mode = HVAC_MODE_HEAT
else:
self._mode = HVAC_MODE_OFF
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._id
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def supported_features(self):
"""Return the list of supported features."""
return SUPPORT_TARGET_TEMPERATURE #| SUPPORT_PRESET_MODE
@property
def hvac_mode(self):
"""Return hvac operation ie. heat, cool mode.
Need to be one of HVAC_MODE_*.
"""
return self._mode
@property
def hvac_modes(self):
"""Return the list of available hvac operation modes.
Need to be a subset of HVAC_MODES.
"""
return SUPPORT_HVAC
@property
def hvac_action(self) -> Optional[str]:
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
"""
return self._state
async def async_update(self):
"""Call by the Tech device callback to update state."""
_LOGGER.debug("Updating Tech zone: %s, udid: %s, id: %s", self._name, self._config_entry.data["udid"], self._id)
device = await self._api.get_zone(self._config_entry.data["udid"], self._id)
self.update_properties(device)
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def current_temperature(self):
"""Return the current temperature."""
return self._temperature
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
async def async_set_temperature(self, **kwargs):
"""Set new target temperatures."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature:
_LOGGER.debug("%s: Setting temperature to %s", self._name, temperature)
self._temperature = temperature
await self._api.set_const_temp(self._config_entry.data["udid"], self._id, temperature)
async def async_set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
_LOGGER.debug("%s: Setting hvac mode to %s", self._name, hvac_mode)
if hvac_mode == HVAC_MODE_OFF:
await self._api.set_zone(self._config_entry.data["udid"], self._id, False)
elif hvac_mode == HVAC_MODE_HEAT:
await self._api.set_zone(self._config_entry.data["udid"], self._id, True)