Skip to content

Instantly share code, notes, and snippets.

@oki
Last active October 20, 2022 18:38
Show Gist options
  • Save oki/2c7d90a139261e7f3934d12b1dbb60fb to your computer and use it in GitHub Desktop.
Save oki/2c7d90a139261e7f3934d12b1dbb60fb to your computer and use it in GitHub Desktop.
---
# secrets: secrets.yaml
appdaemon:
latitude: 1.1
longitude: 1.2
elevation: 3
time_zone: Europe/Amsterdam
plugins:
HASS:
type: hass
MQTT:
type: mqtt
namespace: mqtt # needed
verbose: True
client_id: appdaemon
client_host: 192.168.XX.XX
client_port: 4105
client_user: XXX
client_password: XXX
event_name: MQTT_MESSAGE # the event you will listen in your module
client_topics: NONE
http:
url: http://127.0.0.1:5050
admin:
api:
hadashboard:
boneio_switch:
module: boneio_switch
class: BoneioSwitch
esp_endpoint: "http://192.168.XXX.XXX/"
sleep_time: 0.3
topic_wildcard: "boneio1-dummy/#"
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")
- platform: mqtt
unique_id: light.work1
name: "Work 1"
state_topic: "boneio1/binary_sensor/boneio_in_12/state"
command_topic: "boneio1-dummy/switch/relay_02/command"
payload_on: "ON"
payload_off: "OFF"
retain: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment