From e8a64c0766b625e117f9b35be162a952543ba434 Mon Sep 17 00:00:00 2001 From: Louis King Date: Mon, 20 Nov 2023 22:53:07 +0000 Subject: [PATCH] Stash --- testing/sprites/__main__.py | 8 +- testing/sprites/lib/helpers.py | 26 +++++ testing/sprites/lib/tile_grid.py | 40 +++----- testing/sprites/tiles.py | 171 ++++++++++++++++--------------- 4 files changed, 130 insertions(+), 115 deletions(-) diff --git a/testing/sprites/__main__.py b/testing/sprites/__main__.py index dd9db40..4d91866 100644 --- a/testing/sprites/__main__.py +++ b/testing/sprites/__main__.py @@ -91,6 +91,7 @@ def randomise_state_rare(state: Dict): "sensor.speedtest_upload_average": speedtest_upload, "sensor.speedtest_ping_average": speedtest_ping, "sensor.ds920plus_volume_used": ds920plus_volume_used, + "switch.lounge_fans": "on" if random.choice([True, False]) else "off", } ) @@ -111,16 +112,17 @@ def randomise_state_rare(state: Dict): if event.type == pygame.QUIT: running = False - if frame % 100 == 0: + if frame % 300 == 0: randomise_state_common(state) print(f"State: {state}") - if frame % 300 == 0: + if frame % 1000 == 0: randomise_state_rare(state) print(f"State: {state}") screen.fill((0, 0, 0, 255)) sprite_group.update() - tile_grid.rect.topleft = (SCREEN_WIDTH - tile_grid.rect.width, 0) + if tile_grid.rect: + tile_grid.rect.topleft = (SCREEN_WIDTH - tile_grid.rect.width, 0) sprite_group.draw(screen) pygame.display.flip() clock.tick(FPS) diff --git a/testing/sprites/lib/helpers.py b/testing/sprites/lib/helpers.py index 7e4a97d..5a70223 100644 --- a/testing/sprites/lib/helpers.py +++ b/testing/sprites/lib/helpers.py @@ -11,6 +11,32 @@ LABEL_FONT_FILENAME = "fonts/bitstream-vera.ttf" LABEL_FONT_SIZE = 10 +# CUSTOM COLORS + + +class CommonColors: + COLOR_RED_DARK = pygame.Color(64, 0, 0, 255) + COLOR_RED = pygame.Color(255, 0, 0, 255) + COLOR_BLUE_DARK = pygame.Color(0, 0, 64, 255) + COLOR_BLUE = pygame.Color(0, 0, 255, 255) + COLOR_GREEN_DARK = pygame.Color(0, 64, 0, 255) + COLOR_GREEN = pygame.Color(0, 255, 0, 255) + COLOR_YELLOW_DARK = pygame.Color(64, 64, 0, 255) + COLOR_YELLOW = pygame.Color(255, 255, 0, 255) + COLOR_ORANGE_DARK = pygame.Color(64, 32, 0, 255) + COLOR_ORANGE = pygame.Color(255, 128, 0, 255) + COLOR_PURPLE_DARK = pygame.Color(64, 0, 64, 255) + COLOR_PURPLE = pygame.Color(255, 0, 255, 255) + COLOR_PINK_DARK = pygame.Color(64, 0, 32, 255) + COLOR_PINK = pygame.Color(255, 0, 128, 255) + COLOR_CYAN_DARK = pygame.Color(0, 64, 64, 255) + COLOR_CYAN = pygame.Color(0, 255, 255, 255) + COLOR_WHITE = pygame.Color(255, 255, 255, 255) + COLOR_BLACK = pygame.Color(0, 0, 0, 255) + COLOR_GREY = pygame.Color(64, 64, 64, 255) + COLOR_GREY_DARK = pygame.Color(16, 16, 16, 255) + COLOR_TRANSPARENT = pygame.Color(0, 0, 0, 0) + # FONTAWESOME CODEPOINTS diff --git a/testing/sprites/lib/tile_grid.py b/testing/sprites/lib/tile_grid.py index 40bbb32..2cd370b 100644 --- a/testing/sprites/lib/tile_grid.py +++ b/testing/sprites/lib/tile_grid.py @@ -6,8 +6,14 @@ from datetime import datetime from typing import Any, Callable, List, Dict, Optional, Tuple, Type, TypeVar, cast -from .helpers import Animator, AnimatorState, FontAwesomeIcons, render_icon, render_text -from .helpers import LABEL_FONT_FILENAME, LABEL_FONT_SIZE # TO BE REMOVED +from .helpers import ( + Animator, + AnimatorState, + FontAwesomeIcons, + CommonColors, + render_icon, + render_text, +) # NOTES @@ -16,28 +22,6 @@ logger.setLevel(logging.DEBUG) -# STYLE CLASSES - -""" -TILE_GRID_STYLE_DEFAULT = { - "cell_color_background": pygame.Color(16, 16, 16, 255), - "cell_color_border": pygame.Color(0, 0, 0, 0), - "label_color_text": pygame.Color(255, 255, 255, 255), - "label_color_outline": pygame.Color(0, 0, 0, 255), - "label_font": LABEL_FONT_FILENAME, - "label_size": LABEL_FONT_SIZE, - "label_padding": (2, 1), - "label_outline": True, - "label_antialias": True, - "icon_visible": True, - "icon_width": TILE_GRID_CELL_ICON_WIDTH, - "icon_height": TILE_GRID_CELL_ICON_HEIGHT, - "icon_color_background": pygame.Color(32, 32, 32, 255), - "icon_color_foreground": pygame.Color(255, 255, 255, 255), - "icon_codepoint": None, -} -""" - # CONSTANTS TILE_GRID_CELL_WIDTH = 64 @@ -206,20 +190,22 @@ def render(self): class VerticalCollapseTileGridCell(TileGridCell): + open: bool = True width: int = TILE_GRID_CELL_WIDTH height_animator: Animator def __init__(self, state): super().__init__(state) - self.height_animator = Animator(range=(0.0, 12.0), open=True, speed=1.0) + self.height_animator = Animator(range=(0.0, 12.0), open=self.open, speed=1.0) def update(self): + self.height_animator.set(self.open) self.height_animator.update() self.rect.height = self.height_animator.value super().update() @property - def open(self): + def open_finished(self): return self.height_animator.state != AnimatorState.CLOSED def __repr__(self): @@ -242,7 +228,7 @@ def update(self): @property def open(self): - return any([cell.open for cell in self.cells_inst]) + return any([cell.open_finished for cell in self.cells_inst]) def __repr__(self): return f"HorizontalCollapseTileGridColumn(open={self.open}, width={self.width_animator.value}, cells={len(self.cells_inst)})" diff --git a/testing/sprites/tiles.py b/testing/sprites/tiles.py index 0c5d128..82d573a 100644 --- a/testing/sprites/tiles.py +++ b/testing/sprites/tiles.py @@ -4,43 +4,30 @@ import random from .lib.tile_grid import ( TileGrid, - FontAwesomeIcons, HorizontalCollapseTileGridColumn, VerticalCollapseTileGridCell, + CommonColors, + FontAwesomeIcons, ) -# CUSTOM COLORS - - -class CustomColor(enum.Enum): - RED_DARK = pygame.Color(64, 0, 0, 255) - RED = pygame.Color(255, 0, 0, 255) - BLUE_DARK = pygame.Color(0, 0, 64, 255) - BLUE = pygame.Color(0, 0, 255, 255) - GREEN_DARK = pygame.Color(0, 64, 0, 255) - GREEN = pygame.Color(0, 255, 0, 255) - YELLOW_DARK = pygame.Color(64, 64, 0, 255) - YELLOW = pygame.Color(255, 255, 0, 255) - ORANGE_DARK = pygame.Color(64, 32, 0, 255) - ORANGE = pygame.Color(255, 128, 0, 255) - PURPLE_DARK = pygame.Color(64, 0, 64, 255) - PURPLE = pygame.Color(255, 0, 255, 255) - PINK_DARK = pygame.Color(64, 0, 32, 255) - PINK = pygame.Color(255, 0, 128, 255) - CYAN_DARK = pygame.Color(0, 64, 64, 255) - CYAN = pygame.Color(0, 255, 255, 255) - WHITE = pygame.Color(255, 255, 255, 255) - BLACK = pygame.Color(0, 0, 0, 255) - TRANSPARENT = pygame.Color(0, 0, 0, 0) - # CUSTOM SUBCLASSES class GridCell(VerticalCollapseTileGridCell): - pass + cell_color_background = CommonColors.COLOR_GREY_DARK + icon_color_background = CommonColors.COLOR_GREY + + +# CUSTOM FUNCTIONS + + +def format_watts(watts: int): + return "{:.0f}W".format(watts) if watts < 1000 else "{:.1f}kW".format(watts / 1000) +# TILE DEFINITIONS + # Electricity Tiles @@ -53,21 +40,25 @@ def value(self): self.state.get("sensor.octopus_energy_electricity_current_demand", 0) ) + @property + def open(self): + return self.value > 500 + @property def label(self): - return f"{self.value}" + return format_watts(self.value) @property - def icon_color_background(self): + def cell_color_background(self): return ( - CustomColor.RED.value + CommonColors.COLOR_RED_DARK if self.value > 1000 - else CustomColor.TRANSPARENT.value + else CommonColors.COLOR_GREY_DARK ) - def update(self): - super().update() - self.height_animator.set(self.value > 500) + @property + def icon_color_background(self): + return CommonColors.COLOR_RED if self.value > 1000 else CommonColors.COLOR_GREY class CellElectricityRate(GridCell): @@ -81,7 +72,7 @@ def value(self): @property def label(self): - return f"{self.value}" + return f"£{self.value:.2f}" class CellElectricityAccumulativeCost(GridCell): @@ -97,7 +88,7 @@ def value(self): @property def label(self): - return f"{self.value}" + return f"£{self.value:.2f}" # Battery Tiles @@ -112,7 +103,7 @@ def value(self): @property def label(self): - return f"{self.value}" + return f"{self.value}%" class CellBatteryACInput(GridCell): @@ -123,12 +114,12 @@ def value(self): return int(self.state.get("sensor.delta_2_max_downstairs_ac_in_power", 0)) @property - def label(self): - return f"{self.value}" + def open(self): + return self.value > 100 - def update(self): - super().update() - self.height_animator.set(self.value > 100) + @property + def label(self): + return format_watts(self.value) class CellBatteryACOutput(GridCell): @@ -139,12 +130,12 @@ def value(self): return int(self.state.get("sensor.delta_2_max_downstairs_ac_out_power", 0)) @property - def label(self): - return f"{self.value}" + def open(self): + return self.value > 100 - def update(self): - super().update() - self.height_animator.set(self.value > 100) + @property + def label(self): + return format_watts(self.value) # Network Tiles @@ -157,14 +148,14 @@ class CellSpeedTestDownload(GridCell): def value(self): return int(self.state.get("sensor.speedtest_download_average", 0)) + @property + def open(self): + return self.value > 500 + @property def label(self): return f"{self.value}Mb" - def update(self): - super().update() - self.height_animator.set(self.value > 500) - class CellSpeedTestUpload(GridCell): icon_codepoint = FontAwesomeIcons.ICON_FA_CIRCLE_ARROW_UP @@ -173,14 +164,14 @@ class CellSpeedTestUpload(GridCell): def value(self): return int(self.state.get("sensor.speedtest_upload_average", 0)) + @property + def open(self): + return self.value > 500 + @property def label(self): return f"{self.value}Mb" - def update(self): - super().update() - self.height_animator.set(self.value > 500) - class CellSpeedTestPing(GridCell): icon_codepoint = FontAwesomeIcons.ICON_FA_HEART_PULSE @@ -189,14 +180,14 @@ class CellSpeedTestPing(GridCell): def value(self): return int(self.state.get("sensor.speedtest_ping_average", 0)) + @property + def open(self): + return self.value > 25 + @property def label(self): return f"{self.value}ms" - def update(self): - super().update() - self.height_animator.set(self.value > 25) - class CellDS920VolumeUsage(GridCell): icon_codepoint = FontAwesomeIcons.ICON_FA_HARD_DRIVE @@ -206,12 +197,12 @@ def value(self): return int(self.state.get("sensor.ds920plus_volume_used", 0)) @property - def label(self): - return f"{self.value}" + def open(self): + return self.value > 60 - def update(self): - super().update() - self.height_animator.set(self.value > 60) + @property + def label(self): + return f"{self.value}%" # Test Tiles @@ -224,14 +215,14 @@ class CellTestRandom(GridCell): def value(self): return datetime.datetime.now().second + @property + def open(self): + return 0 <= self.value % 5 <= 2 + @property def label(self): return f"{self.value}" - def update(self): - super().update() - self.height_animator.set(0 <= self.value % 5 <= 2) - # Switch Tiles @@ -242,36 +233,40 @@ class CellSwitchLoungeFan(GridCell): @property def value(self): - return self.state.get("fan", 0) + return self.state.get("fan", "off") - def update(self): - super().update() - self.height_animator.set(self.value > 90) + @property + def open(self): + return self.value == "on" # CUSTOM COLUMNS +rainbox_colors = [ + CommonColors.COLOR_RED, + CommonColors.COLOR_ORANGE, + CommonColors.COLOR_YELLOW, + CommonColors.COLOR_GREEN, + CommonColors.COLOR_BLUE, + CommonColors.COLOR_PURPLE, + CommonColors.COLOR_PINK, +] -class GridColumnNetwork(HorizontalCollapseTileGridColumn): + +class GridColumnHomeLab(HorizontalCollapseTileGridColumn): border_width = 1 - border_color = CustomColor.RED.value + border_color = rainbox_colors[0] cells = [ + CellDS920VolumeUsage, CellSpeedTestDownload, CellSpeedTestUpload, - # CellSpeedTestPing, - # CellDS920VolumeUsage, + CellSpeedTestPing, ] -class GridColumnTest(HorizontalCollapseTileGridColumn): - border_width = 1 - border_color = CustomColor.GREEN.value - cells = [CellTestRandom] - - class GridColumnBattery(HorizontalCollapseTileGridColumn): border_width = 1 - border_color = CustomColor.BLUE.value + border_color = rainbox_colors[1] cells = [ CellBatteryLevel, CellBatteryACInput, @@ -281,7 +276,7 @@ class GridColumnBattery(HorizontalCollapseTileGridColumn): class GridColumnElectricity(HorizontalCollapseTileGridColumn): border_width = 1 - border_color = CustomColor.YELLOW.value + border_color = rainbox_colors[2] cells = [ CellElectricityDemand, CellElectricityRate, @@ -289,13 +284,19 @@ class GridColumnElectricity(HorizontalCollapseTileGridColumn): ] +class GridColumnTest(HorizontalCollapseTileGridColumn): + border_width = 1 + border_color = rainbox_colors[3] + cells = [CellTestRandom] + + # CUSTOM GRID class CustomTileGrid(TileGrid): columns = [ - GridColumnNetwork, - GridColumnTest, + GridColumnHomeLab, + # GridColumnTest, GridColumnBattery, GridColumnElectricity, ]