Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Nov 5, 2023
1 parent 44bed89 commit 333fe5c
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 358 deletions.
1 change: 1 addition & 0 deletions wideboy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AppMetadata:
EVENT_MQTT_MESSAGE_RECEIVED = pygame.USEREVENT + 61
EVENT_MQTT_MESSAGE_SEND = pygame.USEREVENT + 62
EVENT_HASS_ENTITY_UPDATE = pygame.USEREVENT + 71
EVENT_HASS_STATESTREAM_UPDATE = pygame.USEREVENT + 72

# GAMEPAD

Expand Down
5 changes: 5 additions & 0 deletions wideboy/homeassistant/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
AppMetadata,
EVENT_HASS_ENTITY_UPDATE,
EVENT_MQTT_MESSAGE_RECEIVED,
EVENT_HASS_STATESTREAM_UPDATE,
)
from wideboy.homeassistant.mqtt import MQTTClient
from wideboy.utils.helpers import post_event
Expand Down Expand Up @@ -100,6 +101,10 @@ def parse_statestream_message(self, topic, payload) -> None:
except ValueError:
payload_cast = payload
self.state[entity_id] = payload_cast
post_event(
EVENT_HASS_STATESTREAM_UPDATE,
payload=dict(entity_id=entity_id, state=payload_cast),
)

def advertise_entities(self, entities: list[HASSEntity]) -> None:
for entity in entities:
Expand Down
99 changes: 0 additions & 99 deletions wideboy/sprites/hassentitytile/__init__.py

This file was deleted.

45 changes: 7 additions & 38 deletions wideboy/sprites/homeassistant/entity_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from wideboy.constants import (
EVENT_EPOCH_MINUTE,
EVENT_EPOCH_SECOND,
EVENT_MQTT_MESSAGE_RECEIVED,
EVENT_HASS_STATESTREAM_UPDATE,
)

# from wideboy.mqtt.homeassistant import HASS
Expand Down Expand Up @@ -44,16 +44,13 @@ def __init__(
) -> None:
super().__init__(scene, rect)
self.entities = entities
self.entity_states: Dict[str, Any] = dict()
self.entity_watches: Set[str] = set()
self.font_name = font_name
self.font_size = font_size
self.color_fg = color_fg
self.color_bg = color_bg
self.color_outline = color_outline
self.padding_right = padding_right
self.show_all = show_all
self.setup_watches()
self.render()

def update(
Expand All @@ -65,38 +62,11 @@ def update(
) -> None:
super().update(frame, clock, delta, events)
for event in events:
if event.type == EVENT_MQTT_MESSAGE_RECEIVED:
changed = self.parse_state_message(event.topic, event.payload)
if changed:
self.render()
if event.type == EVENT_EPOCH_MINUTE:
if (
event.type == EVENT_HASS_STATESTREAM_UPDATE
or 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:
self.entity_watches.update(entity.get("watch_entities", []))
for watched_entities in self.entity_watches:
self.entity_states[watched_entities] = ""

def parse_state_message(self, topic, payload) -> Union[bool, None]:
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}"
changed = False
if entity_id not in self.entity_watches:
return False
if entity_id in self.entity_states:
changed = self.entity_states[entity_id] != payload
self.entity_states[entity_id] = payload
if changed:
logger.debug(
f"hass:entity_row watch_entity_change entity={entity_id} state={self.entity_states[entity_id]}"
)
return changed

def render(self) -> None:
w, h = 1, 2
Expand All @@ -106,15 +76,14 @@ def render(self) -> None:
template = entity.get("template", None)
try:
try:
display = self.show_all or callback(self.entity_states)
display = self.show_all or callback(self.scene.hass.state)
except Exception as e:
logger.warn(f"hass:entity_row callback error={e}")
display = False

if template:
try:
tmpl = j2env.from_string(template)
label = tmpl.render(states=self.entity_states)
label = tmpl.render(states=self.scene.hass.state)
except Exception as e:
logger.warn(f"hass:entity_row template={template} error={e}")
label = ""
Expand Down
71 changes: 0 additions & 71 deletions wideboy/sprites/homeassistant/template.py

This file was deleted.

64 changes: 0 additions & 64 deletions wideboy/sprites/rotogrid/__init__.py

This file was deleted.

Loading

0 comments on commit 333fe5c

Please sign in to comment.