Exemplo n.º 1
0
def scriptUnloaded():
    """Cancel all Timers at unload to avoid errors in the log and removes the
    rules.
    """

    timers.cancel_all()
    delete_rule(ephem_tod, log)
    delete_rule(load_etod, log)
Exemplo n.º 2
0
def scriptUnloaded():
    """
    Cancels all the timers when the script is unloaded to avoid timers from
    hanging around and deletes the rules.
    """

    timers.cancel_all()
    delete_rule(load_debounce, log)
    delete_rule(debounce, log)
Exemplo n.º 3
0
def scriptLoaded(*args):
    """Create the Ephemeris Time of Day rule."""

    delete_rule(ephem_tod, log)
    if create_simple_rule(
            ETOD_RELOAD_ITEM,
            "Reload Ephemeris Time of Day",
            load_etod,
            log,
            description=(
                "Regenerates the Ephemeris Time of Day rule using the"
                " latest {0} metadata. Run after adding or removing any"
                " {0} metadata to/from and Item.".format(NAMESPACE)),
            tags=["openhab-rules-tools", "etod"]):
        load_etod(None)
Exemplo n.º 4
0
def load_debounce(event):
    """Called at startup or when the Reload Debounce rule is triggered. It
    deletes and recreates the Debounce rule. Should be called at startup and
    when the metadata is changes on Items since there is no event to do this
    automatically.
    """

    if not delete_rule(debounce, log):
        log.error("Failed to delete rule!")
        return

    debounce_items = load_rule_with_metadata(
        "debounce",
        get_config,
        "changed",
        "Debounce",
        debounce,
        log,
        description=("Delays updating a proxy Item until the configured "
                     "Item remains in that state for the configured amount "
                     "of time"),
        tags=["openhab-rules-tools", "debounce"])

    if debounce_items:
        # Cancel any existing timers for Items that are no longer debounced.
        # Leave the timers for those that are still debounced.
        for i in [i for i in timers.timers if not i in debounce_items]:
            timers.cancel(i)

        # Synchronize the debounce_item with it's configured proxy.
        log.debug("debounce_items = {}".format(debounce_items))
        for i in debounce_items:
            cfg = get_config(i, log)
            if cfg:
                post_update_if_different(cfg.value, items[i].toString())
Exemplo n.º 5
0
def load_etod(event):
    """Called at startup or when the Reload Ephemeris Time of Day rule is
    triggered, deletes and recreates the Ephemeris Time of Day rule. Should be
    called at startup and when the metadata is added to or removed from Items.
    """

    # Remove the existing rule if it exists.
    if not delete_rule(ephem_tod, log):
        log.error("Failed to delete rule!")
        return None

    # Generate the rule triggers with the latest metadata configs.
    etod_items = load_rule_with_metadata(
        NAMESPACE,
        check_config,
        "changed",
        "Ephemeris Time of Day",
        ephem_tod,
        log,
        description=("Creates the timers that "
                     "drive the {} state"
                     "machine".format(ETOD_ITEM)),
        tags=["openhab-rules-tools", "etod"])
    if etod_items:
        for i in [i for i in timers.timers if not i in etod_items]:
            timers.cancel(i)

    # Generate the timers now.
    ephem_tod(None)
Exemplo n.º 6
0
def mqtt_eb_pub(event):
    """Called when a configured Item is updated or commanded and publsihes the
    event to the event bus.
    """

    if not check_config(mqtt_eb_pub.log):
        init_logger.error("Cannot publish event bus event, deleting rule")
        delete_rule(mqtt_eb_pub, init_logger)
        return

    from configuration import mqtt_eb_name, mqtt_eb_broker

    is_cmd = hasattr(event, 'itemCommand')
    msg = "{}".format(event.itemCommand if is_cmd else event.itemState)
    topic = "{}/out/{}/{}".format(mqtt_eb_name, event.itemName,
                                  "command" if is_cmd else "state")
    retained = False if is_cmd else True
    init_logger.info("Publishing {} to  {} on {} with retained {}"
                     .format(msg, topic, mqtt_eb_broker, retained))
    action = actions.get("mqtt", mqtt_eb_broker)
    if action:
        action.publishMQTT(topic, msg, retained)
    else:
        init_logger.error("There is no broker Thing {}!".format(mqtt_eb_broker))
