Reduce duplicate code in the Bluetooth WebSocket API (#135808)

This commit is contained in:
J. Nick Koston 2025-01-16 22:30:14 -10:00 committed by GitHub
parent 5f9457ab6e
commit c215aee940
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -106,36 +106,23 @@ class _AdvertisementSubscription:
self._async_added(self.pending_service_infos) self._async_added(self.pending_service_infos)
self.pending_service_infos.clear() self.pending_service_infos.clear()
def _async_added(self, service_infos: Iterable[BluetoothServiceInfoBleak]) -> None: def _async_event_message(self, message: dict[str, Any]) -> None:
self.connection.send_message( self.connection.send_message(
json_bytes( json_bytes(websocket_api.event_message(self.ws_msg_id, message))
websocket_api.event_message( )
self.ws_msg_id,
def _async_added(self, service_infos: Iterable[BluetoothServiceInfoBleak]) -> None:
self._async_event_message(
{ {
"add": [ "add": [
serialize_service_info(service_info, self.time_diff) serialize_service_info(service_info, self.time_diff)
for service_info in service_infos for service_info in service_infos
] ]
}, }
)
)
) )
def _async_removed(self, address: str) -> None: def _async_removed(self, address: str) -> None:
self.connection.send_message( self._async_event_message({"remove": [{"address": address}]})
json_bytes(
websocket_api.event_message(
self.ws_msg_id,
{
"remove": [
{
"address": address,
}
]
},
)
)
)
@callback @callback
def _async_on_advertisement( def _async_on_advertisement(