Skip to content

Commit

Permalink
Refactor ad_main some more
Browse files Browse the repository at this point in the history
  • Loading branch information
acockburn committed Aug 23, 2017
1 parent 87064e1 commit 1071d71
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 444 deletions.
103 changes: 8 additions & 95 deletions appdaemon/ad_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
from urllib.parse import urlparse
import yaml
import asyncio
import concurrent
import threading

import appdaemon.homeassistant as ha
import appdaemon.utils as ha
import appdaemon.appdaemon as ad
import appdaemon.appapi as appapi
import appdaemon.api as api
import appdaemon.appdash as appdash

Expand All @@ -42,117 +39,33 @@ def find_path(name):
# noinspection PyBroadException,PyBroadException
def run():

conf.appq = asyncio.Queue(maxsize=0)

first_time = True

conf.stopping = False

ha.log(conf.logger, "DEBUG", "Entering run()")

conf.loop = asyncio.get_event_loop()

# Save start time

conf.start_time = datetime.datetime.now()

# Take a note of DST

conf.was_dst = ad.is_dst()

# Setup sun

ad.update_sun()

conf.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
tasks = []

if conf.apps is True:
ha.log(conf.logger, "DEBUG", "Creating worker threads ...")

# Create Worker Threads
for i in range(conf.threads):
t = threading.Thread(target=ad.worker)
t.daemon = True
t.start()

ha.log(conf.logger, "DEBUG", "Done")
loop = asyncio.get_event_loop()


if conf.ha_url is not None:
# Read apps and get HA State before we start the timer thread
ha.log(conf.logger, "DEBUG", "Calling HA for initial state with key: {} and url: {}".format(conf.ha_key, conf.ha_url))

while conf.last_state is None:
try:
ad.get_ha_state()
conf.last_state = ha.get_now()
except:
ha.log(
conf.logger, "WARNING",
"Disconnected from Home Assistant, retrying in 5 seconds"
)
if conf.loglevel == "DEBUG":
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", "Unexpected error:")
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", traceback.format_exc())
ha.log(conf.logger, "WARNING", '-' * 60)
time.sleep(5)

ha.log(conf.logger, "INFO", "Got initial state")

# Initialize appdaemon loop
tasks.append(asyncio.async(ad.appdaemon_loop()))

else:
conf.last_state = ha.get_now()
# Initialize AppDaemon

if conf.apps is True:
# Load apps

# Let other parts know we are in business,
appapi.reading_messages = True

ha.log(conf.logger, "DEBUG", "Reading Apps")

ad.read_apps(True)

ha.log(conf.logger, "INFO", "App initialization complete")


# Create timer loop

# First, update "now" for less chance of clock skew error
if conf.realtime:
conf.now = datetime.datetime.now().timestamp()

ha.log(conf.logger, "DEBUG", "Starting timer loop")

tasks.append(asyncio.async(ad.appstate_loop()))

tasks.append(asyncio.async(ad.do_every(conf.tick, ad.do_every_second)))
appapi.reading_messages = True

ha.log(conf.logger, "INFO", "Starting Apps")
ad.run_ad(loop, tasks)
else:
ha.log(conf.logger, "INFO", "Apps are disabled")


# Initialize Dashboard/API

if conf.dashboard is True:
ha.log(conf.logger, "INFO", "Starting dashboard")
appdash.run_dash(conf.loop, tasks)
appdash.run_dash(loop, tasks)
else:
ha.log(conf.logger, "INFO", "Dashboards are disabled")

if conf.api_port is not None:
ha.log(conf.logger, "INFO", "Starting API")
api.run_api(conf.loop, tasks)
api.run_api(loop, tasks)
else:
ha.log(conf.logger, "INFO", "API is disabled")

conf.loop.run_until_complete(asyncio.wait(tasks))
loop.run_until_complete(asyncio.wait(tasks))

while not conf.stopping:
asyncio.sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion appdaemon/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import traceback

import appdaemon.conf as conf
import appdaemon.homeassistant as ha
import appdaemon.utils as ha

app = web.Application()

Expand Down
2 changes: 1 addition & 1 deletion appdaemon/appapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import copy
import iso8601

import appdaemon.homeassistant as ha
import appdaemon.utils as ha

reading_messages = False

Expand Down
94 changes: 93 additions & 1 deletion appdaemon/appdaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import appdaemon.appdash as appdash
import asyncio
import yaml
import concurrent
import threading

import appdaemon.homeassistant as ha
import appdaemon.utils as ha
import appdaemon.appapi as appapi

q = Queue(maxsize=0)
Expand Down Expand Up @@ -1276,3 +1278,93 @@ def appdaemon_loop():
yield from asyncio.sleep(5)

ha.log(conf.logger, "INFO", "Disconnecting from Home Assistant")

def run_ad(loop, tasks):
conf.appq = asyncio.Queue(maxsize=0)

conf.loop = loop

first_time = True

conf.stopping = False

ha.log(conf.logger, "DEBUG", "Entering run()")

# Save start time

conf.start_time = datetime.datetime.now()

# Take a note of DST

conf.was_dst = is_dst()

# Setup sun

update_sun()

conf.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)

ha.log(conf.logger, "DEBUG", "Creating worker threads ...")

# Create Worker Threads
for i in range(conf.threads):
t = threading.Thread(target=worker)
t.daemon = True
t.start()

ha.log(conf.logger, "DEBUG", "Done")


if conf.ha_url is not None:
# Read apps and get HA State before we start the timer thread
ha.log(conf.logger, "DEBUG", "Calling HA for initial state with key: {} and url: {}".format(conf.ha_key, conf.ha_url))

while conf.last_state is None:
try:
get_ha_state()
conf.last_state = ha.get_now()
except:
ha.log(
conf.logger, "WARNING",
"Disconnected from Home Assistant, retrying in 5 seconds"
)
if conf.loglevel == "DEBUG":
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", "Unexpected error:")
ha.log(conf.logger, "WARNING", '-' * 60)
ha.log(conf.logger, "WARNING", traceback.format_exc())
ha.log(conf.logger, "WARNING", '-' * 60)
time.sleep(5)

ha.log(conf.logger, "INFO", "Got initial state")

# Initialize appdaemon loop
tasks.append(asyncio.async(appdaemon_loop()))

else:
conf.last_state = ha.get_now()

# Load apps

# Let other parts know we are in business,
appapi.reading_messages = True

ha.log(conf.logger, "DEBUG", "Reading Apps")

read_apps(True)

ha.log(conf.logger, "INFO", "App initialization complete")


# Create timer loop

# First, update "now" for less chance of clock skew error
if conf.realtime:
conf.now = datetime.datetime.now().timestamp()

ha.log(conf.logger, "DEBUG", "Starting timer loop")

tasks.append(asyncio.async(appstate_loop()))

tasks.append(asyncio.async(do_every(conf.tick, do_every_second)))
appapi.reading_messages = True
2 changes: 1 addition & 1 deletion appdaemon/appdash.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import appdaemon.conf as conf
import appdaemon.dashboard as dashboard
import appdaemon.homeassistant as ha
import appdaemon.utils as ha

# Setup WS handler

Expand Down
2 changes: 1 addition & 1 deletion appdaemon/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pstats
import datetime

import appdaemon.homeassistant as ha
import appdaemon.utils as ha


class Dashboard:
Expand Down

0 comments on commit 1071d71

Please sign in to comment.