From e301c6838f303844357bc60e74b4b7073473ee29 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sat, 15 Apr 2023 16:05:06 -0500 Subject: [PATCH] started ControllerDaylight --- apps/controller.py | 41 +++++++++++++++++++++++++++++++++++------ apps/kitchen.yaml | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/apps/controller.py b/apps/controller.py index 9bcc2f5..d202671 100644 --- a/apps/controller.py +++ b/apps/controller.py @@ -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() - \ No newline at end of file + + +@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"])}') diff --git a/apps/kitchen.yaml b/apps/kitchen.yaml index ad3c9c4..3eb3a24 100644 --- a/apps/kitchen.yaml +++ b/apps/kitchen.yaml @@ -11,11 +11,40 @@ kitchen_motion: off_duration: 00:01:00 entities: - binary_sensor.motion_kitchen - # - binary_sensor.motion_bedroom kitchen_button: module: controller class: ControllerButton room: kitchen buttons: - - kitchen \ No newline at end of file + - kitchen + +kitchen_daylight: + module: controller + class: ControllerDaylight + entities: + - light.kitchen + latitude: 30.150380 + longitude: -97.4487 + periods: + - time: '04:00:00am' + brightness: 75 + color_temp: 200 + + - elevation: 0 + direction: rising + brightness: 150 + color_temp: 200 + + - time: noon + brightness: 255 + color_temp: 300 + + - elevation: 0 + direction: setting + brightness: 125 + color_temp: 325 + + - time: '10:30:00pm' + brightness: 75 + color_temp: 350 \ No newline at end of file