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
|
#!/usr/bin/env python3
# © Copyright 2021-2022, Scott Gasch
"""Most basic definition of a smart device: it must have a name and a
MAC address and may have some optional keywords. All devices have
these whether they are lights, outlets, thermostats, etc...
"""
import re
from typing import List, Optional
import arper
class Device(object):
"""Most basic definition of a smart device: it must have a name and a
MAC address and may have some optional keywords. All devices have
these whether they are lights, outlets, thermostats, etc..."""
def __init__(
self,
name: str,
mac: str,
keywords: Optional[str] = "",
):
self.name = name
self.mac = mac
self.keywords = keywords
self.arper = arper.Arper()
if keywords is not None:
self.kws: List[str] = keywords.split(' ')
else:
self.kws = []
def get_name(self) -> str:
return self.name
def get_mac(self) -> str:
return self.mac
def get_ip(self) -> Optional[str]:
return self.arper.get_ip_by_mac(self.mac)
def has_static_ip(self) -> bool:
for kw in self.kws:
m = re.search(r'static:([\d\.]+)', kw)
if m is not None:
ip = m.group(1)
assert self.get_ip() == ip
return True
return False
# Add command -> URL logic here.
def get_keywords(self) -> Optional[List[str]]:
return self.kws
def has_keyword(self, keyword: str) -> bool:
for kw in self.kws:
if kw == keyword:
return True
return False
def get_on_limit_seconds(self) -> Optional[int]:
for kw in self.kws:
m = re.search(r"timeout:(\d+)", kw)
if m is not None:
return int(m.group(1)) * 60
return None
|