From 2b173673134b8fca7c64da2c5b8face5a59b0b49 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 6 Aug 2024 20:36:14 +0200 Subject: [PATCH] Finished matrix clock client --- src/mash/entities/matrixclock.py | 44 ++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/mash/entities/matrixclock.py b/src/mash/entities/matrixclock.py index 8b73b7f..46fbce4 100644 --- a/src/mash/entities/matrixclock.py +++ b/src/mash/entities/matrixclock.py @@ -1,8 +1,48 @@ from mash.entities.entity import Entity +import requests as r class MatrixClockEntity(Entity): - def __init__(self, *, id: str, name: str, room: str, device_type: str, groups: list[str] = ...) -> None: - super().__init__(id=id, name=name, room=room, device_type=device_type, groups=groups) + def __init__(self, *, id: str, name: str, room: str, ip: str, port: int) -> None: + super().__init__(id=id, name=name, room=room, device_type="matrixclock") + self._ip = ip + self._port = port + self._base_path = "" + def __post_request__(self, endpoint: str, content: dict = {}) -> r.Response: + url: str = f"http://{self._ip}:{self._port}{self._base_path}/{endpoint}" + return r.post(url=url, json=content) + + def display_off(self): + self.__post_request__("off") + + def display_time(self): + self.__post_request__("time") + + def display_full(self): + self.__post_request__("full") + + def display_pattern(self, *, pattern: str = "01", step_ms: int = 500): + self.__post_request__(f"pattern?pattern={pattern}&step_ms={step_ms}") + + def get_climate_and_show_temperature(self) -> dict[str, float]: + return self.__post_request__("temperature").json() + + def get_climate_and_show_humidity(self) -> dict[str, float]: + return self.__post_request__("humidity").json() + + def flash_display(self, *, count: int = 1, contrast: int = None): + path = f"flash?count={count}" + if contrast: + path += "&contrast={contrast}" + self.__post_request__(path) + + def set_contrast(self, *, contrast: int): + self.__post_request__(f"contrast?contrast={contrast}") + + def get_contrast(self) -> int: + return self.__post_request__("contrast").json()["contrast"] + + def show_message(self, *, message: str): + self.__post_request__("message", {"message": message})