Last active
May 30, 2021 11:41
-
-
Save lmeulen/d678eb34b16d3783ed41f4fc81420342 to your computer and use it in GitHub Desktop.
Screens_mainloop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tahoma, utils, logging, datetime | |
CONFIG_FILE = 'systems.cred' | |
STATUS_FILE = "status_screens.dat" | |
COUNT_FILE = "status_screens_count.dat" | |
COUNT_FILE_OPENNOW = "status_screens_count_opennow.dat" | |
DELAY_MINUTES = 7 | |
def run(config=None): | |
logger = logging.getLogger('home automation') | |
logger.debug("Script: Control screens") | |
screens = tahoma.Screens(config.get('ifttt_key')) | |
sunrise, sunset = utils.get_sunrise_and_sunset() | |
# Replace sunrise and sunset placeholders with correct time | |
for s in config['screens']: | |
if s['close'] == 'sunrise': | |
s['close'] = sunrise.strftime("%H:%M") | |
elif s['open'] == 'sunset': | |
s['open'] = sunset.strftime("%H:%M") | |
# Get status indicating a forced opening of screens is underway. REset during night | |
force_opened = False if utils.is_now("04:00") else utils.get_current_status(STATUS_FILE) | |
utils.save_current_state(STATUS_FILE, force_opened) | |
pred_temp, pred_clouds, _ = utils.get_weather_forecast() | |
t, h, press, wb, wind_speed, uv, radiation, precip, cloudperc, cloudperc2, \ | |
raining, thunderstorm, _ = utils.get_current_weather() | |
close_today = determine_close_today(pred_clouds, pred_temp) | |
open_now, no_delay = determine_open_now(t, h, cloudperc, cloudperc2, raining, thunderstorm, | |
wind_speed, radiation, precip, press, logger) | |
open_now = delay(open_now, no_delay, DELAY_MINUTES, COUNT_FILE_OPENNOW) | |
else: | |
open_now = False | |
if close_today: | |
if open_now and not force_opened: | |
logger.info('Condition: Open due to weather') | |
# Force open due to weather conditions | |
screens.open_all() | |
utils.save_current_state(STATUS_FILE, True) | |
utils.save_current_count(COUNT_FILE, 0) | |
elif force_opened and not open_now: | |
logger.debug('Conditions improved, checking to reclose') | |
cnt = utils.get_current_count(COUNT_FILE) | |
if cnt > DELAY_MINUTES: | |
logger.debug('Condition: Re close due to weather') | |
# Reclose screens according to schedule | |
utils.save_current_state(STATUS_FILE, False) | |
for s in config['screens']: | |
if utils.is_after_or_equal(s['close']) & utils.is_before(s['open']): | |
logger.info('Re closing screen : ' + s['screen']) | |
screens.dicht_by_name(s['screen']) | |
else: | |
logger.debug('Waiting ... ' + str(cnt)) | |
utils.save_current_count(COUNT_FILE, cnt+1) | |
elif open_now: | |
logger.debug('Keeping open due to weather circumstances') | |
utils.save_current_count(COUNT_FILE, 0) | |
elif not open_now and not force_opened: | |
logger.debug('Condition: Following schedule') | |
# Follow schedule from settings | |
for s in config['screens']: | |
logger.debug(s) | |
if utils.is_now(s['close']): | |
logger.info('Closing screen : ' + s['screen'] + ' at ' + s['close']) | |
screens.dicht_by_name(s['screen']) | |
elif utils.is_now(s['open']): | |
logger.info('Opening screen : ' + s['screen'] + ' at ' + s['open']) | |
screens.open_by_name(s['screen']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment