import appdaemon.plugins.hass.hassapi as hass import requests import re from time import sleep class BoneioSwitch(hass.Hass): def initialize(self): # self.log( self.args["topic_wildcard"] ) self.mqtt = self.get_plugin_api("MQTT") self.mqtt.mqtt_subscribe(topic=self.args["topic_wildcard"]) self.mqtt.listen_event(self.mqtt_message_received_event, "MQTT_MESSAGE", wildcard=self.args["topic_wildcard"]) def mqtt_message_received_event(self, event, data, kwargs): # self.log("mqtt_message_received_event!") # self.log(data) match = re.search('(switch/[\w+_]+)', data["topic"]) if match: base_url = self.args["esp_endpoint"] url = f"{base_url}{match.group(1)}/turn_" self.log(url) requests.post(f"{url}on", "true") sleep(self.args["sleep_time"]) requests.post(f"{url}off", "true") else: self.log("Unable to extract switch name")