X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=smart_home%2Fdevice.py;h=64302ad791b35131e597472547e81027c6ec4451;hb=532df2c5b57c7517dfb3dddd8c1358fbadf8baf3;hp=04b0bfee8abee690ab857692b15e928abf3bae03;hpb=05a26ae305adfc47f38b8534ec8f35640df3955e;p=python_utils.git diff --git a/smart_home/device.py b/smart_home/device.py index 04b0bfe..64302ad 100644 --- a/smart_home/device.py +++ b/smart_home/device.py @@ -1,5 +1,13 @@ #!/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 @@ -7,18 +15,22 @@ 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[List[str]], + 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 = keywords.split() + self.kws: List[str] = keywords.split(' ') else: self.kws = [] @@ -28,9 +40,20 @@ class Device(object): def get_mac(self) -> str: return self.mac - def get_ip(self) -> str: + 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