From 84fcb20f49e5930ca4bf2d192c706c1bb0649f1f Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 15 May 2020 19:06:31 -0400 Subject: [PATCH] send lixie_clock config upon boot --- models.py | 18 ++++++++++++------ mqtt.py | 6 +----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/models.py b/models.py index 8fc14f0..ba7d6e8 100644 --- a/models.py +++ b/models.py @@ -4,11 +4,10 @@ Data models for different device types managed by the Juice IOT hub. """ import re import json +import requests import tools -import requests - class Network(list): """ Represents a network of IOT devices. @@ -166,7 +165,7 @@ class LightStrip(Device): animation_rotate_count = 1, animation_delay = 200, - async def mqtt_callback(self, app, msg): + def mqtt_callback(self, app, msg): topic = msg.topic.split('/') payload = msg.payload.decode('utf8') @@ -236,7 +235,7 @@ class LightStrip(Device): save_network(network) res = {'event': 'neopixel', 'data': data} - await app.send_json_all(res) + app.loop.create_task(app.send_json_all(res)) class NeoPixel(SubDevice): @@ -261,7 +260,7 @@ class LixieClock(Device): self.time_offset = -5 self.display_number = 0 - async def mqtt_callback(self, app, msg): + def mqtt_callback(self, app, msg): topic = msg.topic.split('/') payload = msg.payload.decode('utf8') @@ -288,10 +287,17 @@ class LixieClock(Device): data['change_mode'] = 'time_offset' data['time_offset'] = int(payload) self.time_offset = int(payload) + elif topic[1] == 'helo': + app['mqtt'].publish(topic[0] + '/' + self.display_mode, '') + if self.display_mode == 'number': + app['mqtt'].publish(topic[0] + '/number', self.display_number) + app['mqtt'].publish(topic[0] + '/time_offset', self.time_offset) + color = ','.join([str(n) for n in self.color]) + app['mqtt'].publish(topic[0] + '/color', color) save_network(network) res = {'event': 'lixie_clock', 'data': data} - await app.send_json_all(res) + app.loop.create_task(app.send_json_all(res)) class LixieDisplay(SubDevice): diff --git a/mqtt.py b/mqtt.py index a96453f..0c62f4f 100644 --- a/mqtt.py +++ b/mqtt.py @@ -2,8 +2,6 @@ """ Contains MQTT funtionality. """ -import asyncio - import paho.mqtt.client as mqtt from models import network @@ -21,9 +19,7 @@ def on_message(client, userdata, msg): root = msg.topic.partition('/')[0] devices = [device for device in network if root == device.mqtt_root] for device in devices: - coro = device.mqtt_callback(client.app, msg) - future = asyncio.run_coroutine_threadsafe(coro, client.app.loop) - future.result() + device.mqtt_callback(client.app, msg) def init_client(app):