This commit is contained in:
Jeffrey C. Ollie 2023-08-30 16:56:45 -05:00
parent 3278fd431f
commit a93031c35b
Signed by: jeff
GPG key ID: 6F86035A6D97044E
2 changed files with 14 additions and 12 deletions

View file

@ -1,8 +1,15 @@
from typing import Annotated, Literal, Union from typing import Annotated
from typing import Literal
from typing import Union
from pydantic import BaseModel, Field, ValidationError, root_validator, validator from pydantic import BaseModel
from pydantic import Field
from pydantic import ValidationError
from pydantic import root_validator
from pydantic import validator
from pydantic.color import Color from pydantic.color import Color
from pydantic_yaml import YamlModel
# from pydantic_yaml import YamlModel
from typing_extensions import Self from typing_extensions import Self
@ -130,7 +137,7 @@ class TileConfig(BaseModel):
actions: list[ActionConfig] actions: list[ActionConfig]
class Config(YamlModel): class Config(BaseModel):
homeassistant: HomeAssistantConfig homeassistant: HomeAssistantConfig
streamdecks: list[StreamDeckConfig] streamdecks: list[StreamDeckConfig]
screens: dict[str, list[TileConfig]] screens: dict[str, list[TileConfig]]

View file

@ -8,6 +8,7 @@ import click
# from PIL import ImageDraw # from PIL import ImageDraw
# from PIL import ImageFont # from PIL import ImageFont
from pydantic import parse_obj_as from pydantic import parse_obj_as
from pydantic_yaml import parse_yaml_raw_as
from typing_extensions import Self from typing_extensions import Self
from greendeck.config import Config from greendeck.config import Config
@ -74,7 +75,6 @@ class DeckHandler:
return None return None
async def start(self: Self): async def start(self: Self):
await self.deck.open() await self.deck.open()
await self.deck.reset() await self.deck.reset()
@ -102,7 +102,6 @@ class DeckHandler:
async def update_key_state( async def update_key_state(
self: Self, key: int, old_state: bool | None, new_state: bool self: Self, key: int, old_state: bool | None, new_state: bool
) -> None: ) -> None:
self.key_states[key] = new_state self.key_states[key] = new_state
if new_state is False: if new_state is False:
@ -126,7 +125,6 @@ class DeckHandler:
t.add_done_callback(task_done_callback) t.add_done_callback(task_done_callback)
async def update_key_image(self: Self, key: int) -> None: async def update_key_image(self: Self, key: int) -> None:
tile_config = self.get_tile_config_for_key(key) tile_config = self.get_tile_config_for_key(key)
if tile_config is None: if tile_config is None:
@ -141,7 +139,6 @@ class DeckHandler:
) )
case "homeassistant-icon": case "homeassistant-icon":
home_assistant_icon_image_config = parse_obj_as( home_assistant_icon_image_config = parse_obj_as(
HomeAssistantIconImageConfig, tile_config.image HomeAssistantIconImageConfig, tile_config.image
) )
@ -174,7 +171,6 @@ class DeckHandler:
old_state: bool | None, old_state: bool | None,
new_state: bool, new_state: bool,
) -> None: ) -> None:
assert self.deck.serial_number == deck.serial_number assert self.deck.serial_number == deck.serial_number
position = self.convert_key_to_position(key) position = self.convert_key_to_position(key)
@ -214,14 +210,12 @@ class DeckHandler:
key: int, key: int,
event: EventResponse, event: EventResponse,
) -> None: ) -> None:
self.homeassistant_states[key] = event.event.data.new_state.state self.homeassistant_states[key] = event.event.data.new_state.state
t = asyncio.create_task(self.update_key_image(key)) t = asyncio.create_task(self.update_key_image(key))
t.add_done_callback(task_done_callback) t.add_done_callback(task_done_callback)
async def update_homeassistant_callbacks(self: Self, key: int): async def update_homeassistant_callbacks(self: Self, key: int):
tile_config = self.get_tile_config_for_key(key) tile_config = self.get_tile_config_for_key(key)
if tile_config is None: if tile_config is None:
return return
@ -331,7 +325,8 @@ def main(
config_path: pathlib.Path, config_path: pathlib.Path,
): ):
setup_logging() setup_logging()
config = Config.parse_raw(config_path.read_bytes(), proto="yaml") # config = Config.parse_raw(config_path.read_bytes(), proto="yaml")
config: Config = parse_yaml_raw_as(Config, config_path.read_bytes())
asyncio.run(_main(config)) asyncio.run(_main(config))