Skip to content

Commit

Permalink
Stash
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Nov 11, 2023
1 parent 91ef099 commit b289ae6
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 11 deletions.
28 changes: 19 additions & 9 deletions wideboy/scenes/default/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from wideboy.sprites.homeassistant.entity_row import (
HomeAssistantEntityRowSprite,
HomeAssistantEntityTile,
HomeAssistantEntityTileOrientation,
)
from wideboy.sprites.homeassistant.entity_grid import HomeAssistantEntityGridSprite
from wideboy.sprites.notification import NotificationSprite
from wideboy.sprites.weather.animation import WeatherAnimationSprite
from wideboy.sprites.weather.temperature import WeatherTemperatureSprite
Expand Down Expand Up @@ -170,13 +170,9 @@ def setup(self):

self.hass_row_power = HomeAssistantEntityRowSprite(
self,
Rect(700, 2, 128, 60),
Rect(512, 48, 128, 16),
hass_row_power_entities,
min_size=(64, None),
color_bg=Color(0, 0, 0, 196),
orientation=HomeAssistantEntityTileOrientation.VERTICAL,
font_size=11,
padding_bottom=2,
)
self.group.add(self.hass_row_power)

Expand All @@ -196,6 +192,20 @@ def setup(self):
)
self.group.add(self.hass_row_battery)

# =====================================================================
# HASS ENTITY GRID WIDGETS
# =====================================================================

self.hass_grid_test = HomeAssistantEntityGridSprite(
self,
Rect(self.width - 460, 0, 128, 64),
grid_size=(2, 4),
padding=(0, 0),
title="Battery",
cells=[[1, 2, 3, 4], [5, 6, 7, 8]],
)
self.group.add(self.hass_grid_test)

# =====================================================================
# NOTIFICATION WIDGET
# =====================================================================
Expand Down Expand Up @@ -226,9 +236,9 @@ def update(
events: list[Event],
) -> None:
super().update(clock, delta, events)
self.hass_row_main.rect.topright = self.width - 256 - 3, 2
self.hass_row_power.rect.topright = self.width - 68, 4
self.hass_row_battery.rect.topright = self.width - 256 - 3, 32
self.hass_row_main.rect.topright = self.width - 128 - 3, 2
self.hass_row_power.rect.topright = self.width - 128 - 3, 17
self.hass_row_battery.rect.topright = self.width - 128 - 3, 32

# Handle Events

Expand Down
1 change: 0 additions & 1 deletion wideboy/scenes/default/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class TileStepsLouis(HomeAssistantEntityTile):
label_font_size = 12
icon = MaterialIcons.MDI_DIRECTIONS_WALK
icon_color = Color(255, 0, 255, 255)

Expand Down
163 changes: 163 additions & 0 deletions wideboy/sprites/homeassistant/entity_grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import logging
import types
from enum import Enum
from pygame import Clock, Color, Event, Rect, Surface, SRCALPHA
from typing import Optional, List, Set, Dict, Any, Union, Tuple
from wideboy.constants import (
EVENT_EPOCH_MINUTE,
EVENT_HASS_STATESTREAM_UPDATE,
)

from wideboy.scenes.base import BaseScene
from wideboy.sprites.base import BaseSprite
from wideboy.sprites.image_helpers import (
MaterialIcons,
render_text,
render_material_icon,
)

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


class HomeAssistantEntityTile:
visible: bool = True
icon: int = MaterialIcons.MDI_DELETE
icon_color: Color = Color(255, 255, 255, 255)
label: str = ""
label_color: Color = Color(255, 255, 255, 255)
label_font_size: Optional[int] = None

def process(self, state) -> None:
pass


class HomeAssistantEntityGridSprite(BaseSprite):
rect: Rect
image: Surface

def __init__(
self,
scene: BaseScene,
rect: Rect,
grid_size: Tuple[int, int] = (1, 5),
cell_size: Tuple[int, int] = (64, 12),
title: Optional[str] = None,
cells: List[List[int]] = [],
alpha: int = 192,
accent_color: Color = Color(255, 0, 0, 255),
padding: Tuple[int, int] = (0, 0),
font_name: str = "fonts/bitstream-vera.ttf",
font_size: int = 10,
) -> None:
super().__init__(scene, rect)
self.grid_size = grid_size
self.cell_size = cell_size
self.title = title
self.cells = cells
self.alpha = alpha
self.accent_color = accent_color
self.padding = padding
self.font_name = font_name
self.font_size = font_size
self.render()

def update(
self,
frame: str,
clock: Clock,
delta: float,
events: list[Event],
) -> None:
super().update(frame, clock, delta, events)
for event in events:
if (
event.type == EVENT_HASS_STATESTREAM_UPDATE
or event.type == EVENT_EPOCH_MINUTE
):
self.render()

def render(self) -> None:
self.image = Surface((self.rect.width, self.rect.height), SRCALPHA)
self.image.fill(Color(0, 0, 0, self.alpha))
bx, by = self.padding[0], self.padding[1]
cx, cy = bx, by
if self.title is not None:
title_surface = render_text(
self.title,
self.font_name,
self.font_size,
color_fg=Color(255, 255, 255, 255),
)
self.image.blit(title_surface, (cx + 1, cy - 1))
by += title_surface.get_rect().height
self.image.fill(self.accent_color, (bx, by, self.rect.width, 1))
cy = by + 1
for col_idx, col in enumerate(self.cells):
if col_idx + 1 > self.grid_size[0]:
continue
for cell_idx, cell in enumerate(col):
if cell_idx + 1 > self.grid_size[1]:
continue
self.image.blit(
render_hass_tile(
icon_codepoint=MaterialIcons.MDI_DELETE,
icon_color=Color(255, 255, 255, 255),
icon_size=11,
label_text=f"{cell}",
label_font_name="fonts/bitstream-vera.ttf",
label_font_size=10,
label_color=Color(255, 255, 255, 255),
bg_color=Color(0, 0, 0, 0),
outline_color=Color(0, 0, 0, 255),
padding_right=0,
),
(cx, cy),
)
cy += self.cell_size[1]
cy = by
cx += self.cell_size[0]
self.dirty = 1


def render_hass_tile(
icon_codepoint: Optional[int] = None,
icon_color: Color = Color(255, 255, 255, 255),
icon_size: int = 11,
label_text: Optional[str] = None,
label_font_name: str = "fonts/bitstream-vera.ttf",
label_font_size: int = 10,
label_color: Color = Color(255, 255, 255, 255),
bg_color: Color = Color(0, 0, 0, 0),
outline_color: Color = Color(0, 0, 0, 255),
padding_right: int = 0,
) -> Surface:
w, h = 0, 0
icon_surface = None
# logger.debug(f"render_hass_tile: state={state.dict()}")
if icon_codepoint:
icon_surface = render_material_icon(
icon_codepoint, icon_size, icon_color, outline_color
)
w += icon_surface.get_rect().width
h = icon_surface.get_rect().height
label_surface = None
if label_text:
label_surface = render_text(
label_text,
label_font_name,
label_font_size,
color_fg=label_color,
color_bg=bg_color,
color_outline=outline_color,
)
w += label_surface.get_rect().width
h = max(h, label_surface.get_rect().height)
surface = Surface((w + padding_right, h), SRCALPHA)
x = 0
if icon_surface is not None:
surface.blit(icon_surface, (x, 0))
x += icon_surface.get_rect().width
if label_surface is not None:
surface.blit(label_surface, (x, -1))
x += label_surface.get_rect().width
return surface
2 changes: 1 addition & 1 deletion wideboy/sprites/homeassistant/entity_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
color_fg: Color = Color(255, 255, 255, 255),
color_bg: Color = Color(0, 0, 0, 255),
color_outline: Optional[Color] = None,
padding_right: int = 3,
padding_right: int = 0,
padding_bottom: int = 0,
show_all: bool = False,
) -> None:
Expand Down

0 comments on commit b289ae6

Please sign in to comment.