-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91ef099
commit b289ae6
Showing
4 changed files
with
183 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters