Skip to content

Commit

Permalink
Some basic global statestream state
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Nov 5, 2023
1 parent 2d6c321 commit 44bed89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
35 changes: 28 additions & 7 deletions wideboy/homeassistant/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta
from pygame import Event
from requests_cache import CachedSession
from typing import Optional, Tuple
from typing import Optional, Tuple, Any, Union
from homeassistant_api import Client, State
from wideboy.config import settings
from wideboy.constants import (
Expand Down Expand Up @@ -33,7 +33,8 @@ def __init__(


class HASSManager:
state: Tuple[State, ...]
entities: dict[str, dict]
state: dict[str, Any]

def __init__(self, mqtt: MQTTClient, device_id: str):
self.mqtt = mqtt
Expand All @@ -46,7 +47,8 @@ def __init__(self, mqtt: MQTTClient, device_id: str):
expire_after=timedelta(seconds=settings.homeassistant.cache_duration),
),
)
self.entities: dict[str, dict] = dict()
self.entities = dict()
self.state = dict()

def handle_event(self, event: Event) -> None:
if event.type == EVENT_HASS_ENTITY_UPDATE:
Expand All @@ -63,10 +65,10 @@ def handle_event(self, event: Event) -> None:
for entity in self.entities.values()
if "command_topic" in entity["config"]
]
if event.topic.startswith(f"{settings.homeassistant.topic_prefix}/"):
logger.debug(
f"mqtt:statestream topic={event.topic} payload={event.payload}"
)
if event.topic.startswith(
f"{settings.homeassistant.topic_prefix}/"
) and event.topic.endswith("/state"):
self.parse_statestream_message(event.topic, event.payload)
else:
for entity in command_entities:
config, event_trigger = entity["config"], entity["event"]
Expand All @@ -80,6 +82,25 @@ def handle_event(self, event: Event) -> None:
)
post_event(event_trigger, payload=event.payload)

def parse_statestream_message(self, topic, payload) -> None:
logger.debug(f"mqtt:statestream:parse topic={topic} payload={payload}")
topic_exploded = topic.split("/")
if len(topic_exploded) < 3:
return None
device_class, device_id = topic_exploded[1:3]
entity_id = f"{device_class}.{device_id}"
payload_cast: Any = None
if payload.lower() in ["on", "true"]:
payload_cast = True
elif payload.lower() in ["off", "false"]:
payload_cast = False
else:
try:
payload_cast = float(payload)
except ValueError:
payload_cast = payload
self.state[entity_id] = payload_cast

def advertise_entities(self, entities: list[HASSEntity]) -> None:
for entity in entities:
self.advertise_entity(entity)
Expand Down
1 change: 1 addition & 0 deletions wideboy/scenes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
bg_color: Color = Color(0, 0, 0, 255),
) -> None:
self.engine = engine
self.hass = engine.hass
self.background = build_background(
Vector2(self.width, self.height),
bg_color,
Expand Down
2 changes: 2 additions & 0 deletions wideboy/sprites/homeassistant/entity_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def update(
self.render()
if event.type == EVENT_EPOCH_MINUTE:
self.render()
if event.type == EVENT_EPOCH_SECOND:
logger.debug(f"test:hass.state state={self.scene.hass.state}")

def setup_watches(self) -> None:
for entity in self.entities:
Expand Down

0 comments on commit 44bed89

Please sign in to comment.