start refactoring the way ha api is called

This commit is contained in:
joBr99
2022-09-14 20:58:51 +02:00
parent b8871b3590
commit 2a1068f0a0
5 changed files with 119 additions and 114 deletions

View File

@@ -1,12 +1,14 @@
import datetime
import apis
from helper import scale, pos_to_color, rgb_dec565
from pages import LuiPagesGen
class LuiController(object):
def __init__(self, ha_api, config, send_mqtt_msg):
self._ha_api = ha_api
def __init__(self, config, send_mqtt_msg):
self._config = config
self._send_mqtt_msg = send_mqtt_msg
@@ -15,17 +17,17 @@ class LuiController(object):
# first card (default, after startup)
self._previous_cards.append(self._config.getCard(0))
self._pages_gen = LuiPagesGen(ha_api, config, send_mqtt_msg)
self._pages_gen = LuiPagesGen(config, send_mqtt_msg)
# send panel back to startup page on restart of this script
self._pages_gen.page_type("pageStartup")
# time update callback
time = datetime.time(0, 0, 0)
ha_api.run_minutely(self._pages_gen.update_time, time)
apis.ha_api.run_minutely(self._pages_gen.update_time, time)
# Setup date callback
ha_api.run_daily(self._pages_gen.update_date, time)
apis.ha_api.run_daily(self._pages_gen.update_date, time)
# register callbacks
self.register_callbacks()
@@ -37,31 +39,31 @@ class LuiController(object):
# register callbacks for each time
if type(self._config.get("sleepBrightness")) == list:
for index, timeset in enumerate(self._config.get("sleepBrightness")):
self._ha_api.run_daily(self.update_screensaver_brightness, timeset["time"], ssbr=timeset["value"], sbr=self.current_screen_brightness)
apis.ha_api.run_daily(self.update_screensaver_brightness, timeset["time"], ssbr=timeset["value"], sbr=self.current_screen_brightness)
# call update_screensaver_brightness on changes of entity configured in sleepTracking
bst = self._config.get("sleepTracking")
if bst is not None and self._ha_api.entity_exists(bst):
self._ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=bst)
if bst is not None and apis.ha_api.entity_exists(bst):
apis.ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=bst)
# call update_screensaver_brightness on entity configured in sleepOverride
sleepOverride = self._config.get("sleepOverride")
if sleepOverride is not None and type(sleepOverride) is dict and sleepOverride["entity"] is not None and sleepOverride["brightness"] is not None and self._ha_api.entity_exists(sleepOverride["entity"]):
self._ha_api.log(f"Configuring Sleep Override. Config is {sleepOverride}")
self._ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=sleepOverride["entity"])
if sleepOverride is not None and type(sleepOverride) is dict and sleepOverride["entity"] is not None and sleepOverride["brightness"] is not None and apis.ha_api.entity_exists(sleepOverride["entity"]):
apis.ha_api.log(f"Configuring Sleep Override. Config is {sleepOverride}")
apis.ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=sleepOverride["entity"])
# register callback for state changes on tracked value (for input_number) - sleepBrightness
sleep_brightness_config = self._config.get("sleepBrightness")
if type(sleep_brightness_config) == str and self._ha_api.entity_exists(sleep_brightness_config):
self._ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=sleep_brightness_config)
if type(sleep_brightness_config) == str and apis.ha_api.entity_exists(sleep_brightness_config):
apis.ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=sleep_brightness_config)
# register callback for state changes on tracked value (for input_number) - screenBrightness
screen_brightness_config = self._config.get("screenBrightness")
if type(screen_brightness_config) == str and self._ha_api.entity_exists(screen_brightness_config):
self._ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=screen_brightness_config)
if type(screen_brightness_config) == str and apis.ha_api.entity_exists(screen_brightness_config):
apis.ha_api.listen_state(self.update_screensaver_brightness_state_callback, entity_id=screen_brightness_config)
def startup(self):
self._ha_api.log(f"Startup Event")
apis.ha_api.log(f"Startup Event")
# send time and date on startup
self._pages_gen.update_time("")
self._pages_gen.update_date("")
@@ -94,12 +96,12 @@ class LuiController(object):
sleepBrightness = 0
brightness = self.calc_current_brightness(self._config.get("screenBrightness"))
if bst is not None and self._ha_api.entity_exists(bst) and self._ha_api.get_entity(bst).state in self._config.get("sleepTrackingZones"):
self._ha_api.log(f"sleepTracking setting brightness to 0")
if bst is not None and apis.ha_api.entity_exists(bst) and apis.ha_api.get_entity(bst).state in self._config.get("sleepTrackingZones"):
apis.ha_api.log(f"sleepTracking setting brightness to 0")
sleepBrightness = 0
elif sOEntity is not None and sOBrightness is not None and self._ha_api.entity_exists(sOEntity) and self._ha_api.get_entity(sOEntity).state in ["on", "true", "home"]:
self._ha_api.log(f"sleepOverride setting brightness to {sOBrightness}")
elif sOEntity is not None and sOBrightness is not None and apis.ha_api.entity_exists(sOEntity) and apis.ha_api.get_entity(sOEntity).state in ["on", "true", "home"]:
apis.ha_api.log(f"sleepOverride setting brightness to {sOBrightness}")
sleepBrightness = sOBrightness
else:
@@ -131,34 +133,34 @@ class LuiController(object):
if type(sleep_brightness_config) == int:
current_screensaver_brightness = sleep_brightness_config
elif type(sleep_brightness_config) == str:
current_screensaver_brightness = int(float(self._ha_api.get_state(sleep_brightness_config)))
current_screensaver_brightness = int(float(apis.ha_api.get_state(sleep_brightness_config)))
elif type(sleep_brightness_config) == list:
sorted_timesets = sorted(sleep_brightness_config, key=lambda d: self._ha_api.parse_time(d['time']))
sorted_timesets = sorted(sleep_brightness_config, key=lambda d: apis.ha_api.parse_time(d['time']))
# calc current screensaver brightness
found_current_dim_value = False
for i in range(len(sorted_timesets)):
found = self._ha_api.now_is_between(sorted_timesets[i-1]['time'], sorted_timesets[i]['time'])
found = apis.ha_api.now_is_between(sorted_timesets[i-1]['time'], sorted_timesets[i]['time'])
if found:
found_current_dim_value = True
current_screensaver_brightness = sorted_timesets[i-1]['value']
# still no dim value
if not found_current_dim_value:
self._ha_api.log("Chooseing %s as fallback", sorted_timesets[0])
apis.ha_api.log("Chooseing %s as fallback", sorted_timesets[0])
current_screensaver_brightness = sorted_timesets[0]["value"]
return current_screensaver_brightness
def register_callbacks(self):
items = self._config.get_all_entity_names()
self._ha_api.log(f"Registering callbacks for the following items: {items}")
apis.ha_api.log(f"Registering callbacks for the following items: {items}")
for item in items:
if self._ha_api.entity_exists(item):
self._ha_api.listen_state(self.state_change_callback, entity_id=item, attribute="all")
if apis.ha_api.entity_exists(item):
apis.ha_api.listen_state(self.state_change_callback, entity_id=item, attribute="all")
def state_change_callback(self, entity, attribute, old, new, kwargs):
self._ha_api.log(f"Got callback for: {entity}", level="DEBUG")
self._ha_api.log(f"Current page has the following items: {self._current_card.get_entity_names()}", level="DEBUG")
apis.ha_api.log(f"Got callback for: {entity}", level="DEBUG")
apis.ha_api.log(f"Current page has the following items: {self._current_card.get_entity_names()}", level="DEBUG")
if entity in self._current_card.get_entity_names():
self._ha_api.log(f"Callback Entity is on current page: {entity}", level="DEBUG")
apis.ha_api.log(f"Callback Entity is on current page: {entity}", level="DEBUG")
self._pages_gen.render_card(self._current_card, send_page_type=False)
# send detail page update, just in case
if self._current_card.cardType in ["cardGrid", "cardEntities"]:
@@ -179,16 +181,16 @@ class LuiController(object):
self._pages_gen.generate_fan_detail_page(entity_id)
def button_press(self, entity_id, button_type, value):
self._ha_api.log(f"Button Press Event; entity_id: {entity_id}; button_type: {button_type}; value: {value} ")
apis.ha_api.log(f"Button Press Event; entity_id: {entity_id}; button_type: {button_type}; value: {value} ")
# internal buttons
if entity_id == "screensaver" and button_type == "bExit":
# get default card if there is one
defaultCard = self._config.get("screensaver.defaultCard")
if defaultCard is not None:
defaultCard = self._ha_api.render_template(defaultCard)
self._ha_api.log(f"Searching for the following page as defaultPage: {defaultCard}")
defaultCard = apis.ha_api.render_template(defaultCard)
apis.ha_api.log(f"Searching for the following page as defaultPage: {defaultCard}")
dstCard = self._config.searchCard(defaultCard)
self._ha_api.log(f"Result for the following page as defaultPage: {dstCard}")
apis.ha_api.log(f"Result for the following page as defaultPage: {dstCard}")
if dstCard is not None:
self._previous_cards = []
self._previous_cards.append(dstCard)
@@ -231,37 +233,37 @@ class LuiController(object):
# buttons with actions on HA
if button_type == "OnOff":
if value == "1":
self._ha_api.turn_on(entity_id)
apis.ha_api.turn_on(entity_id)
else:
self._ha_api.turn_off(entity_id)
apis.ha_api.turn_off(entity_id)
if button_type == "number-set":
if entity_id.startswith('fan'):
entity = self._ha_api.get_entity(entity_id)
entity = apis.ha_api.get_entity(entity_id)
value = float(value)*float(entity.attributes.get("percentage_step", 0))
entity.call_service("set_percentage", percentage=value)
else:
self._ha_api.get_entity(entity_id).call_service("set_value", value=value)
apis.ha_api.get_entity(entity_id).call_service("set_value", value=value)
# for shutter / covers
if button_type == "up":
self._ha_api.get_entity(entity_id).call_service("open_cover")
apis.ha_api.get_entity(entity_id).call_service("open_cover")
if button_type == "stop":
self._ha_api.get_entity(entity_id).call_service("stop_cover")
apis.ha_api.get_entity(entity_id).call_service("stop_cover")
if button_type == "down":
self._ha_api.get_entity(entity_id).call_service("close_cover")
apis.ha_api.get_entity(entity_id).call_service("close_cover")
if button_type == "positionSlider":
pos = int(value)
self._ha_api.get_entity(entity_id).call_service("set_cover_position", position=pos)
apis.ha_api.get_entity(entity_id).call_service("set_cover_position", position=pos)
if button_type == "tiltOpen":
self._ha_api.get_entity(entity_id).call_service("open_cover_tilt")
apis.ha_api.get_entity(entity_id).call_service("open_cover_tilt")
if button_type == "tiltStop":
self._ha_api.get_entity(entity_id).call_service("stop_cover_tilt")
apis.ha_api.get_entity(entity_id).call_service("stop_cover_tilt")
if button_type == "tiltClose":
self._ha_api.get_entity(entity_id).call_service("close_cover_tilt")
apis.ha_api.get_entity(entity_id).call_service("close_cover_tilt")
if button_type == "tiltSlider":
pos = int(value)
self._ha_api.get_entity(entity_id).call_service("set_cover_tilt_position", position=pos)
apis.ha_api.get_entity(entity_id).call_service("set_cover_tilt_position", position=pos)
if button_type == "button":
@@ -277,92 +279,92 @@ class LuiController(object):
self._current_card = dstCard
self._pages_gen.render_card(self._current_card)
else:
self._ha_api.log(f"No page with key {entity_id} found")
apis.ha_api.log(f"No page with key {entity_id} found")
elif entity_id.startswith('scene'):
self._ha_api.get_entity(entity_id).call_service("turn_on")
apis.ha_api.get_entity(entity_id).call_service("turn_on")
elif entity_id.startswith('script'):
self._ha_api.get_entity(entity_id).call_service("turn_on")
apis.ha_api.get_entity(entity_id).call_service("turn_on")
elif entity_id.startswith('light') or entity_id.startswith('switch') or entity_id.startswith('input_boolean') or entity_id.startswith('automation') or entity_id.startswith('fan'):
self._ha_api.get_entity(entity_id).call_service("toggle")
apis.ha_api.get_entity(entity_id).call_service("toggle")
elif entity_id.startswith('lock'):
if self._ha_api.get_entity(entity_id).state == "locked":
self._ha_api.get_entity(entity_id).call_service("unlock")
if apis.ha_api.get_entity(entity_id).state == "locked":
apis.ha_api.get_entity(entity_id).call_service("unlock")
else:
self._ha_api.get_entity(entity_id).call_service("lock")
apis.ha_api.get_entity(entity_id).call_service("lock")
elif entity_id.startswith('button') or entity_id.startswith('input_button'):
self._ha_api.get_entity(entity_id).call_service("press")
apis.ha_api.get_entity(entity_id).call_service("press")
elif entity_id.startswith('input_select'):
self._ha_api.get_entity(entity_id).call_service("select_next")
apis.ha_api.get_entity(entity_id).call_service("select_next")
elif entity_id.startswith('vacuum'):
if self._ha_api.get_entity(entity_id).state == "docked":
self._ha_api.get_entity(entity_id).call_service("start")
if apis.ha_api.get_entity(entity_id).state == "docked":
apis.ha_api.get_entity(entity_id).call_service("start")
else:
self._ha_api.get_entity(entity_id).call_service("return_to_base")
apis.ha_api.get_entity(entity_id).call_service("return_to_base")
elif entity_id.startswith('service'):
self._ha_api.call_service(entity_id.replace('service.', '', 1).replace('.','/', 1), **le.data)
apis.ha_api.call_service(entity_id.replace('service.', '', 1).replace('.','/', 1), **le.data)
# for media page
if button_type == "media-next":
self._ha_api.get_entity(entity_id).call_service("media_next_track")
apis.ha_api.get_entity(entity_id).call_service("media_next_track")
if button_type == "media-back":
self._ha_api.get_entity(entity_id).call_service("media_previous_track")
apis.ha_api.get_entity(entity_id).call_service("media_previous_track")
if button_type == "media-pause":
self._ha_api.get_entity(entity_id).call_service("media_play_pause")
apis.ha_api.get_entity(entity_id).call_service("media_play_pause")
if button_type == "media-OnOff":
if self._ha_api.get_entity(entity_id).state == "off":
self._ha_api.get_entity(entity_id).call_service("turn_on")
if apis.ha_api.get_entity(entity_id).state == "off":
apis.ha_api.get_entity(entity_id).call_service("turn_on")
else:
self._ha_api.get_entity(entity_id).call_service("turn_off")
apis.ha_api.get_entity(entity_id).call_service("turn_off")
if button_type == "volumeSlider":
pos = int(value)
# HA wants this value between 0 and 1 as float
pos = pos/100
self._ha_api.get_entity(entity_id).call_service("volume_set", volume_level=pos)
apis.ha_api.get_entity(entity_id).call_service("volume_set", volume_level=pos)
if button_type == "speaker-sel":
self._ha_api.get_entity(entity_id).call_service("select_source", source=value)
apis.ha_api.get_entity(entity_id).call_service("select_source", source=value)
# for light detail page
if button_type == "brightnessSlider":
# scale 0-100 to ha brightness range
brightness = int(scale(int(value),(0,100),(0,255)))
self._ha_api.get_entity(entity_id).call_service("turn_on", brightness=brightness)
apis.ha_api.get_entity(entity_id).call_service("turn_on", brightness=brightness)
if button_type == "colorTempSlider":
entity = self._ha_api.get_entity(entity_id)
entity = apis.ha_api.get_entity(entity_id)
#scale 0-100 from slider to color range of lamp
color_val = scale(int(value), (0, 100), (entity.attributes.min_mireds, entity.attributes.max_mireds))
self._ha_api.get_entity(entity_id).call_service("turn_on", color_temp=color_val)
apis.ha_api.get_entity(entity_id).call_service("turn_on", color_temp=color_val)
if button_type == "colorWheel":
self._ha_api.log(value)
apis.ha_api.log(value)
value = value.split('|')
color = pos_to_color(int(value[0]), int(value[1]), int(value[2]))
self._ha_api.log(color)
self._ha_api.get_entity(entity_id).call_service("turn_on", rgb_color=color)
apis.ha_api.log(color)
apis.ha_api.get_entity(entity_id).call_service("turn_on", rgb_color=color)
# for climate page
if button_type == "tempUpd":
temp = int(value)/10
self._ha_api.get_entity(entity_id).call_service("set_temperature", temperature=temp)
apis.ha_api.get_entity(entity_id).call_service("set_temperature", temperature=temp)
if button_type == "tempUpdHighLow":
value = value.split("|")
temp_high = int(value[0])/10
temp_low = int(value[1])/10
self._ha_api.get_entity(entity_id).call_service("set_temperature", target_temp_high=temp_high, target_temp_low=temp_low)
apis.ha_api.get_entity(entity_id).call_service("set_temperature", target_temp_high=temp_high, target_temp_low=temp_low)
if button_type == "hvac_action":
self._ha_api.get_entity(entity_id).call_service("set_hvac_mode", hvac_mode=value)
apis.ha_api.get_entity(entity_id).call_service("set_hvac_mode", hvac_mode=value)
# for alarm page
if button_type in ["disarm", "arm_home", "arm_away", "arm_night", "arm_vacation"]:
self._ha_api.get_entity(entity_id).call_service(f"alarm_{button_type}", code=value)
apis.ha_api.get_entity(entity_id).call_service(f"alarm_{button_type}", code=value)
if button_type == "opnSensorNotify":
msg = ""
entity = self._ha_api.get_entity(entity_id)
entity = apis.ha_api.get_entity(entity_id)
if "open_sensors" in entity.attributes and entity.attributes.open_sensors is not None:
for e in entity.attributes.open_sensors:
msg += f"- {self._ha_api.get_entity(e).attributes.friendly_name}\r\n"
msg += f"- {apis.ha_api.get_entity(e).attributes.friendly_name}\r\n"
self._pages_gen.send_message_page("opnSensorNotifyRes", "", msg, "", "")
# for fan popup / preset selection
if button_type == "mode-sel":
entity = self._ha_api.get_entity(entity_id)
entity = apis.ha_api.get_entity(entity_id)
preset_mode = entity.attributes.preset_modes[int(value)]
entity.call_service("set_preset_mode", preset_mode=preset_mode)