-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelectriq_cdx_pw.be
147 lines (131 loc) · 4.04 KB
/
electriq_cdx_pw.be
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
# To use this code, add it to your `autoexec.be` - or upload this script to your device and add `load("/<filename>.be")`.
# Before using this code, make sure you've completed the initial Tuya setup, as shown here: https://templates.blakadder.com/electriq_CD12PW.html
import hct
var In=hct.CallbackIn
var Out=hct.CallbackOut
log("Setting up development Home Assistant controls (using hct version "+hct.VERSION+")...")
TUYA0_DELAY=1000
var tuyasend0=/->tasmota.cmd('TuyaSend0')
tasmota.add_rule('power1#state=1',tuyasend0)
tasmota.add_rule('Mqtt#Connected',tuyasend0)
preset_data=hct.MapData({'Smart':0, 'Purify':1})
def set_mode_if_power(value,fan_keyword,dry_keyword)
if !tasmota.get_power()[0]
return 'off'
end
return value==1?fan_keyword:dry_keyword
end
callbacks=[
Out(
'tuyareceived#DpType4Id2',
/value->set_mode_if_power(value,'fan','drying'),
'action'
),
Out(
'power1#state=0',
/value->'off',
'action'
),
Out(
'tuyareceived#DpType2Id4',
def (value,data)
# Bit of a hack to workaround a bug with the MCU. Whenever Purify mode is selected, the target humidity defaults to 55%.
# The next time Smart (Drying) mode is selected, the original target humidity is restored on the unit, but that change is never sent to the ESP.
# The only way to resolve this is to force a full Tuya update whenever the target humidity changes to 55.
# That update is wrapped in a delay, so that its result should not win the race condition with the original update to 55, and be overridden.
if value==55 && data.value_last!=55
hct.logger.debug('Unit set to 55% target humidity. Forcing full Tuya update (TuyaSend0)')
tasmota.set_timer(TUYA0_DELAY, tuyasend0)
end
return value
end,
'target_humidity'
),
In(
/value->hct.NoPublish(hct.tuya.send(2,4,value)),
'target_humidity'
),
Out(
'tuyareceived#DpType2Id103',
nil,
'current_temperature',
nil,
true # Dedupe, as Tuya published every 10 seconds, including duplicates.
),
Out(
'tuyareceived#DpType2Id3',
nil,
'current_humidity',
nil,
true # Dedupe, as Tuya published every 10 seconds, including duplicates.
),
Out(
'tuyareceived#DpType4Id2',
/value->set_mode_if_power(value,'fan_only','dry'),
'mode'
),
Out(
'power1#state=0',
/value->'off',
'mode'
),
In(
def (value)
if value=='off'
tasmota.set_power(0,false)
else
tasmota.set_power(0,true)
hct.tuya.send(4,2,value=='fan_only'?1:0)
end
end,
'mode'
),
Out(
'tuyareceived#DpType4Id2',
/value->preset_data.out.find(value,'Unknown'),
'preset_mode'
),
In(
/value->hct.tuya.send(4,2,preset_data.in.find(value,0)),
'preset_mode'
)
]
climate=hct.Climate(
hct.UseDeviceName,
nil,
nil,
'C',
nil,
35..80,
['dry', 'fan_only','off'],
preset_data.keys,
nil,
nil,
nil,
callbacks
)
light_indicator=hct.Light(
'Humidity Health Indicator',
nil,
'mdi:wall-sconce-flat-variant',
[
In(/value->hct.tuya.send(1,101,value)),
Out('tuyareceived#DpType1Id101'),
Out(
'tuyareceived#DpType2Id3',
def (value)
# The indicator color is not reported from the Tuya MCU, but we can infer it from the current humidity, using values from the manual.
if value>=70
return hct.Light.DataRGB(255,102,102)
elif value>=60
return hct.Light.DataRGB(255,178,102)
else
return hct.Light.DataRGB(178,255,102)
end
end,
'rgb',
nil,
true # Dedupe.
)
]
)