2021-04-30 00:51:22 +02:00
|
|
|
"""Support for HVAC units."""
|
2021-04-05 19:02:56 +02:00
|
|
|
import typing as t
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-03-31 19:27:05 +02:00
|
|
|
from . import exceptions as e
|
2021-04-03 06:01:38 +02:00
|
|
|
from .device import Device
|
2021-04-01 20:02:35 +02:00
|
|
|
from .helpers import CRC16
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class hysen(Device):
|
2021-10-17 18:23:05 +02:00
|
|
|
"""Controls a Hysen heating thermostat.
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-10-17 18:23:05 +02:00
|
|
|
This device is manufactured by Hysen and sold under different
|
|
|
|
brands, including Floureon, Beca Energy, Beok and Decdeal.
|
|
|
|
|
|
|
|
Supported models:
|
|
|
|
- HY02B05H
|
|
|
|
- HY03WE
|
|
|
|
"""
|
|
|
|
|
|
|
|
TYPE = "HYS"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def send_request(self, request: t.Sequence[int]) -> bytes:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Send a request to the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
packet = bytearray()
|
|
|
|
packet.extend((len(request) + 2).to_bytes(2, "little"))
|
|
|
|
packet.extend(request)
|
|
|
|
packet.extend(CRC16.calculate(request).to_bytes(2, "little"))
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
response = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2021-04-05 19:02:56 +02:00
|
|
|
payload = self.decrypt(response[0x38:])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
p_len = int.from_bytes(payload[:0x02], "little")
|
|
|
|
if p_len + 2 > len(payload):
|
2020-11-05 20:00:42 +01:00
|
|
|
raise ValueError(
|
|
|
|
"hysen_response_error", "first byte of response is not length"
|
|
|
|
)
|
2021-04-05 19:02:56 +02:00
|
|
|
|
|
|
|
nom_crc = int.from_bytes(payload[p_len : p_len + 2], "little")
|
|
|
|
real_crc = CRC16.calculate(payload[0x02:p_len])
|
|
|
|
if nom_crc != real_crc:
|
|
|
|
raise ValueError("hysen_response_error", "CRC check on response failed")
|
|
|
|
|
|
|
|
return payload[0x02:p_len]
|
|
|
|
|
|
|
|
def get_temp(self) -> float:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return the room temperature in degrees celsius."""
|
2021-04-05 19:02:56 +02:00
|
|
|
payload = self.send_request([0x01, 0x03, 0x00, 0x00, 0x00, 0x08])
|
2020-09-17 05:41:32 +02:00
|
|
|
return payload[0x05] / 2.0
|
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def get_external_temp(self) -> float:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return the external temperature in degrees celsius."""
|
2021-04-05 19:02:56 +02:00
|
|
|
payload = self.send_request([0x01, 0x03, 0x00, 0x00, 0x00, 0x08])
|
2020-09-17 05:41:32 +02:00
|
|
|
return payload[18] / 2.0
|
|
|
|
|
|
|
|
def get_full_status(self) -> dict:
|
|
|
|
"""Return the state of the device.
|
|
|
|
|
|
|
|
Timer schedule included.
|
|
|
|
"""
|
2021-04-05 19:02:56 +02:00
|
|
|
payload = self.send_request([0x01, 0x03, 0x00, 0x00, 0x00, 0x16])
|
2020-09-17 05:41:32 +02:00
|
|
|
data = {}
|
2020-11-05 20:00:42 +01:00
|
|
|
data["remote_lock"] = payload[3] & 1
|
|
|
|
data["power"] = payload[4] & 1
|
|
|
|
data["active"] = (payload[4] >> 4) & 1
|
|
|
|
data["temp_manual"] = (payload[4] >> 6) & 1
|
2021-04-05 19:02:56 +02:00
|
|
|
data["room_temp"] = payload[5] / 2.0
|
|
|
|
data["thermostat_temp"] = payload[6] / 2.0
|
|
|
|
data["auto_mode"] = payload[7] & 0xF
|
|
|
|
data["loop_mode"] = payload[7] >> 4
|
2020-11-05 20:00:42 +01:00
|
|
|
data["sensor"] = payload[8]
|
|
|
|
data["osv"] = payload[9]
|
|
|
|
data["dif"] = payload[10]
|
|
|
|
data["svh"] = payload[11]
|
|
|
|
data["svl"] = payload[12]
|
2021-04-05 19:02:56 +02:00
|
|
|
data["room_temp_adj"] = (
|
|
|
|
int.from_bytes(payload[13:15], "big", signed=True) / 10.0
|
|
|
|
)
|
2020-11-05 20:00:42 +01:00
|
|
|
data["fre"] = payload[15]
|
|
|
|
data["poweron"] = payload[16]
|
|
|
|
data["unknown"] = payload[17]
|
2021-04-05 19:02:56 +02:00
|
|
|
data["external_temp"] = payload[18] / 2.0
|
2020-11-05 20:00:42 +01:00
|
|
|
data["hour"] = payload[19]
|
|
|
|
data["min"] = payload[20]
|
|
|
|
data["sec"] = payload[21]
|
|
|
|
data["dayofweek"] = payload[22]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
weekday = []
|
|
|
|
for i in range(0, 6):
|
|
|
|
weekday.append(
|
2020-11-05 20:00:42 +01:00
|
|
|
{
|
|
|
|
"start_hour": payload[2 * i + 23],
|
|
|
|
"start_minute": payload[2 * i + 24],
|
|
|
|
"temp": payload[i + 39] / 2.0,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
data["weekday"] = weekday
|
2020-09-17 05:41:32 +02:00
|
|
|
weekend = []
|
|
|
|
for i in range(6, 8):
|
|
|
|
weekend.append(
|
2020-11-05 20:00:42 +01:00
|
|
|
{
|
|
|
|
"start_hour": payload[2 * i + 23],
|
|
|
|
"start_minute": payload[2 * i + 24],
|
|
|
|
"temp": payload[i + 39] / 2.0,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
data["weekend"] = weekend
|
2020-09-17 05:41:32 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
# Change controller mode
|
|
|
|
# auto_mode = 1 for auto (scheduled/timed) mode, 0 for manual mode.
|
|
|
|
# Manual mode will activate last used temperature.
|
|
|
|
# In typical usage call set_temp to activate manual control and set temp.
|
|
|
|
# loop_mode refers to index in [ "12345,67", "123456,7", "1234567" ]
|
2021-04-30 00:51:22 +02:00
|
|
|
# E.g. loop_mode = 0 ("12345,67") means Saturday and Sunday (weekend schedule)
|
|
|
|
# loop_mode = 2 ("1234567") means every day, including Saturday and Sunday (weekday schedule)
|
2020-09-17 05:41:32 +02:00
|
|
|
# The sensor command is currently experimental
|
|
|
|
def set_mode(self, auto_mode: int, loop_mode: int, sensor: int = 0) -> None:
|
|
|
|
"""Set the mode of the device."""
|
|
|
|
mode_byte = ((loop_mode + 1) << 4) + auto_mode
|
2021-04-05 19:02:56 +02:00
|
|
|
self.send_request([0x01, 0x06, 0x00, 0x02, mode_byte, sensor])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# Advanced settings
|
|
|
|
# Sensor mode (SEN) sensor = 0 for internal sensor, 1 for external sensor,
|
|
|
|
# 2 for internal control temperature, external limit temperature. Factory default: 0.
|
|
|
|
# Set temperature range for external sensor (OSV) osv = 5..99. Factory default: 42C
|
|
|
|
# Deadzone for floor temprature (dIF) dif = 1..9. Factory default: 2C
|
|
|
|
# Upper temperature limit for internal sensor (SVH) svh = 5..99. Factory default: 35C
|
|
|
|
# Lower temperature limit for internal sensor (SVL) svl = 5..99. Factory default: 5C
|
2021-04-05 19:02:56 +02:00
|
|
|
# Actual temperature calibration (AdJ) adj = -0.5. Precision 0.1C
|
2020-09-17 05:41:32 +02:00
|
|
|
# Anti-freezing function (FrE) fre = 0 for anti-freezing function shut down,
|
|
|
|
# 1 for anti-freezing function open. Factory default: 0
|
2021-04-30 00:51:22 +02:00
|
|
|
# Power on memory (POn) poweron = 0 for off, 1 for on. Default: 0
|
2020-09-17 05:41:32 +02:00
|
|
|
def set_advanced(
|
|
|
|
self,
|
|
|
|
loop_mode: int,
|
|
|
|
sensor: int,
|
|
|
|
osv: int,
|
|
|
|
dif: int,
|
|
|
|
svh: int,
|
|
|
|
svl: int,
|
|
|
|
adj: float,
|
|
|
|
fre: int,
|
|
|
|
poweron: int,
|
|
|
|
) -> None:
|
|
|
|
"""Set advanced options."""
|
2021-04-05 19:02:56 +02:00
|
|
|
self.send_request(
|
2020-11-05 20:00:42 +01:00
|
|
|
[
|
|
|
|
0x01,
|
|
|
|
0x10,
|
|
|
|
0x00,
|
|
|
|
0x02,
|
|
|
|
0x00,
|
|
|
|
0x05,
|
|
|
|
0x0A,
|
|
|
|
loop_mode,
|
|
|
|
sensor,
|
|
|
|
osv,
|
|
|
|
dif,
|
|
|
|
svh,
|
|
|
|
svl,
|
2021-04-05 19:02:56 +02:00
|
|
|
int(adj * 10) >> 8 & 0xFF,
|
|
|
|
int(adj * 10) & 0xFF,
|
2020-11-05 20:00:42 +01:00
|
|
|
fre,
|
|
|
|
poweron,
|
|
|
|
]
|
|
|
|
)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# For backwards compatibility only. Prefer calling set_mode directly.
|
|
|
|
# Note this function invokes loop_mode=0 and sensor=0.
|
|
|
|
def switch_to_auto(self) -> None:
|
|
|
|
"""Switch mode to auto."""
|
|
|
|
self.set_mode(auto_mode=1, loop_mode=0)
|
|
|
|
|
|
|
|
def switch_to_manual(self) -> None:
|
|
|
|
"""Switch mode to manual."""
|
|
|
|
self.set_mode(auto_mode=0, loop_mode=0)
|
|
|
|
|
|
|
|
# Set temperature for manual mode (also activates manual mode if currently in automatic)
|
|
|
|
def set_temp(self, temp: float) -> None:
|
|
|
|
"""Set the target temperature."""
|
2021-04-05 19:02:56 +02:00
|
|
|
self.send_request([0x01, 0x06, 0x00, 0x01, 0x00, int(temp * 2)])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# Set device on(1) or off(0), does not deactivate Wifi connectivity.
|
|
|
|
# Remote lock disables control by buttons on thermostat.
|
|
|
|
def set_power(self, power: int = 1, remote_lock: int = 0) -> None:
|
|
|
|
"""Set the power state of the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
self.send_request([0x01, 0x06, 0x00, 0x00, remote_lock, power])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# set time on device
|
|
|
|
# n.b. day=1 is Monday, ..., day=7 is Sunday
|
|
|
|
def set_time(self, hour: int, minute: int, second: int, day: int) -> None:
|
|
|
|
"""Set the time."""
|
2020-11-05 20:00:42 +01:00
|
|
|
self.send_request(
|
2021-04-05 19:02:56 +02:00
|
|
|
[0x01, 0x10, 0x00, 0x08, 0x00, 0x02, 0x04, hour, minute, second, day]
|
2020-11-05 20:00:42 +01:00
|
|
|
)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# Set timer schedule
|
|
|
|
# Format is the same as you get from get_full_status.
|
|
|
|
# weekday is a list (ordered) of 6 dicts like:
|
|
|
|
# {'start_hour':17, 'start_minute':30, 'temp': 22 }
|
|
|
|
# Each one specifies the thermostat temp that will become effective at start_hour:start_minute
|
|
|
|
# weekend is similar but only has 2 (e.g. switch on in morning and off in afternoon)
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_schedule(self, weekday: t.List[dict], weekend: t.List[dict]) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set timer schedule."""
|
2021-04-05 19:02:56 +02:00
|
|
|
request = [0x01, 0x10, 0x00, 0x0A, 0x00, 0x0C, 0x18]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# weekday times
|
|
|
|
for i in range(0, 6):
|
2021-04-05 19:02:56 +02:00
|
|
|
request.append(weekday[i]["start_hour"])
|
|
|
|
request.append(weekday[i]["start_minute"])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# weekend times
|
|
|
|
for i in range(0, 2):
|
2021-04-05 19:02:56 +02:00
|
|
|
request.append(weekend[i]["start_hour"])
|
|
|
|
request.append(weekend[i]["start_minute"])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# weekday temperatures
|
|
|
|
for i in range(0, 6):
|
2021-04-05 19:02:56 +02:00
|
|
|
request.append(int(weekday[i]["temp"] * 2))
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
# weekend temperatures
|
|
|
|
for i in range(0, 2):
|
2021-04-05 19:02:56 +02:00
|
|
|
request.append(int(weekend[i]["temp"] * 2))
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
self.send_request(request)
|