@@ -88,16 +88,15 @@ async def _send_raw_command(
8888 raise NotImplementedError
8989
9090
91- class CombinedV1RpcChannel (BaseV1RpcChannel ):
92- """A V1 RPC channel that can use both local and MQTT channels, preferring local when available ."""
91+ class PickFirstAvailable (BaseV1RpcChannel ):
92+ """A V1 RPC channel that tries multiple channels and picks the first that works ."""
9393
9494 def __init__ (
95- self , local_channel : LocalChannel , local_rpc_channel : V1RpcChannel , mqtt_channel : V1RpcChannel
95+ self ,
96+ channel_cbs : list [Callable [[], V1RpcChannel | None ]],
9697 ) -> None :
97- """Initialize the combined channel with local and MQTT channels."""
98- self ._local_channel = local_channel
99- self ._local_rpc_channel = local_rpc_channel
100- self ._mqtt_rpc_channel = mqtt_channel
98+ """Initialize the pick-first-available channel."""
99+ self ._channel_cbs = channel_cbs
101100
102101 async def _send_raw_command (
103102 self ,
@@ -106,9 +105,10 @@ async def _send_raw_command(
106105 params : ParamsType = None ,
107106 ) -> Any :
108107 """Send a command and return a parsed response RoborockBase type."""
109- if self ._local_channel .is_connected :
110- return await self ._local_rpc_channel .send_command (method , params = params )
111- return await self ._mqtt_rpc_channel .send_command (method , params = params )
108+ for channel_cb in self ._channel_cbs :
109+ if channel := channel_cb ():
110+ return await channel .send_command (method , params = params )
111+ raise RoborockException ("No available connection to send command" )
112112
113113
114114class PayloadEncodedV1RpcChannel (BaseV1RpcChannel ):
@@ -170,11 +170,10 @@ def create_mqtt_rpc_channel(mqtt_channel: MqttChannel, security_data: SecurityDa
170170 )
171171
172172
173- def create_combined_rpc_channel (local_channel : LocalChannel , mqtt_rpc_channel : V1RpcChannel ) -> V1RpcChannel :
174- """Create a V1 RPC channel that combines local and MQTT channels ."""
175- local_rpc_channel = PayloadEncodedV1RpcChannel (
173+ def create_local_rpc_channel (local_channel : LocalChannel ) -> V1RpcChannel :
174+ """Create a V1 RPC channel using a local channel ."""
175+ return PayloadEncodedV1RpcChannel (
176176 "local" ,
177177 local_channel ,
178178 lambda x : x .encode_message (RoborockMessageProtocol .GENERAL_REQUEST ),
179179 )
180- return CombinedV1RpcChannel (local_channel , local_rpc_channel , mqtt_rpc_channel )
0 commit comments