Update mqtt-hass-sync.py
This commit is contained in:
parent
87c18128f9
commit
ab69f75120
|
@ -3,28 +3,25 @@ import appdaemon.plugins.mqtt.mqttapi as mqtt
|
||||||
import appdaemon.adbase as base
|
import appdaemon.adbase as base
|
||||||
|
|
||||||
|
|
||||||
class hass_mqtt_state_sync(base.ADBase):
|
class hass_mqtt_state_sync(hass.Hass,mqtt.Mqtt):
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.hass = self.get_plugin_api("HASS")
|
|
||||||
self.mqtt = self.get_plugin_api("MQTT")
|
|
||||||
self.ad = self.get_ad_api()
|
|
||||||
|
|
||||||
self.entity_id = self.args["entity_id"]
|
self.entity_id = self.args["entity_id"]
|
||||||
self.entity = self.hass.get_entity(self.entity_id)
|
self.entity = self.get_entity(self.entity_id)
|
||||||
self.mqtt_topic_state = self.args["mqtt_topic_state"]
|
self.mqtt_topic_state = self.args["mqtt_topic_state"]
|
||||||
self.mqtt_topic_set_state = self.args["mqtt_topic_set_state"]
|
self.mqtt_topic_set_state = self.args["mqtt_topic_set_state"]
|
||||||
self.mqtt_topic_request = self.args["mqtt_topic_request"]
|
self.mqtt_topic_request = self.args["mqtt_topic_request"]
|
||||||
|
|
||||||
self.mqtt.mqtt_subscribe(self.mqtt_topic_set_state)
|
self.mqtt_subscribe(self.mqtt_topic_set_state, namespace="mqtt")
|
||||||
self.mqtt.mqtt_subscribe(self.mqtt_topic_request)
|
self.mqtt_subscribe(self.mqtt_topic_request, namespace="mqtt")
|
||||||
self.mqtt.listen_event(self.handle_set_state_payload, "MQTT_MESSAGE", topic=self.mqtt_topic_set_state)
|
self.listen_event(self.handle_set_state_payload, "MQTT_MESSAGE", topic=self.mqtt_topic_set_state, namespace="mqtt")
|
||||||
self.mqtt.listen_event(self.handle_request_payload, "MQTT_MESSAGE", topic=self.mqtt_topic_request)
|
self.listen_event(self.handle_request_payload, "MQTT_MESSAGE", topic=self.mqtt_topic_request, namespace="mqtt")
|
||||||
self.entity.listen_state(self.handle_hass_state_change)
|
self.entity.listen_state(self.handle_hass_state_change)
|
||||||
|
self.publish_state_to_mqtt()
|
||||||
|
|
||||||
def publish_state_to_mqtt(self):
|
def publish_state_to_mqtt(self):
|
||||||
state = self.hass.get_state(self.entity_id)
|
state = self.get_state(self.entity_id)
|
||||||
self.mqtt.mqtt_publish(self.mqtt_topic_state, str(state))
|
self.mqtt_publish(self.mqtt_topic_state, str(state), namespace="mqtt")
|
||||||
|
|
||||||
def handle_set_state_payload(self, event_name, data, cb_args):
|
def handle_set_state_payload(self, event_name, data, cb_args):
|
||||||
self.entity.set_state(state = data["payload"])
|
self.entity.set_state(state = data["payload"])
|
||||||
|
@ -33,5 +30,5 @@ class hass_mqtt_state_sync(base.ADBase):
|
||||||
self.publish_state_to_mqtt()
|
self.publish_state_to_mqtt()
|
||||||
|
|
||||||
def handle_hass_state_change(self, entity, attribute, old, new, cb_args):
|
def handle_hass_state_change(self, entity, attribute, old, new, cb_args):
|
||||||
self.ad.log("State change detected: {} -> {}".format(old, new))
|
self.log("State change detected: {} -> {}".format(old, new))
|
||||||
self.publish_state_to_mqtt()
|
self.publish_state_to_mqtt()
|
Loading…
Reference in New Issue