diff options
Diffstat (limited to 'light_utils.py')
| -rw-r--r-- | light_utils.py | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/light_utils.py b/light_utils.py index 8c21b86..f63ba0b 100644 --- a/light_utils.py +++ b/light_utils.py @@ -80,6 +80,18 @@ class Light(ABC): pass @abstractmethod + def is_on(self) -> bool: + pass + + @abstractmethod + def is_off(self) -> bool: + pass + + @abstractmethod + def get_dimmer_level(self) -> Optional[int]: + pass + + @abstractmethod def set_dimmer_level(self, level: int) -> bool: pass @@ -119,11 +131,42 @@ class GoogleLight(Light): goog.ask_google(f"turn {self.goog_name()} off") ) + def is_on(self) -> bool: + r = goog.ask_google(f"is {self.goog_name()} on?") + if not r.success: + return False + return 'is on' in r.audio_transcription + + def is_off(self) -> bool: + return not self.is_on() + + def get_dimmer_level(self) -> Optional[int]: + if not self.has_keyword("dimmer"): + return False + r = goog.ask_google(f'how bright is {self.goog_name()}?') + if not r.success: + return None + + # the bookcase one is set to 40% bright + txt = r.audio_transcription + m = re.search(r"(\d+)% bright", txt) + if m is not None: + return int(m.group(1)) + if "is off" in txt: + return 0 + return None + def set_dimmer_level(self, level: int) -> bool: + if not self.has_keyword("dimmer"): + return False if 0 <= level <= 100: - return GoogleLight.parse_google_response( - goog.ask_google(f"set {self.goog_name()} to {level} percent") - ) + was_on = self.is_on() + r = goog.ask_google(f"set {self.goog_name()} to {level} percent") + if not r.success: + return False + if not was_on: + self.turn_off() + return True return False def make_color(self, color: str) -> bool: @@ -177,6 +220,12 @@ class TPLinkLight(Light): def turn_off(self, child: str = None) -> bool: return self.command("off", child) + def is_on(self) -> bool: + return self.get_on_duration_seconds() > 0 + + def is_off(self) -> bool: + return not self.is_on() + def make_color(self, color: str) -> bool: raise NotImplementedError @@ -220,6 +269,14 @@ class TPLinkLight(Light): return int(m.group(1)) * 60 return None + def get_dimmer_level(self) -> Optional[int]: + if not self.has_keyword("dimmer"): + return False + self.info = self.get_info() + if self.info is None: + return None + return int(self.info.get("brightness", "0")) + def set_dimmer_level(self, level: int) -> bool: if not self.has_keyword("dimmer"): return False |
