redis_state_listener fix

This commit is contained in:
2025-12-25 19:36:19 +01:00
parent 62f68fb513
commit 49e555ce51

View File

@@ -127,27 +127,19 @@ async def redis_state_listener():
logger.info("Redis state listener connected") logger.info("Redis state listener connected")
while True: # listen() blocks async and waits for messages - prevents busy loop
try: async for message in pubsub.listen():
message = await asyncio.wait_for( if message["type"] == "message":
pubsub.get_message(ignore_subscribe_messages=True), data = message["data"]
timeout=1.0 try:
) state_data = json.loads(data)
if state_data.get("type") == "state" and state_data.get("device_id"):
if message and message["type"] == "message": device_id = state_data["device_id"]
data = message["data"] payload = state_data.get("payload", {})
try: device_states[device_id] = payload
state_data = json.loads(data) logger.debug(f"Updated state cache for {device_id}: {payload}")
if state_data.get("type") == "state" and state_data.get("device_id"): except Exception as e:
device_id = state_data["device_id"] logger.warning(f"Failed to parse state data: {e}")
payload = state_data.get("payload", {})
device_states[device_id] = payload
logger.debug(f"Updated state cache for {device_id}: {payload}")
except Exception as e:
logger.warning(f"Failed to parse state data: {e}")
except asyncio.TimeoutError:
pass # No message, continue
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("Redis state listener cancelled") logger.info("Redis state listener cancelled")