Skip to content

Commit

Permalink
Publish an update for new subscribers
Browse files Browse the repository at this point in the history
Using the Mosquitto's subscriber log.
#20
  • Loading branch information
deiger authored Jun 1, 2020
1 parent d348a6c commit 2f2ca59
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions hisense.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ class Data:
properties: Properties
properties_lock = threading.Lock()

def get_property(self, name: str):
"""Get a stored property."""
with self.properties_lock:
return getattr(self.properties, name)

def update_property(self, name: str, value) -> None:
"""Update the stored properties, if changed."""
with self.properties_lock:
Expand Down Expand Up @@ -672,13 +677,26 @@ def _write_json(self, data: dict) -> None:


def mqtt_on_connect(client: mqtt.Client, userdata, flags, rc):
for data_field in fields(_data.properties):
client.subscribe(_mqtt_topics['sub'].format(data_field.name))
client.subscribe([(_mqtt_topics['sub'].format(data_field.name), 0)
for data_field in fields(_data.properties)])
# Subscribe to subscription updates.
client.subscribe('$SYS/broker/log/M/subscribe/#')


def mqtt_on_subscribe(payload: bytes):
# The last segment in the space delimited string is the topic.
topic = payload.decode('utf-8').rsplit(' ', 1)[-1]
if topic not in _mqtt_topics['pub']:
return
name = topic.rsplit('/', 2)[1]
mqtt_publish_update(name, _data.get_property(name))


def mqtt_on_message(client: mqtt.Client, userdata, message: mqtt.MQTTMessage):
logging.info('MQTT message Topic: %r, Payload %r',
message.topic, message.payload)
if message.topic.startswith('$SYS/broker/log/M/subscribe'):
return mqtt_on_subscribe(message.payload)
name = message.topic.rsplit('/', 2)[1]
payload = message.payload.decode('utf-8')
if name == 't_work_mode' and payload == 'fan_only':
Expand Down

0 comments on commit 2f2ca59

Please sign in to comment.