@@ -110,12 +110,12 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
110110 self ._host : str = entry .data [CONF_HOST ]
111111 self ._port : int = entry .data .get (CONF_PORT , DEFAULT_PORT )
112112 self ._modules : list [dict [str , Any ]] = entry .data .get (CONF_MODULES , [])
113- raw_write_interval = entry .data .get (CONF_WRITE_INTERVAL , DEFAULT_WRITE_INTERVAL )
114- try :
115- write_interval = float (raw_write_interval )
116- except (TypeError , ValueError ):
117- write_interval = float (DEFAULT_WRITE_INTERVAL )
118- self ._write_interval : float = max (0.0 , write_interval )
113+ raw_write_interval = entry .data .get (CONF_WRITE_INTERVAL , DEFAULT_WRITE_INTERVAL )
114+ try :
115+ self . _write_interval : float = float (raw_write_interval )
116+ except (TypeError , ValueError ):
117+ self . _write_interval = float (DEFAULT_WRITE_INTERVAL )
118+ self ._write_interval = max (0.0 , self . _write_interval )
119119
120120 # Build barcode allowlist from configured modules
121121 self ._configured_barcodes : set [str ] = {
@@ -350,36 +350,36 @@ def _listen(self) -> None:
350350 if self ._process_event (event ):
351351 self ._ha_update_pending = True
352352
353- # Push to HA at most once per write interval, sending
354- # per-barcode averages over the buffered readings rather
355- # than the most-recent raw snapshot.
356- now = time .monotonic ()
357- if self ._ha_update_pending and (
358- now - self ._last_ha_update >= self ._write_interval
359- ):
360- self .data ["counters" ] = parser .counters
361- snapshot = self ._build_averaged_snapshot ()
362- per_node_counts = {
363- barcode : len (readings )
364- for barcode , readings in self ._reading_buffers .items ()
365- if readings and barcode in snapshot ["nodes" ]
366- }
367- _LOGGER .debug (
368- "HA update: %d node(s) — %s" ,
369- len (per_node_counts ),
370- ", " .join (
371- f"{ snapshot ['nodes' ][b ].get ('name' , b )} : "
372- f"{ n } reading(s)"
373- for b , n in per_node_counts .items ()
374- ),
375- )
376- self ._reading_buffers .clear ()
377- self .hass .loop .call_soon_threadsafe (
378- self .async_set_updated_data ,
379- snapshot ,
380- )
381- self ._last_ha_update = now
382- self ._ha_update_pending = False
353+ # Push to HA at most once per write interval, sending
354+ # per-barcode averages over the buffered readings rather
355+ # than the most-recent raw snapshot.
356+ now = time .monotonic ()
357+ if self ._ha_update_pending and (
358+ now - self ._last_ha_update >= self ._write_interval
359+ ):
360+ self .data ["counters" ] = parser .counters
361+ snapshot = self ._build_averaged_snapshot ()
362+ per_node_counts = {
363+ barcode : len (readings )
364+ for barcode , readings in self ._reading_buffers .items ()
365+ if readings and barcode in snapshot ["nodes" ]
366+ }
367+ _LOGGER .debug (
368+ "HA update: %d node(s) — %s" ,
369+ len (per_node_counts ),
370+ ", " .join (
371+ f"{ snapshot ['nodes' ][b ].get ('name' , b )} : "
372+ f"{ n } reading(s)"
373+ for b , n in per_node_counts .items ()
374+ ),
375+ )
376+ self ._reading_buffers .clear ()
377+ self .hass .loop .call_soon_threadsafe (
378+ self .async_set_updated_data ,
379+ snapshot ,
380+ )
381+ self ._last_ha_update = now
382+ self ._ha_update_pending = False
383383 elif (
384384 RECONNECT_TIMEOUT > 0
385385 and (time .monotonic () - last_data_time ) > RECONNECT_TIMEOUT
0 commit comments