started ControllerDaylight

This commit is contained in:
John Lancaster
2023-04-15 16:05:06 -05:00
parent 6aa9154a78
commit e301c6838f
2 changed files with 66 additions and 8 deletions

View File

@@ -1,11 +1,11 @@
from dataclasses import dataclass, field
from datetime import timedelta
from datetime import datetime, timedelta
from html import entities
from typing import List
from appdaemon.adapi import ADAPI
from appdaemon.entity import Entity
from appdaemon.plugins.hass.hassapi import Hass
from daylight_adjuster import DaylightAdjuster
@dataclass(init=False)
@@ -26,6 +26,7 @@ class ControllerBase(Hass):
self.log(f'Initialized controller for {[e.friendly_name for e in self.entities]}')
@dataclass(init=False)
class ControllerRoom(ControllerBase):
def initialize(self):
@@ -47,6 +48,7 @@ class ControllerRoom(ControllerBase):
def state(self) -> bool:
return any([e.get_state() == 'on' for e in self.entities])
@dataclass(init=False)
class ControllerMotion(ControllerBase):
room: ControllerRoom
@@ -67,7 +69,6 @@ class ControllerMotion(ControllerBase):
self.off_duration = timedelta()
self.sync_state()
self.listen_state(self.sync_state, [e.entity_id for e in self.entities])
@property
@@ -115,6 +116,7 @@ class ControllerMotion(ControllerBase):
self.log(f'Motion stopped on {self.friendly_name(entity)} for {self.off_duration}')
self.room.deactivate()
@dataclass(init=False)
class ControllerButton(Hass):
room: ControllerRoom
@@ -133,7 +135,7 @@ class ControllerButton(Hass):
id=button,
)
self.log(f'Listening to presses on button ID={button}')
def callback_button(self, event_name, data, kwargs):
# single press
if data['event'] == 1002:
@@ -142,4 +144,31 @@ class ControllerButton(Hass):
self.room.deactivate()
else:
self.room.activate()
@dataclass(init=False)
class ControllerDaylight(ControllerBase):
latitude: float
longitude: float
def initialize(self):
super().initialize()
self.adjuster = DaylightAdjuster(
latitude=self.args['latitude'],
longitude=self.args['longitude'],
periods=self.args['periods'],
resolution=500
)
self.log(self.adjuster)
self.listen_state(callback=self.adjust, entity_id=[e.entity_id for e in self.entities])
self.log(f'Listening for state {[e.friendly_name for e in self.entities]}')
def adjust(self, entity, attribute, old, new, kwargs):
if new == 'on':
self.adjustment_handle = self.run_every(callback=self.ongoing_adjustment, start='now', interval=10, entity=entity)
else:
self.log(f'Cancelling adjustments')
self.cancel_timer(self.adjustment_handle)
def ongoing_adjustment(self, kwargs):
self.log(f'Adjusting {self.friendly_name(kwargs["entity"])}')