More refactoring, and example config-yaml and code snippets

This commit is contained in:
Maximilian Giller 2024-04-22 19:36:25 +02:00
parent 62709b6698
commit e668fc1eaa
8 changed files with 101 additions and 24 deletions

View file

@ -1,7 +1,31 @@
modules: features:
hue: hue:
- ip: "192.168.178.23" hue-bridge:
ip: 192.168.178.23
matrixclock: matrixclock:
- name: wfwfo clock:
- ip: "192.168.178.23" 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

View file

@ -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

53
src/example.py Normal file
View file

@ -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())

View file

@ -1,17 +1,17 @@
from fastapi import FastAPI, APIRouter from fastapi import FastAPI, APIRouter
from hue.hue_adapter import HueAdapter from hue.hue_adapter import HueAdapter
from ..core.module import Module from ..mash.feature import Feature
from fastapi import APIRouter from fastapi import APIRouter
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
router = APIRouter(tags=["hue"]) router = APIRouter(tags=["hue"])
hue = HueAdapter("192.168.178.85") hue = HueAdapter("192.168.178.85")
########## Module ########## ########## Integration ##########
class HueModule(Module): class HueIntegration(Feature):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__("hue") super().__init__("hue")

View file

@ -1,6 +1,6 @@
from core.mash import MaSH from core.mash import MaSH
from hue.hue_module import HueModule from hue.hue_feature import HueModule
from matrix_clock.matrix_clock_module import MatrixClockModule from matrix_clock.matrix_clock_feature import MatrixClockModule
mash: MaSH = MaSH("config.yaml") mash: MaSH = MaSH("config.yaml")

9
src/mash/feature.py Normal file
View file

@ -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

View file

@ -1,7 +1,7 @@
import yaml import yaml
from fastapi import FastAPI from fastapi import FastAPI
from core.module import Module from mash.feature import Feature
class MaSH: class MaSH:
@ -18,9 +18,9 @@ class MaSH:
except FileNotFoundError: except FileNotFoundError:
raise f"Config file for MaSH server could not be opened at [{config_path}]." raise f"Config file for MaSH server could not be opened at [{config_path}]."
def add_module(self, module: Module) -> None: def add_integration(self, feature: Feature) -> None:
module.add_routes(self.server) feature.add_routes(self.server)
self.server.include_router(module.get_router()) self.server.include_router(feature.get_router())
def run(self): def run(self):
self.server.run() self.server.run()

View file

@ -1,6 +1,6 @@
from core.module import Module from mash.feature import Feature
class MatrixClockModule(Module): class MatrixClockIntegration(Feature):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__("matrixclock") super().__init__("matrixclock")