@@ -27,7 +27,9 @@ def async_setup(hass: HomeAssistant) -> None:
27
27
28
28
29
29
@lru_cache (maxsize = 1024 )
30
- def serialize_service_info (service_info : BluetoothServiceInfoBleak ) -> dict [str , Any ]:
30
+ def serialize_service_info (
31
+ service_info : BluetoothServiceInfoBleak , time_diff : float
32
+ ) -> dict [str , Any ]:
31
33
"""Serialize a BluetoothServiceInfoBleak object."""
32
34
return {
33
35
"name" : service_info .name ,
@@ -44,7 +46,7 @@ def serialize_service_info(service_info: BluetoothServiceInfoBleak) -> dict[str,
44
46
"service_uuids" : service_info .service_uuids ,
45
47
"source" : service_info .source ,
46
48
"connectable" : service_info .connectable ,
47
- "time" : service_info .time + ( time . time () - time . monotonic ()) ,
49
+ "time" : service_info .time + time_diff ,
48
50
"tx_power" : service_info .tx_power ,
49
51
}
50
52
@@ -66,6 +68,12 @@ def __init__(
66
68
self .ws_msg_id = ws_msg_id
67
69
self .connection = connection
68
70
self .pending = True
71
+ # Keep time_diff precise to 2 decimal places
72
+ # so the cached serialization can be reused,
73
+ # however we still want to calculate it each
74
+ # subscription in case the system clock is wrong
75
+ # and gets corrected.
76
+ self .time_diff = round (time .time () - time .monotonic (), 2 )
69
77
70
78
@callback
71
79
def async_start (self ) -> None :
@@ -89,7 +97,7 @@ def _send_service_infos(
89
97
websocket_api .event_message (
90
98
self .ws_msg_id ,
91
99
[
92
- serialize_service_info (service_info )
100
+ serialize_service_info (service_info , self . time_diff )
93
101
for service_info in service_infos
94
102
],
95
103
)
0 commit comments