Skip to content

Instantly share code, notes, and snippets.

@lrodorigo
Created May 23, 2023 14:39
Show Gist options
  • Save lrodorigo/5883e1d39c211d246064d59e645fd14f to your computer and use it in GitHub Desktop.
Save lrodorigo/5883e1d39c211d246064d59e645fd14f to your computer and use it in GitHub Desktop.
import datetime
import pathlib
import hassapi as hass
import yaml
from appdaemon.appdaemon import AppDaemon
SLEEP = "sleep"
AWAY = "away"
AT_HOME = "at_home"
HOLIDAY = "holiday"
MODES = (SLEEP, AWAY, AT_HOME, HOLIDAY)
COOLING = 'cooling'
HEATING = 'heating'
BAD_STATES = ["unavailable", "unknown", {}]
STATES_EQUALS_TRUE = ('on', '1', 'true')
ALARMO_CONTROL_PANEL = "alarm_control_panel.alarmo"
class BaseHandler(hass.Hass):
def __init__(self, ad: AppDaemon, name, logging, args, config, app_config, global_vars):
super().__init__(ad, name, logging, args, config, app_config, global_vars)
with open(f"{pathlib.Path(__file__).parent.resolve()}/house_config.yaml") as f:
self.config = yaml.safe_load(f)
self.house_mode_input_name = self.config["house_mode_input"]
self.thermal_mode_input_name = self.config["thermal_mode_input"]
@property
def house_mode(self):
return self.get_state(self.house_mode_input_name)
@property
def alarm_state(self):
return self.get_state(ALARMO_CONTROL_PANEL)
def is_dark(self):
now = self.datetime()
sunset = self.sunset()
sunrise = self.sunrise()
there_is_light = now.time() > sunrise.time() and now.time() < sunset.time()
return not there_is_light
def move_to(self, state):
if state not in state:
raise ValueError("Bad State")
self.set_state("input_select.house_mode", state=state)
def someone_at_home(self):
return self.house_mode in (AT_HOME, SLEEP)
def _on_house_mode_changed(self, entity, attribute, old, new, kwargs):
self.log(f"House Mode changed from {old} to {new}")
self.on_house_mode_changed(entity, attribute, old, new, kwargs)
def on_house_mode_changed(self, entity, attribute, old, new, kwargs):
pass
def register_house_mode_change(self):
self.listen_state(self._on_house_mode_changed, self.house_mode_input_name)
def create_entity(self, entity_id, initial_state, **kwargs):
self.set_state(entity=entity_id, state=initial_state)
def set_state_preserving_attributes(self, entity, state, attributes_update={}):
old = self.get_state(entity, attribute='all')
updated_attributes = old.get('attributes', {})
updated_attributes.update(attributes_update)
if "binary_sensor" in entity:
if state is not None:
state = "on" if state else "off"
else:
state = "unavailable"
self.set_state(entity, state=state, attributes=updated_attributes)
def is_state_bad(self, state):
return state in BAD_STATES
def set_switch(self, entity, state):
if state:
self.turn_on(entity)
else:
self.turn_off(entity)
def set_switches(self, entities, state):
for e in entities:
self.set_switch(e, state)
def get_boolean_state(self, entity, raise_on_missing=True, default=False):
state = self.get_state(entity)
if not state:
if raise_on_missing:
raise AttributeError(f"Missing entity {entity} - failed to get boolean state")
else:
return default
return state.lower() in STATES_EQUALS_TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment