How to use the appdaemon.homeassistant.get_now function in appdaemon

To help you get started, we’ve selected a few appdaemon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github home-assistant / appdaemon / appdaemon / appdaemon.py View on Github external
def today_is_constrained(days):
    day = ha.get_now().weekday()
    daylist = [ha.day_of_week(day) for day in days.split(",")]
    if day in daylist:
        return False
    return True
github home-assistant / appdaemon / appdaemon / appdaemon.py View on Github external
global was_dst
    global last_state

    # Lets check if we are connected, if not give up.
    if not reading_messages:
        return
    try:

        # now = datetime.datetime.now()
        # now = now.replace(microsecond=0)
        now = datetime.datetime.fromtimestamp(utc)
        conf.now = utc

        # If we have reached endtime bail out

        if conf.endtime is not None and ha.get_now() >= conf.endtime:
            ha.log(conf.logger, "INFO", "End time reached, exiting")
            sys.exit(0)

        if conf.realtime:
            real_now = datetime.datetime.now().timestamp()
            delta = abs(utc - real_now)
            if delta > 1:
                ha.log(
                    conf.logger, "WARNING",
                    "Scheduler clock skew detected -"
                    " delta = {} - resetting".format(delta)
                )
                return real_now

        # Update sunrise/sunset etc.
github home-assistant / appdaemon / appdaemon / appdaemon.py View on Github external
def update_sun():
    # now = datetime.datetime.now(conf.tz)
    now = conf.tz.localize(ha.get_now())
    mod = -1
    while True:
        try:
            next_rising_dt = conf.location.sunrise(
                now + datetime.timedelta(days=mod), local=False
            )
            if next_rising_dt > now:
                break
        except astral.AstralError:
            pass
        mod += 1

    mod = -1
    while True:
        try:
            next_setting_dt = conf.location.sunset(
github home-assistant / appdaemon / appdaemon / appdaemon.py View on Github external
# 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")

    # Read apps and get HA State before we start the timer thread
    ha.log(conf.logger, "DEBUG", "Calling HA for initial state")

    while last_state is None:
        try:
            get_ha_state()
            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")
    # Load apps
github home-assistant / appdaemon / appdaemon / appdaemon.py View on Github external
t.start()

    # Enter main loop

    first_time = True
    reading_messages = True

    _id = 0

    while not stopping:
        _id += 1
        try:
            if first_time is False:
                # Get initial state
                get_ha_state()
                last_state = ha.get_now()
                ha.log(conf.logger, "INFO", "Got initial state")

                # Let the timer thread know we are in business,
                # and give it time to tick at least once
                reading_messages = True
                if conf.apps is True:
                    time.sleep(2)

                # Load apps
                if conf.apps is True:
                    read_apps(True)

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

            #
            # Fire HA_STARTED and APPD_STARTED Events