Compare commits

...

2 Commits

Author SHA1 Message Date
b8012de934 BUG-003: HeatingRule auf heizungsrelevante Sensoren begrenzen
- matches(): nur climate oder sensor mit device_class in {temperature, humidity, occupancy, presence, heating}
- Tests: ergänzt für relevante und nicht-relevante Sensoren
- Verhindert falsche Empfehlungen bei Strom-/Tür-/Wetter-Sensoren
2026-06-10 21:30:25 +02:00
6540d62ff7 ml/rules-recommendations: regelbasierte Heizungsempfehlung und Recommender 2026-06-10 20:46:54 +02:00
5 changed files with 97 additions and 1 deletions

1
app/rules/__init__.py Normal file
View File

@@ -0,0 +1 @@
# sillyhome-next.rules

29
app/rules/heating.py Normal file
View File

@@ -0,0 +1,29 @@
from __future__ import annotations
from collections.abc import Sequence
from app.ha.models import HaEntitySummary
from app.rules.recommender import Rule
class HeatingRule(Rule):
def matches(self, entities: Sequence[HaEntitySummary]) -> bool:
domains = {item.domain for item in entities}
if "climate" in domains:
return True
if "sensor" in domains:
# Nur Sensoren mit Heizungs-/Klimarelevanz
relevant_device_classes = {
"temperature",
"humidity",
"occupancy",
"presence",
"heating",
}
for entity in entities:
if entity.domain == "sensor" and entity.device_class in relevant_device_classes:
return True
return False
def recommendation(self, entities: Sequence[HaEntitySummary]) -> str:
return "Prüfe Heizungsregelung: Aktiviere energiesparenden Modus bei Abwesenheit."

25
app/rules/recommender.py Normal file
View File

@@ -0,0 +1,25 @@
from __future__ import annotations
from collections.abc import Sequence
from app.ha.models import HaEntitySummary
class Rule:
def matches(self, entities: Sequence[HaEntitySummary]) -> bool:
raise NotImplementedError
def recommendation(self, entities: Sequence[HaEntitySummary]) -> str:
raise NotImplementedError
class Recommender:
def __init__(self, rules: Sequence[Rule]) -> None:
self._rules = rules
def run(self, entities: Sequence[HaEntitySummary]) -> list[str]:
results: list[str] = []
for rule in self._rules:
if rule.matches(entities):
results.append(rule.recommendation(entities))
return results

View File

@@ -25,4 +25,4 @@ strict = true
[tool.ruff]
line-length = 100
target-version = "py311"
target-version = "py311"

View File

@@ -0,0 +1,41 @@
from __future__ import annotations
from app.ha.models import HaEntitySummary
from app.rules.heating import HeatingRule
from app.rules.recommender import Recommender
def _sensor(entity_id: str, device_class: str | None = None) -> HaEntitySummary:
return HaEntitySummary(entity_id=entity_id, domain="sensor", device_class=device_class)
def _climate(entity_id: str) -> HaEntitySummary:
return HaEntitySummary(entity_id=entity_id, domain="climate")
def test_heating_rule_triggers() -> None:
rule = HeatingRule()
assert rule.matches([_climate("climate.living_room")])
# Relevante Sensoren
assert rule.matches([_sensor("sensor.temperature_living", device_class="temperature")])
assert rule.matches([_sensor("sensor.humidity_bath", device_class="humidity")])
assert rule.matches([_sensor("sensor.occupancy_living", device_class="occupancy")])
assert rule.matches([_sensor("sensor.presence_entry", device_class="presence")])
assert rule.matches([_sensor("sensor.heating_status", device_class="heating")])
def test_heating_rule_ignores_non_relevant_sensors() -> None:
rule = HeatingRule()
# Nicht-relevante Sensoren
assert not rule.matches([_sensor("sensor.power", device_class="power")])
assert not rule.matches([_sensor("sensor.voltage", device_class="voltage")])
assert not rule.matches([_sensor("sensor.door", device_class="door")])
assert not rule.matches([_sensor("sensor.window", device_class="window")])
assert not rule.matches([_sensor("sensor.light", device_class="illuminance")])
def test_recommender_uses_rule() -> None:
recommender = Recommender(rules=[HeatingRule()])
assert recommender.run([_climate("climate.living_room")]) == [
"Prüfe Heizungsregelung: Aktiviere energiesparenden Modus bei Abwesenheit."
]