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 333fe5c commit 1f944b9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 75 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ types-paho-mqtt = "^1.6.0.5"
dynaconf = "^3.1.12"
pyyaml = "^6.0"
pygame-ce = "^2.2.1"
jinja2 = "^3.1.2"

[tool.poetry.group.dev.dependencies]
black = "^23.1.0"
Expand Down
88 changes: 39 additions & 49 deletions wideboy/scenes/default/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,60 +117,52 @@ def setup(self):
dict(
icon=MaterialIcons.MDI_DIRECTIONS_WALK,
icon_color=Color(255, 0, 255, 255),
template="{{ states['sensor.steps_louis'] }}",
template="{state[sensor.steps_louis]}",
watch_entities=["sensor.steps_louis"],
),
# active when public IP is home IP
dict(
icon=MaterialIcons.MDI_LOCK,
icon_color=Color(255, 0, 0, 255),
template="VPN DOWN ({{ states['sensor.privacy_ip_info'] }})",
cb_active=lambda states: states["sensor.privacy_ip_info"]
template="VPN DOWN ({state[sensor.privacy_ip_info]})",
cb_active=lambda state: state["sensor.privacy_ip_info"]
== settings.secrets.home_ip,
watch_entities=["sensor.privacy_ip_info"],
),
dict(
icon=MaterialIcons.MDI_VPN_LOCK,
icon_color=Color(255, 255, 255, 255),
template="{{ states['sensor.transmission_down_speed'] | int }}Mbps",
cb_active=lambda states: float(states["sensor.transmission_down_speed"])
> 0,
template="{state[sensor.transmission_down_speed]:.0f}Mbps",
cb_active=lambda state: state["sensor.transmission_down_speed"] > 0,
watch_entities=["sensor.transmission_down_speed"],
),
dict(
icon=MaterialIcons.MDI_DNS,
icon_color=Color(255, 255, 0, 255),
template="{{ states['sensor.ds920plus_volume_used'] | int }}%",
cb_active=lambda states: float(states["sensor.ds920plus_volume_used"])
> 66.66,
template="{state[sensor.ds920plus_volume_used]:.0f}%",
cb_active=lambda state: state["sensor.ds920plus_volume_used"] > 66.66,
watch_entities=["sensor.ds920plus_volume_used"],
),
dict(
icon=MaterialIcons.MDI_DOWNLOAD,
icon_color=Color(0, 255, 0, 255),
template="{{ states['sensor.speedtest_download_average'] | int }}Mbps",
cb_active=lambda states: float(
states["sensor.speedtest_download_average"]
)
template="{state[sensor.speedtest_download_average]:.0f}Mbps",
cb_active=lambda state: state["sensor.speedtest_download_average"]
< 600,
watch_entities=["sensor.speedtest_download_average"],
),
dict(
icon=MaterialIcons.MDI_UPLOAD,
icon_color=Color(255, 0, 0, 255),
template="{{ states['sensor.speedtest_upload_average'] | int }}Mbps",
cb_active=lambda states: float(
states["sensor.speedtest_upload_average"]
)
< 600,
template="{state[sensor.speedtest_upload_average]:.0f}Mbps",
cb_active=lambda state: state["sensor.speedtest_upload_average"] < 600,
watch_entities=["sensor.speedtest_upload_average"],
),
dict(
icon=MaterialIcons.MDI_WIFI,
icon_color=Color(0, 0, 255, 255),
template="{{ states['sensor.speedtest_ping_average'] | int }}ms",
cb_active=lambda states: float(states["sensor.speedtest_ping_average"])
> 10,
template="{state[sensor.speedtest_ping_average]:.0f}ms",
cb_active=lambda state: state["sensor.speedtest_ping_average"] > 10,
watch_entities=["sensor.speedtest_ping_average"],
),
# dict(
Expand All @@ -184,7 +176,7 @@ def setup(self):
icon=MaterialIcons.MDI_DOOR,
icon_color=Color(255, 64, 64, 255),
template="Back",
cb_active=lambda states: states[
cb_active=lambda state: state[
"binary_sensor.back_door_contact_sensor_contact"
]
== "on",
Expand All @@ -194,7 +186,7 @@ def setup(self):
icon=MaterialIcons.MDI_DOOR,
icon_color=Color(255, 64, 64, 255),
template="Front",
cb_active=lambda states: states[
cb_active=lambda state: state[
"binary_sensor.front_door_contact_sensor_contact"
]
== "on",
Expand All @@ -204,14 +196,14 @@ def setup(self):
icon=MaterialIcons.MDI_TOGGLE_ON,
icon_color=Color(255, 0, 0, 255),
template="MANUAL",
cb_active=lambda states: states["input_boolean.house_manual"] == "on",
cb_active=lambda state: state["input_boolean.house_manual"] == "on",
watch_entities=["input_boolean.house_manual"],
),
dict(
icon=MaterialIcons.MDI_AC_UNIT,
icon_color=Color(196, 196, 255, 255),
template="ON",
cb_active=lambda states: states["switch.lounge_fans"] == "on",
cb_active=lambda state: state["switch.lounge_fans"] == "on",
watch_entities=["switch.lounge_fans"],
),
]
Expand All @@ -228,28 +220,27 @@ def setup(self):
dict(
icon=MaterialIcons.MDI_BOLT,
icon_color=Color(192, 192, 192, 255),
template="{{ states['sensor.octopus_energy_electricity_current_demand'] | int }}w",
template="{state[sensor.octopus_energy_electricity_current_demand]:.0f}w",
watch_entities=["sensor.octopus_energy_electricity_current_demand"],
),
dict(
icon=MaterialIcons.MDI_SYMBOL_AT,
icon_color=Color(192, 192, 192, 255),
template="£{{ '{:.2f}'.format(states['sensor.octopus_energy_electricity_current_rate'] | float) }}",
template="£{state[sensor.octopus_energy_electricity_current_rate]:.2f}",
watch_entities=["sensor.octopus_energy_electricity_current_rate"],
),
dict(
icon=MaterialIcons.MDI_CURRENCY_DOLLAR,
icon_color=Color(255, 64, 64, 255),
template="£{{ ( (states['sensor.octopus_energy_electricity_current_demand'] | float / 1000) * (states['sensor.octopus_energy_electricity_current_rate'] | float) ) | currency }}",
template="£{state[sensor.electricity_hourly_rate]:.2f}",
watch_entities=[
"sensor.octopus_energy_electricity_current_demand",
"sensor.octopus_energy_electricity_current_rate",
"sensor.electricity_hourly_rate",
],
),
dict(
icon=MaterialIcons.MDI_SCHEDULE,
icon_color=Color(255, 64, 64, 255),
template="£{{ '{:.2f}'.format(states['sensor.octopus_energy_electricity_current_accumulative_cost'] | float) }}",
template="£{state[sensor.octopus_energy_electricity_current_accumulative_cost]:.2f}",
watch_entities=[
"sensor.octopus_energy_electricity_current_accumulative_cost"
],
Expand All @@ -268,46 +259,45 @@ def setup(self):
dict(
icon=MaterialIcons.MDI_BATTERY,
icon_color=Color(192, 192, 192, 255),
template="{{ states['sensor.delta_2_max_downstairs_battery_level'] | int }}%",
template="{state[sensor.delta_2_max_downstairs_battery_level]:.0f}%",
watch_entities=["sensor.delta_2_max_downstairs_battery_level"],
),
dict(
icon=MaterialIcons.MDI_LOOP,
icon_color=Color(192, 192, 192, 255),
template="{{ states['sensor.delta_2_max_downstairs_cycles'] | int }}",
template="{state[sensor.delta_2_max_downstairs_cycles]:.0f}",
watch_entities=["sensor.delta_2_max_downstairs_cycles"],
),
dict(
icon=MaterialIcons.MDI_HOURGLASS,
icon_color=Color(255, 64, 64, 255),
template="{{ states['sensor.delta_2_max_downstairs_discharge_remaining_time'] | int }}m",
cb_active=lambda states: float(
states["sensor.delta_2_max_downstairs_discharge_remaining_time"]
or 0
)
> 0,
template="{state[sensor.delta_2_max_downstairs_discharge_remaining_time]:.0f}m",
cb_active=lambda state: state[
"sensor.delta_2_max_downstairs_discharge_remaining_time"
]
or 0 > 0,
watch_entities=[
"sensor.delta_2_max_downstairs_discharge_remaining_time"
],
),
dict(
icon=MaterialIcons.MDI_HOURGLASS,
icon_color=Color(64, 255, 64, 255),
template="{{ ( states['sensor.delta_2_max_downstairs_charge_remaining_time'] | int ) }}m",
cb_active=lambda states: float(
states["sensor.delta_2_max_downstairs_charge_remaining_time"] or 0
)
> 0,
template="{state[sensor.delta_2_max_downstairs_charge_remaining_time]:.0f}m",
cb_active=lambda state: state[
"sensor.delta_2_max_downstairs_charge_remaining_time"
]
or 0 > 0,
watch_entities=["sensor.delta_2_max_downstairs_charge_remaining_time"],
),
dict(
icon=MaterialIcons.MDI_POWER,
icon_color=Color(255, 64, 64, 255),
template="{{ states['sensor.delta_2_max_downstairs_ac_in_power'] | int }}w",
cb_active=lambda states: float(
states["sensor.delta_2_max_downstairs_ac_in_power"] or 0
)
> 0,
template="{state[sensor.delta_2_max_downstairs_ac_in_power]:.0f}w",
cb_active=lambda state: state[
"sensor.delta_2_max_downstairs_ac_in_power"
]
or 0 > 0,
watch_entities=["sensor.delta_2_max_downstairs_ac_in_power"],
),
]
Expand Down
12 changes: 10 additions & 2 deletions wideboy/scenes/night/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,23 @@ def setup(self):
icon_color=Color(255, 255, 255, 255),
label_color=Color(128, 128, 128, 255),
template="Back",
cb_active=lambda state: state.state == "on",
cb_active=lambda state: state[
"binary_sensor.back_door_contact_sensor_contact"
]
== "on",
watch_entities=["binary_sensor.back_door_contact_sensor_contact"],
),
dict(
entity_id="binary_sensor.front_door_contact_sensor_contact",
icon=MaterialIcons.MDI_DOOR,
icon_color=Color(255, 255, 255, 255),
label_color=Color(128, 128, 128, 128),
template="Front",
cb_active=lambda state: state.state == "on",
cb_active=lambda state: state[
"binary_sensor.front_door_contact_sensor_contact"
]
== "on",
watch_entities=["binary_sensor.front_door_contact_sensor_contact"],
),
]

