fix: narrow manual context suggestions

This commit is contained in:
2026-06-14 23:42:50 +02:00
parent 8cd8f3e3b7
commit 658516cd96
5 changed files with 60 additions and 7 deletions

View File

@@ -79,6 +79,23 @@ _MANUAL_CONTEXT_DOMAINS = frozenset({
"weather",
})
_CONTEXT_SUGGESTION_LIMIT = 120
_OUTDOOR_TOKENS = frozenset({"aussen", "außen", "outdoor", "garten", "terrasse", "balkon"})
_DIAGNOSTIC_TOKENS = frozenset({
"connect",
"count",
"diagnostic",
"firmware",
"last",
"linkquality",
"mqtt",
"reason",
"restart",
"rssi",
"signal",
"ssid",
"uptime",
"wifi",
})
class ActuatorReconciliationService:
@@ -131,6 +148,11 @@ class ActuatorReconciliationService:
selected = entity.entity_id in selected_ids
if selected:
score = max(score, 1.0)
if not selected and (
_is_diagnostic_context(entity)
or not _has_context_relationship(actuator, entity)
):
continue
if not selected and score < 0.1:
continue
ranked.append((score, _context_sort_group(entity), entity))
@@ -687,6 +709,32 @@ def _context_sort_group(entity: HaEntitySummary) -> str:
return f"20_{entity.domain}_{device_class}"
def _is_diagnostic_context(entity: HaEntitySummary) -> bool:
tokens = _metadata_tokens(entity, include_stopwords=True)
return bool(tokens.intersection(_DIAGNOSTIC_TOKENS))
def _has_context_relationship(actuator: HaEntitySummary, entity: HaEntitySummary) -> bool:
if (
actuator.area_name
and entity.area_name
and actuator.area_name == entity.area_name
and actuator.area_name.lower() not in _GENERIC_AREA_NAMES
):
return True
if actuator.device_id and entity.device_id and actuator.device_id == entity.device_id:
return True
if actuator.device_name and entity.device_name and actuator.device_name == entity.device_name:
return True
if _metadata_tokens(actuator).intersection(_metadata_tokens(entity)):
return True
entity_tokens = _metadata_tokens(entity, include_stopwords=True)
return bool(
entity_tokens.intersection(_OUTDOOR_TOKENS)
and entity.device_class in {"illuminance", "humidity", "temperature"}
)
def _score_candidate(
actuator: HaEntitySummary,
entity: HaEntitySummary,
@@ -732,8 +780,7 @@ def _score_candidate(
if context and role is EntityRole.BINARY_CONTEXT:
score += 0.05
evidence.append("Binärer Kontextsensor bevorzugt für Zusatzkontext.")
outdoor_tokens = {"aussen", "außen", "outdoor", "garten", "terrasse", "balkon"}
if entity_tokens.intersection(outdoor_tokens) and entity.device_class in {
if entity_tokens.intersection(_OUTDOOR_TOKENS) and entity.device_class in {
"illuminance",
"humidity",
"temperature",
@@ -801,7 +848,7 @@ def _preferred_device_classes(domain: str, *, context: bool) -> frozenset[str]:
return frozenset(mapping.get(domain, {"power", "energy", "temperature"}))
def _metadata_tokens(entity: HaEntitySummary) -> set[str]:
def _metadata_tokens(entity: HaEntitySummary, *, include_stopwords: bool = False) -> set[str]:
raw_values = [
entity.entity_id,
entity.friendly_name,
@@ -813,7 +860,7 @@ def _metadata_tokens(entity: HaEntitySummary) -> set[str]:
if value is None:
continue
for token in _TOKEN_PATTERN.findall(value.lower().replace("_", " ")):
if len(token) < 3 or token in _STOPWORDS:
if len(token) < 3 or (not include_stopwords and token in _STOPWORDS):
continue
tokens.add(token)
return tokens