Compare commits

..

8 Commits

Author SHA1 Message Date
John Lancaster
2f52febf38 additional bar lights stage 2025-12-02 10:45:55 -06:00
John Lancaster
d8476e9fdf added explicit hermes dns 2025-12-02 10:45:38 -06:00
John Lancaster
43beb1e950 commented get_history call 2025-12-02 10:45:25 -06:00
John Lancaster
81b5d5afa5 app tweaks 2025-11-30 23:05:37 -06:00
John Lancaster
b33016089f added conditions and transtions 2025-11-30 23:05:21 -06:00
John Lancaster
25892e1de8 added transition config 2025-11-30 16:41:46 -06:00
John Lancaster
05bad81571 added server being off to some stages 2025-11-23 13:59:19 -06:00
John Lancaster
697b09a7a1 made activation at startup optional 2025-11-23 13:56:22 -06:00
6 changed files with 97 additions and 16 deletions

View File

@@ -6,4 +6,4 @@ class HelloWorld(Hass):
self.log('Hello from AppDaemon')
self.log('You are now ready to run Apps!')
self.get_history('ball1')
# self.get_history('ball1')

View File

@@ -1,6 +1,8 @@
bar_lights:
module: light
class: StagedLight
activate-at-start: true
transition: 3
stages:
- start: '03:00 am'
scene:
@@ -8,18 +10,32 @@ bar_lights:
state: on
color_temp_kelvin: 4500
brightness: 25
light.server_lamp:
state: off
- start: '07:00 am'
scene:
light.bar:
state: on
color_temp_kelvin: 3500
brightness: 100
- start: '13:00'
brightness: 75
light.server_lamp:
state: off
- start: '09:00 am'
scene:
light.bar:
state: on
color_temp_kelvin: 2500
color_temp_kelvin: 3500
brightness: 125
light.server_lamp:
state: off
- start: '01:00 pm'
scene:
light.bar:
state: on
color_temp_kelvin: 3000
brightness: 150
light.server_lamp:
state: off
- start: 'sunset'
scene:
light.bar:
@@ -46,3 +62,5 @@ bar_lights:
state: on
color_temp_kelvin: 2202
brightness: 30
light.server_lamp:
state: off

View File

@@ -12,10 +12,19 @@ from stages import Stage
class StagedControlEvent(str, Enum):
"""Enum to define the different types of valid stage control events"""
ACTIVATE = 'activate'
DEACTIVATE = 'deactivate'
class StageCondition(str, Enum):
"""Enum to define the different types of conditions for an event"""
ANY_ON = 'any_on'
ALL_OFF = 'all_off'
class StagedLight(Hass):
def initialize(self):
self.set_log_level('DEBUG')
@@ -24,7 +33,13 @@ class StagedLight(Hass):
self.log(f'Initialized Motion Sensor with {len(self._stages)} stages')
self.listen_event(self.handle_event, 'stage_control', app=self.name)
self.activate()
if self.args.get('activate-at-start', False):
self.activate()
# self._check_transition()
self.schedule_transition_checks()
self.run_daily(self.schedule_transition_checks, start='00:00:00')
### Stages
@@ -52,30 +67,74 @@ class StagedLight(Hass):
def current_scene(self):
return self.current_stage().scene_json()
### Transitions
def schedule_transition_checks(self, **kwargs: Any):
now = self.get_now()
for stage in self._stages:
dt = self.parse_datetime(stage.start, aware=True, today=True)
if dt > now:
self.log(f'Scehduling transition at: {dt.isoformat()}', level='DEBUG')
self.run_at(self._check_transition, start=dt)
def _check_transition(self, **kwargs: Any):
self.log('Firing transition event', level='DEBUG')
self.fire_event(
'stage_control',
app=self.name,
condition=StageCondition.ANY_ON,
action=StagedControlEvent.ACTIVATE,
)
### Events
def handle_event(self, event_type: str, data: dict[str, Any], **kwargs: Any) -> None:
self.log(f'Event handler: {event_type}', level='DEBUG')
stage = self.current_stage()
start_time_str = stage.formatted_start('%I:%M %p')
scene = stage.scene_json()
match data:
case {'condition': StageCondition.ANY_ON}:
any_on = any(self.get_state(e) == 'on' for e in scene)
if not any_on:
self.log('Nothing is on, skipping', level='DEBUG')
return
case {'condition': StageCondition.ALL_OFF}:
all_off = all(self.get_state(e) == 'off' for e in scene)
if not all_off:
self.log('Everything is not off, skipping', level='DEBUG')
return
match data:
case {'action': StagedControlEvent.ACTIVATE}:
self.log(f'Activating current stage: {start_time_str}')
self.activate(scene=stage.scene_json())
self.activate(stage)
case {'action': StagedControlEvent.DEACTIVATE}:
self.log(f'Deactivating current stage: {start_time_str}')
self.deactivate()
self.deactivate(stage)
case _:
self.log(str(data), level='DEBUG')
self.log(str(kwargs), level='DEBUG')
### Actions
def activate(self, scene: dict | None = None, **kwargs):
scene = scene if scene is not None else self.current_scene()
return self.call_service('scene/apply', entities=scene, transition=5)
def activate(self, stage: Stage | None = None, **kwargs: Any):
if stage is None:
stage = self.current_stage()
kwargs['entities'] = stage.scene_json()
else:
kwargs['entities'] = stage.scene_json()
if t := self.args.get('transition'):
kwargs['transition'] = t
start_time_str = stage.formatted_start('%I:%M %p')
self.log(f'Activating current stage: {start_time_str}')
return self.call_service('scene/apply', **kwargs)
def deactivate(self, stage: Stage | None = None, **kwargs):
stage = stage if stage is not None else self.current_stage()
start_time_str = stage.formatted_start('%I:%M %p')
self.log(f'Deactivating current stage: {start_time_str}')
for entity in stage.scene:
self.turn_off(entity)

View File

@@ -23,8 +23,9 @@ class EntityState(BaseModel, extra='allow'):
class Stage(BaseModel):
# start: Annotated[time, BeforeValidator(lambda v: parser(v).time())]
start: str
_start: datetime = PrivateAttr()
scene: dict[str, EntityState]
_start: datetime = PrivateAttr()
transition: int | None = None
def assign_start(self, dt: datetime):
self._start = dt

View File

@@ -32,7 +32,7 @@ upper_stairs:
state: "on"
color_temp_kelvin: 2202
brightness: 60
- start: '11:00'
- start: '11:00 pm'
scene:
light.bathroom_stairs:
state: "on"

View File

@@ -2,8 +2,11 @@ services:
appdaemon:
container_name: appdaemon
image: acockburn/appdaemon:dev
# image: acockburn/appdaemon:local-dev
restart: unless-stopped
tty: true
tty: true
dns:
- 192.168.1.150
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro