-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmartir_generator.py
155 lines (113 loc) · 4.04 KB
/
smartir_generator.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
import json
import argparse
# my modules
from helpers import get_device, learn_command
def main():
# initialise some variables
suspend = False
args = parse_args()
json_config = args.json_file
# read json file to dict
with open(json_config, "r") as fp:
ac_dict = json.load(fp)
# get broadlink device
device = get_device()
# get config values
min_temp = int(ac_dict.get("minTemperature", 18))
max_temp = int(ac_dict.get("maxTemperature", 30))
temp_step = int(ac_dict.get("precision", 1))
op_modes = ac_dict.get("operationModes", ["cool", "heat"])
fan_modes = ac_dict.get("fanModes", ["auto"])
commands = ac_dict.get("commands", {})
# loop through operation modes
for op_mode in ["off"] + op_modes:
if suspend:
break
# skip over off command if already in config
if op_mode == "off" and "off" in commands:
continue
# initialise operation mode dictionary
if op_mode not in commands:
commands[op_mode] = {}
# loop through fan modes
for fan_mode in fan_modes:
if suspend:
break
# initialise fan mode dictionary
if fan_mode not in commands[op_mode]:
commands[op_mode][fan_mode] = {}
# loop through temps
temp = min_temp
while temp <= max_temp:
# skip temp if already in config
if clean_temp(temp) in commands[op_mode][fan_mode]:
temp += temp_step
continue
# label for command
if op_mode == "off":
lbl = "off"
else:
lbl = f"{op_mode}_{fan_mode}_{clean_temp(temp)}"
# get packet
pkt = learn_command(device, lbl)
# get next action
action = prompt_next_action()
# save command to json if action is continue or stop
if action in ["continue", "stop"]:
if op_mode == "off":
commands["off"] = pkt
else:
commands[op_mode][fan_mode][clean_temp(temp)] = pkt
update_json(json_config, commands)
# break out if action was stop
if action == "stop":
suspend = True
break
# increment temp
temp += temp_step
# redo command if action is redo
else:
continue
if op_mode == "off":
break
if op_mode == "off":
break
def prompt_next_action():
"""Prompt user for next action
Returns:
str: one of "redo", "stop" or "continue"
"""
sel = input(f"Press:\n[ENTER] to continue\n[R] to redo last command\n[S] to stop\n")
if sel in ["R", "r"]:
return "redo"
elif sel in ["S", "s"]:
return "stop"
else:
return "continue"
def update_json(json_config_file, commands_dict):
with open(json_config_file, "r") as fp:
config_dict = json.load(fp)
config_dict["commands"] = commands_dict
with open(json_config_file, "w") as fp:
json.dump(config_dict, fp, indent=4)
def clean_temp(temp):
if isinstance(temp, int):
return str(temp)
elif isinstance(temp, float):
if int(temp) == temp:
return str(int(temp))
else:
return str(temp)
elif isinstance(temp, str):
return temp
else:
raise ValueError(f"Temperature: {temp} not valid")
def parse_args():
# cli arguments
parser = argparse.ArgumentParser(
description="Generates json file of Climate IR commands for SmartIR Home Assistant integration"
)
parser.add_argument("json_file", metavar="JSON-FILE", help="SmartIR json file to update")
return parser.parse_args()
if __name__ == "__main__":
main()