3 """Utilities for dealing with the smart lights."""
5 from abc import ABC, abstractmethod
13 from typing import Dict, List, Optional, Set
19 import google_assistant as goog
20 from decorator_utils import timeout, memoized
22 logger = logging.getLogger(__name__)
24 parser = config.add_commandline_args(
25 f"Light Utils ({__file__})",
26 "Args related to light utilities."
29 '--light_utils_tplink_location',
30 default='/home/scott/bin/tplink.py',
32 help='The location of the tplink.py helper',
33 type=argparse_utils.valid_filename,
36 '--light_utils_network_mac_addresses_location',
37 default='/home/scott/bin/network_mac_addresses.txt',
39 help='The location of network_mac_addresses.txt',
40 type=argparse_utils.valid_filename,
45 5.0, use_signals=False, error_message="Timed out waiting for tplink.py"
47 def tplink_light_command(command: str) -> bool:
48 result = os.system(command)
49 signal = result & 0xFF
51 logging_utils.hlog("%s died with signal %d" % (command, signal))
54 exit_value = result >> 8
56 logging_utils.hlog("%s failed, exit %d" % (command, exit_value))
62 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
63 self.name = name.strip()
64 self.mac = mac.strip()
65 self.keywords = keywords.strip()
66 self.kws = keywords.split()
68 def get_name(self) -> str:
71 def get_mac(self) -> str:
75 def turn_on(self) -> bool:
79 def turn_off(self) -> bool:
83 def is_on(self) -> bool:
87 def is_off(self) -> bool:
91 def get_dimmer_level(self) -> Optional[int]:
95 def set_dimmer_level(self, level: int) -> bool:
99 def make_color(self, color: str) -> bool:
102 def get_keywords(self) -> List[str]:
105 def has_keyword(self, keyword: str) -> bool:
112 class GoogleLight(Light):
113 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
114 super().__init__(name, mac, keywords)
116 def goog_name(self) -> str:
117 name = self.get_name()
118 return name.replace("_", " ")
121 def parse_google_response(response: goog.GoogleResponse) -> bool:
122 return response.success
124 def turn_on(self) -> bool:
125 return GoogleLight.parse_google_response(
126 goog.ask_google(f"turn {self.goog_name()} on")
129 def turn_off(self) -> bool:
130 return GoogleLight.parse_google_response(
131 goog.ask_google(f"turn {self.goog_name()} off")
134 def is_on(self) -> bool:
135 r = goog.ask_google(f"is {self.goog_name()} on?")
138 return 'is on' in r.audio_transcription
140 def is_off(self) -> bool:
141 return not self.is_on()
143 def get_dimmer_level(self) -> Optional[int]:
144 if not self.has_keyword("dimmer"):
146 r = goog.ask_google(f'how bright is {self.goog_name()}?')
150 # the bookcase one is set to 40% bright
151 txt = r.audio_transcription
152 m = re.search(r"(\d+)% bright", txt)
154 return int(m.group(1))
159 def set_dimmer_level(self, level: int) -> bool:
160 if not self.has_keyword("dimmer"):
162 if 0 <= level <= 100:
163 was_on = self.is_on()
164 r = goog.ask_google(f"set {self.goog_name()} to {level} percent")
172 def make_color(self, color: str) -> bool:
173 return GoogleLight.parse_google_response(
174 goog.ask_google(f"make {self.goog_name()} {color}")
178 class TPLinkLight(Light):
179 def __init__(self, name: str, mac: str, keywords: str = "") -> None:
180 super().__init__(name, mac, keywords)
181 self.children: List[str] = []
182 self.info: Optional[Dict] = None
183 self.info_ts: Optional[datetime.datetime] = None
184 if "children" in self.keywords:
185 self.info = self.get_info()
186 if self.info is not None:
187 for child in self.info["children"]:
188 self.children.append(child["id"])
191 def get_tplink_name(self) -> Optional[str]:
192 self.info = self.get_info()
193 if self.info is not None:
194 return self.info["alias"]
197 def get_cmdline(self, child: str = None) -> str:
199 f"{config.config['light_utils_tplink_location']} -m {self.mac} "
200 f"--no_logging_console "
202 if child is not None:
203 cmd += f"-x {child} "
206 def get_children(self) -> List[str]:
210 self, cmd: str, child: str = None, extra_args: str = None
212 cmd = self.get_cmdline(child) + f"-c {cmd}"
213 if extra_args is not None:
214 cmd += f" {extra_args}"
215 return tplink_light_command(cmd)
217 def turn_on(self, child: str = None) -> bool:
218 return self.command("on", child)
220 def turn_off(self, child: str = None) -> bool:
221 return self.command("off", child)
223 def is_on(self) -> bool:
224 return self.get_on_duration_seconds() > 0
226 def is_off(self) -> bool:
227 return not self.is_on()
229 def make_color(self, color: str) -> bool:
230 raise NotImplementedError
233 10.0, use_signals=False, error_message="Timed out waiting for tplink.py"
235 def get_info(self) -> Optional[Dict]:
236 cmd = self.get_cmdline() + "-c info"
237 out = subprocess.getoutput(cmd)
238 out = re.sub("Sent:.*\n", "", out)
239 out = re.sub("Received: *", "", out)
241 self.info = json.loads(out)["system"]["get_sysinfo"]
242 self.info_ts = datetime.datetime.now()
244 except Exception as e:
246 print(out, file=sys.stderr)
251 def get_on_duration_seconds(self, child: str = None) -> int:
252 self.info = self.get_info()
254 if self.info is None:
256 return int(self.info.get("on_time", "0"))
258 if self.info is None:
260 for chi in self.info.get("children", {}):
261 if chi["id"] == child:
262 return int(chi.get("on_time", "0"))
265 def get_on_limit_seconds(self) -> Optional[int]:
267 m = re.search(r"timeout:(\d+)", kw)
269 return int(m.group(1)) * 60
272 def get_dimmer_level(self) -> Optional[int]:
273 if not self.has_keyword("dimmer"):
275 self.info = self.get_info()
276 if self.info is None:
278 return int(self.info.get("brightness", "0"))
280 def set_dimmer_level(self, level: int) -> bool:
281 if not self.has_keyword("dimmer"):
285 + f'-j \'{{"smartlife.iot.dimmer":{{"set_brightness":{{"brightness":{level} }} }} }}\''
287 return tplink_light_command(cmd)
290 class LightingConfig(object):
291 """Representation of the smart light device config."""
295 config_file: str = None,
297 if config_file is None:
298 config_file = config.config[
299 'light_utils_network_mac_addresses_location'
301 self.macs_by_name = {}
302 self._keywords_by_name = {}
303 self.keywords_by_mac = {}
304 self.names_by_mac = {}
305 self.corpus = logical_search.Corpus()
306 with open(config_file, "r") as f:
307 contents = f.readlines()
308 for line in contents:
309 line = line.rstrip("\n")
310 line = re.sub(r"#.*$", r"", line)
314 (mac, name, keywords) = line.split(",")
317 keywords = keywords.strip()
318 if "perm" not in keywords:
320 properties = [("name", name)]
322 for kw in keywords.split():
324 key, value = kw.split(":")
325 properties.append((key, value))
328 properties.append(("name", name))
329 self.macs_by_name[name] = mac
330 self._keywords_by_name[name] = keywords
331 self.keywords_by_mac[mac] = keywords
332 self.names_by_mac[mac] = name
334 logical_search.Document(
337 properties=properties,
342 def __repr__(self) -> str:
343 s = "Known devices:\n"
344 for name, keywords in self._keywords_by_name.items():
345 mac = self.macs_by_name[name]
346 s += f" {name} ({mac}) => {keywords}\n"
349 def get_keywords_by_name(self, name: str) -> Optional[str]:
350 return self._keywords_by_name.get(name, None)
352 def get_macs_by_name(self, name: str) -> Set[str]:
354 for (mac, lname) in self.names_by_mac.items():
359 def get_macs_by_keyword(self, keyword: str) -> Set[str]:
361 for (mac, keywords) in self.keywords_by_mac.items():
362 if keyword in keywords:
366 def get_light_by_name(self, name: str) -> Optional[Light]:
367 if name in self.macs_by_name:
368 return self.get_light_by_mac(self.macs_by_name[name])
371 def get_all_lights(self) -> List[Light]:
373 for (mac, kws) in self.keywords_by_mac.items():
375 light = self.get_light_by_mac(mac)
376 if light is not None:
380 def get_light_by_mac(self, mac: str) -> Optional[Light]:
381 if mac in self.keywords_by_mac:
382 name = self.names_by_mac[mac]
383 kws = self.keywords_by_mac[mac]
384 if "tplink" in kws.lower():
385 return TPLinkLight(name, mac, kws)
387 return GoogleLight(name, mac, kws)
390 def query(self, query: str) -> List[Light]:
391 """Evaluates a lighting query expression formed of keywords to search
392 for, logical operators (and, or, not), and parenthesis.
393 Returns a list of matching lights.
396 results = self.corpus.query(query)
397 if results is not None:
400 light = self.get_light_by_mac(mac)
401 if light is not None: