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
This commit is contained in:
+7
-3
@@ -12,8 +12,12 @@ class TempSensorTask(ATask):
|
||||
self.msg_handler = msg_handler
|
||||
msg_handler.set_recv_handler(self.recv)
|
||||
self.sensor = sensor_device
|
||||
self.temp = self.sensor.temperature()
|
||||
self.set_on_changed("temp", ChangedFloat(self.on_temp_changed, prec=1).set)
|
||||
# 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}))
|
||||
@@ -26,5 +30,5 @@ class TempSensorTask(ATask):
|
||||
|
||||
async def on_process(self):
|
||||
while True:
|
||||
self.temp = self.sensor.temperature()
|
||||
self.sensor.temperature()
|
||||
await asyncio.sleep(self.interval)
|
||||
|
||||
Reference in New Issue
Block a user