Files
brewpi/tasks/tempsensor.py
jensandClaude Sonnet 5 698c019581 refactor: make ATemperatureSensor's temp observable directly on the sensor
TempSensorSim/TempSensor_max31865's temperature() now stores its
reading on self.temp, so ATemperatureSensor's inherited AttributeChange
(previously never triggered by anything) actually fires. TempSensorTask
no longer keeps its own shadow copy of the reading - it registers its
websocket-push callback on self.sensor directly and just drives the
read each tick; server/brewpi.py's TC-feeding registration moved from
sensor_task to sensor for the same reason.

Priming read happens before registering the callback (not after) since
all tasks are built synchronously at module level, before the asyncio
event loop starts - registering first would fire on_temp_changed's
asyncio.create_task() with no running loop.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GpePKZiEZWbGo9HrfuML6U
2026-07-02 22:03:50 +02:00

35 lines
1.1 KiB
Python

import asyncio
from tasks import ATask
from ws.message import MsgIo
from utils.value import ChangedFloat
from components import ATemperatureSensor
class TempSensorTask(ATask):
def __init__(self, sensor_device: ATemperatureSensor, interval, msg_handler: MsgIo):
ATask.__init__(self, interval)
self.msg_handler = msg_handler
msg_handler.set_recv_handler(self.recv)
self.sensor = sensor_device
# Prime before registering - temperature() sets self.sensor.temp,
# which would otherwise fire on_temp_changed() (and its
# asyncio.create_task()) before the event loop is running, since
# all tasks are built synchronously at module level in brewpi.py.
self.sensor.temperature()
self.sensor.set_on_changed("temp", ChangedFloat(self.on_temp_changed, prec=1).set)
def on_temp_changed(self, value):
asyncio.create_task(self.send({'Temp': value}))
async def recv(self, data):
pass
async def send(self, data):
await self.msg_handler.send(data)
async def on_process(self):
while True:
self.sensor.temperature()
await asyncio.sleep(self.interval)