Exemplo n.º 7
0
def load_online():
    """Loads the online status publishing rule."""

    # Delete the old online rule.
    if not delete_rule(online, init_logger):
        init_logger("Failed to delete rule!")
        return False

    triggers = ["System started"]

    if not create_rule("MQTT Event Bus Online", triggers, online, init_logger,
                       description=("Publishes ONLINE to the configured LWT "
                                    "topic."),
                       tags=["openhab-rules-tools","mqtt_eb"]):
        init_logger.error("Failed to create MQTT Event Bus Online rule!")
Exemplo n.º 8
0
def load_mqtt_eb_sub(event):
    """Deletes and recreates the MQTT Event Bus subscription rule."""

    # Delete the old rule
    delete_rule(mqtt_eb_sub, init_logger)

    # Reload to get the latest subscription channel
    import configuration
    reload(configuration)
    try:
        from configuration import mqtt_eb_in_chan
    except:
        load_mqtt_eb_sub.log.error("mqtt_eb_in_chan is not defined in "
                                   "configuration.py")
        return

    # Add the trigger
    triggers = ["Channel {} triggered".format(mqtt_eb_in_chan)]
    if not create_rule("MQTT Event Bus Subscription", triggers, mqtt_eb_sub,
            load_mqtt_eb_sub.log,
            description=("Triggers by an MQTT event Channel and updates or "
                         "commands based on the topic the message came from"),
            tags=["openhab-rules-tools","mqtt_eb"]):
        load_mqtt_eb_sub.log.error("Failed to create MQTT Event Bus Subscription!")
Exemplo n.º 9
0
def load_publisher():

    # Delete the old publisher rule.
    if not delete_rule(mqtt_eb_pub, init_logger):
        init_logger("Failed to delete rule!")
        return False

    # Default to publishing all updates and all commands for all Items.
    puball = True
    try:
        from configuration import mqtt_eb_puball
        puball = mqtt_eb_puball
    except:
        init_logger.warn("No mqtt_eb_puball in configuration.py, "
                                  "defaulting to publishing all Items")

    # Don't bother to create the rule if we can't use it.
    if not check_config(init_logger):
        init_logger.error("Cannot create MQTT event bus publication rule!")
        return False

    triggers = []

    # Create triggers for Items.
    update_items = items if puball else [i for i in items if ir.getItem(i).getTags().contains("eb_update")]
    command_items = items if puball else [i for i in items if ir.getItem(i).getTags().contains("eb_command")]
    for i in update_items:
        triggers.append("Item {} received update".format(i))
    for i in command_items:
        triggers.append("Item {} received command".format(i))

    # No triggers, no need for the rule.
    if not triggers:
        init_logger.warn("No event bus Items found")
        return False

    # Create the rule to publish the events.
    if not create_rule("MQTT Event Bus Publisher", triggers, mqtt_eb_pub,
                       init_logger,
                       description=("Publishes updates and commands on "
                                    "configured Items to the configured "
                                    "event bus topics"),
                       tags=["openhab-rules-tools","mqtt_eb"]):
        init_logger.error("Failed to create MQTT Event Bus Publisher!")
        return False

    return True
Exemplo n.º 10
0
def scriptUnloaded():
    """Cancel all the timers."""

    deferred.cancel_all()
    delete_rule(expire_event, init_logger)
    delete_rule(load_expire, init_logger)
Exemplo n.º 11
0
def scriptUnloaded():
    """Deletes the MQTT Event Bus Publisher and Online rules and the reload rule."""

    delete_rule(load_mqtt_eb_pub, init_logger)
    delete_rule(mqtt_eb_pub, init_logger)
Exemplo n.º 12
0
def scriptUnloaded():
    """Deletes the MQTT Event Bus Subscription rule and the reload rule."""

    delete_rule(load_mqtt_eb_sub, init_logger)
    delete_rule(mqtt_eb_sub, init_logger)