Expand Down
40 changes: 17 additions & 23 deletions wideboy/sprites/homeassistant/entity_row.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import logging
import types
from pygame import Clock, Color, Event, Rect, Surface, SRCALPHA
from jinja2 import Environment
from typing import Optional, List, Set, Dict, Any, Union
from wideboy.constants import (
EVENT_EPOCH_MINUTE,
EVENT_EPOCH_SECOND,
EVENT_HASS_STATESTREAM_UPDATE,
)

# from wideboy.mqtt.homeassistant import HASS
from wideboy.scenes.base import BaseScene
from wideboy.sprites.base import BaseSprite
from wideboy.sprites.image_helpers import render_text, render_material_icon

logger = logging.getLogger("sprite.hass_entity_row")

j2env = Environment()


def format_currency(amount):
return "{:,.2f}".format(amount)


j2env.filters["currency"] = format_currency


class HomeAssistantEntityRowSprite(BaseSprite):
rect: Rect
Expand Down Expand Up @@ -73,21 +62,26 @@ def render(self) -> None:
surfaces = []
for entity in self.entities:
callback = entity.get("cb_active", lambda e: True)
template = entity.get("template", None)
template = entity.get("template", "")
label = ""
display = False
try:
try:
display = self.show_all or callback(self.scene.hass.state)
except KeyError as e:
pass
except Exception as e:
logger.warn(f"hass:entity_row callback error={e}", exc_info=e)

try:
if isinstance(template, types.FunctionType):
label = template(self.scene.hass.state)
else:
label = template.format(state=self.scene.hass.state)
except KeyError as e:
pass
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.scene.hass.state)
except Exception as e:
logger.warn(f"hass:entity_row template={template} error={e}")
label = ""
display = False
logger.warn(f"hass:entity_row template={template} error={e}")

if not display:
continue
Expand Down

0 comments on commit 1f944b9

Please sign in to comment.