handling multiple buttons

This commit is contained in:
John Lancaster
2024-07-27 14:53:07 -05:00
parent 9dfd3e7f38
commit 9af8db9198
3 changed files with 6 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from logging import Logger, LoggerAdapter from logging import LoggerAdapter
from appdaemon.adapi import ADAPI from appdaemon.adapi import ADAPI

View File

@@ -4,7 +4,7 @@ from typing import Annotated, Dict, List, Optional, Self
import yaml import yaml
from astral import SunDirection from astral import SunDirection
from pydantic import BaseModel, BeforeValidator, Field, model_validator, root_validator from pydantic import BaseModel, BeforeValidator, Field, model_validator
from pydantic_core import PydanticCustomError from pydantic_core import PydanticCustomError
from rich.console import Console, ConsoleOptions, RenderResult from rich.console import Console, ConsoleOptions, RenderResult
from rich.table import Column, Table from rich.table import Column, Table
@@ -53,10 +53,7 @@ class ControllerStateConfig(BaseModel):
@model_validator(mode='before') @model_validator(mode='before')
def check_args(cls, values): def check_args(cls, values):
time, elevation = values.get('time'), values.get('elevation') if values.get('elevation') is not None and values.get('direction') is None:
# if time is not None and elevation is not None:
# raise PydanticCustomError('bad_time_spec', 'Only one of time or elevation can be set.')
if elevation is not None and 'direction' not in values:
raise PydanticCustomError('no_sun_dir', 'Needs sun direction with elevation') raise PydanticCustomError('no_sun_dir', 'Needs sun direction with elevation')
return values return values

View File

@@ -79,6 +79,8 @@ class RoomController(Hass):
if button := self.args.get('button'): if button := self.args.get('button'):
if isinstance(button, str): if isinstance(button, str):
self.button = Button(self, button_name=button) self.button = Button(self, button_name=button)
elif isinstance(button, list) and all(isinstance(b, str) for b in button):
self.button = [Button(self, button_name=b) for b in button]
if door := self.args.get('door'): if door := self.args.get('door'):
if isinstance(door, str): if isinstance(door, str):
@@ -166,7 +168,7 @@ class RoomController(Hass):
self.call_service(f'{self.name}/activate', namespace='controller', **kwargs) self.call_service(f'{self.name}/activate', namespace='controller', **kwargs)
def _service_activate(self, namespace: str, domain: str, service: str, kwargs: Dict[str, Any]): def _service_activate(self, namespace: str, domain: str, service: str, kwargs: Dict[str, Any]):
self.log(f'Custom kwargs: {kwargs}', level='DEBUG') # self.log(f'Custom kwargs: {kwargs}', level='DEBUG')
state = self.current_state() state = self.current_state()
if isinstance(state.scene, str): if isinstance(state.scene, str):
self.turn_on(state.scene) self.turn_on(state.scene)