From e668fc1eaac642fd4ea713997d0b0f850ed5887f Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 22 Apr 2024 19:36:25 +0200 Subject: [PATCH] More refactoring, and example config-yaml and code snippets --- example.conf.yaml | 32 +++++++++-- src/core/module.py | 9 ---- src/example.py | 53 +++++++++++++++++++ src/hue/{hue_module.py => hue_feature.py} | 6 +-- src/main.py | 4 +- src/mash/feature.py | 9 ++++ src/{core => mash}/mash.py | 8 +-- ...lock_module.py => matrix_clock_feature.py} | 4 +- 8 files changed, 101 insertions(+), 24 deletions(-) delete mode 100644 src/core/module.py create mode 100644 src/example.py rename src/hue/{hue_module.py => hue_feature.py} (92%) create mode 100644 src/mash/feature.py rename src/{core => mash}/mash.py (75%) rename src/matrix_clock/{matrix_clock_module.py => matrix_clock_feature.py} (50%) diff --git a/example.conf.yaml b/example.conf.yaml index 444105b..c5f5413 100644 --- a/example.conf.yaml +++ b/example.conf.yaml @@ -1,7 +1,31 @@ -modules: +features: hue: - - ip: "192.168.178.23" + hue-bridge: + ip: 192.168.178.23 matrixclock: - - name: wfwfo - - ip: "192.168.178.23" + clock: + ip: 192.168.178.23 +home: + latitude: 52.51860 + longitude: 13.37565 + + rooms: + - id : &hw hallway + name: Flur + doors: + - to: ~ + - to: *bath + - to: *kit + - to: *living + - id : &bath bath + name: Badezimmer + - id : &kit kitchen + name: Küche + - id : &living living + name: Wohnzimmer + doors: + - to: *max + - id : &max max + name: Max' Zimmer + diff --git a/src/core/module.py b/src/core/module.py deleted file mode 100644 index 3120f67..0000000 --- a/src/core/module.py +++ /dev/null @@ -1,9 +0,0 @@ -from fastapi import FastAPI - - -class Module: - def __init__(self, module_id: str) -> None: - self.module_id = module_id - - def add_routes(self, server: FastAPI) -> None: - pass diff --git a/src/example.py b/src/example.py new file mode 100644 index 0000000..3eca4eb --- /dev/null +++ b/src/example.py @@ -0,0 +1,53 @@ +class Device: + def __init__(self, device_id: str, home: "Home") -> None: + self.device_id: str = device_id + self.home: Home = home + + def trigger_change(self): + self.home.trigger_device_change(self.device_id) + + +class Home: + automations: list["Automation"] + + def _get_device_behaviours_(self, device_id: str) -> list["Automation"]: + return [b for a in self.automations] + + def trigger_device_change(self, device_id: str): + pass + + +class Behaviour: + def __init__(self) -> None: + self.action: function = None + self.rule: function = None + self.devices: list[str] = [] + + +class Automation: + def __init__(self) -> None: + self.behaviours: list[Behaviour] = [] + + def device(self, device_id: str) -> dict: + return {"contrast": 3} + + def trigger(self): + def decorator(func): + return func + + return decorator + + +class PeopleCountEngineV1(Automation): + @Automation.trigger( + devices=["matrixclock"], rule=lambda h: h.device("matrixclock").contrast == 6 + ) + def turn_light_on_sometimes(self, home: Home): + home.room("max").lights().on = True + + +from mash.mash import MaSH + +mash = MaSH() + +mash.add_automation(PeopleCountEngineV1()) diff --git a/src/hue/hue_module.py b/src/hue/hue_feature.py similarity index 92% rename from src/hue/hue_module.py rename to src/hue/hue_feature.py index 86c63c4..72be1cf 100644 --- a/src/hue/hue_module.py +++ b/src/hue/hue_feature.py @@ -1,17 +1,17 @@ from fastapi import FastAPI, APIRouter from hue.hue_adapter import HueAdapter -from ..core.module import Module +from ..mash.feature import Feature from fastapi import APIRouter from fastapi.responses import HTMLResponse router = APIRouter(tags=["hue"]) hue = HueAdapter("192.168.178.85") -########## Module ########## +########## Integration ########## -class HueModule(Module): +class HueIntegration(Feature): def __init__(self) -> None: super().__init__("hue") diff --git a/src/main.py b/src/main.py index f715e03..1e34ed9 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ from core.mash import MaSH -from hue.hue_module import HueModule -from matrix_clock.matrix_clock_module import MatrixClockModule +from hue.hue_feature import HueModule +from matrix_clock.matrix_clock_feature import MatrixClockModule mash: MaSH = MaSH("config.yaml") diff --git a/src/mash/feature.py b/src/mash/feature.py new file mode 100644 index 0000000..ef1ed6b --- /dev/null +++ b/src/mash/feature.py @@ -0,0 +1,9 @@ +from fastapi import FastAPI + + +class Feature: + def __init__(self, feature_id: str) -> None: + self.integration_id = feature_id + + def add_routes(self, server: FastAPI) -> None: + pass diff --git a/src/core/mash.py b/src/mash/mash.py similarity index 75% rename from src/core/mash.py rename to src/mash/mash.py index b26630e..64b649d 100644 --- a/src/core/mash.py +++ b/src/mash/mash.py @@ -1,7 +1,7 @@ import yaml from fastapi import FastAPI -from core.module import Module +from mash.feature import Feature class MaSH: @@ -18,9 +18,9 @@ class MaSH: except FileNotFoundError: raise f"Config file for MaSH server could not be opened at [{config_path}]." - def add_module(self, module: Module) -> None: - module.add_routes(self.server) - self.server.include_router(module.get_router()) + def add_integration(self, feature: Feature) -> None: + feature.add_routes(self.server) + self.server.include_router(feature.get_router()) def run(self): self.server.run() diff --git a/src/matrix_clock/matrix_clock_module.py b/src/matrix_clock/matrix_clock_feature.py similarity index 50% rename from src/matrix_clock/matrix_clock_module.py rename to src/matrix_clock/matrix_clock_feature.py index 526acc8..fb03235 100644 --- a/src/matrix_clock/matrix_clock_module.py +++ b/src/matrix_clock/matrix_clock_feature.py @@ -1,6 +1,6 @@ -from core.module import Module +from mash.feature import Feature -class MatrixClockModule(Module): +class MatrixClockIntegration(Feature): def __init__(self) -> None: super().__init__("matrixclock")