Skip to content

Commit

Permalink
update to brewblox-service 2
Browse files Browse the repository at this point in the history
  • Loading branch information
steersbob committed May 30, 2023
1 parent b3fdf6a commit 0a561a8
Show file tree
Hide file tree
Showing 4 changed files with 775 additions and 644 deletions.
21 changes: 11 additions & 10 deletions brewblox_tilt/broadcaster.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json
import time
from os import getenv
from pathlib import Path
Expand Down Expand Up @@ -51,8 +52,8 @@ async def on_event(self, mac: str, rssi: int, packet: IBeaconAdvertisement, info
txpower=packet.tx_power,
rssi=rssi)

async def on_names_change(self, topic: str, data: dict):
self.parser.apply_custom_names(data)
async def on_names_change(self, topic: str, payload: str):
self.parser.apply_custom_names(json.loads(payload))

async def detect_device_id(self) -> int:
bt_dir = Path('/sys/class/bluetooth')
Expand Down Expand Up @@ -142,11 +143,11 @@ async def run(self):
# This will make the service show up in the UI even without active Tilts
await mqtt.publish(self.app,
self.state_topic,
{
json.dumps({
'key': self.name,
'type': 'Tilt.state.service',
'timestamp': time_ms(),
},
}),
err=False,
retain=True)

Expand All @@ -159,13 +160,13 @@ async def run(self):
# Devices can share an event
await mqtt.publish(self.app,
self.history_topic,
{
json.dumps({
'key': self.name,
'data': {
msg.name: msg.data
for msg in messages
},
},
}),
err=False)

# Publish state
Expand All @@ -175,30 +176,30 @@ async def run(self):
for msg in messages:
await mqtt.publish(self.app,
f'{self.state_topic}/{msg.color}/{msg.mac}',
{
json.dumps({
'key': self.name,
'type': 'Tilt.state',
'timestamp': timestamp,
'color': msg.color,
'mac': msg.mac,
'name': msg.name,
'data': msg.data,
},
}),
err=False,
retain=True)

for sync in msg.sync:
if sync.type == 'TempSensorExternal':
await mqtt.publish(self.app,
'brewcast/spark/blocks/patch',
{
json.dumps({
'id': sync.block,
'serviceId': sync.service,
'type': 'TempSensorExternal',
'data': {
'setting[degC]': msg.data['temperature[degC]'],
},
},
}),
err=False)


Expand Down
21 changes: 11 additions & 10 deletions brewblox_tilt/broadcaster_sim.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import json
import time
from random import uniform

Expand Down Expand Up @@ -61,8 +62,8 @@ def __init__(self, app: web.Application):

LOGGER.info(f'Started simulation for {config["simulate"]}')

async def on_names_change(self, topic: str, data: dict):
self.parser.apply_custom_names(data)
async def on_names_change(self, topic: str, payload: str):
self.parser.apply_custom_names(json.loads(payload))

async def prepare(self):
await mqtt.listen(self.app, self.names_topic, self.on_names_change)
Expand All @@ -81,11 +82,11 @@ async def run(self):

await mqtt.publish(self.app,
self.state_topic,
{
json.dumps({
'key': self.name,
'type': 'Tilt.state.service',
'timestamp': time_ms(),
},
}),
err=False,
retain=True)

Expand All @@ -98,13 +99,13 @@ async def run(self):
# Devices can share an event
await mqtt.publish(self.app,
self.history_topic,
{
json.dumps({
'key': self.name,
'data': {
msg.name: msg.data
for msg in messages
},
},
}),
err=False)

# Publish state
Expand All @@ -114,30 +115,30 @@ async def run(self):
for msg in messages:
await mqtt.publish(self.app,
f'{self.state_topic}/{msg.color}/{msg.mac}',
{
json.dumps({
'key': self.name,
'type': 'Tilt.state',
'timestamp': timestamp,
'color': msg.color,
'mac': msg.mac,
'name': msg.name,
'data': msg.data,
},
}),
err=False,
retain=True)

for sync in msg.sync:
if sync.type == 'Spark.Temperature':
await mqtt.publish(self.app,
'brewcast/spark/blocks/patch',
{
json.dumps({
'id': sync.block,
'serviceId': sync.service,
'type': 'TempSensorExternal',
'data': {
'setting[degC]': msg.data['temperature[degC]'],
},
},
}),
err=False)


Expand Down
Loading

0 comments on commit 0a561a8

Please sign in to comment.