@@ -424,23 +424,30 @@ def get_rpc_channel_name(device: RpcDevice, key: str) -> str | None:
424424 if BLU_TRV_IDENTIFIER in key :
425425 return None
426426
427- component = key .split (":" )[0 ]
428- component_id = key .split (":" )[- 1 ]
427+ _ , component , component_id = get_rpc_key (key )
429428
430429 if custom_name := get_rpc_custom_name (device , key ):
431430 if component in (* VIRTUAL_COMPONENTS , "input" , "presencezone" , "script" ):
432431 return custom_name
433432
434- channels = get_rpc_number_of_channels ( device , component )
435-
436- return custom_name if channels == 1 else None
433+ return (
434+ custom_name if get_rpc_number_of_channels ( device , component ) == 1 else None
435+ )
437436
438437 if component in (* VIRTUAL_COMPONENTS , "input" ):
439438 return f"{ component .title ()} { component_id } "
440439
441440 return None
442441
443442
443+ def get_rpc_key_normalized (key : str ) -> str :
444+ """Get normalized key. Workaround for Pro EM50 and Pro 3EM."""
445+ # workaround for Pro EM50
446+ key = key .replace ("em1data" , "em1" )
447+ # workaround for Pro 3EM
448+ return key .replace ("emdata" , "em" )
449+
450+
444451def get_rpc_sub_device_name (
445452 device : RpcDevice , key : str , emeter_phase : str | None = None
446453) -> str :
@@ -455,11 +462,7 @@ def get_rpc_sub_device_name(
455462 if entity_name := device .config [key ].get ("name" ):
456463 return cast (str , entity_name )
457464
458- key = key .replace ("emdata" , "em" )
459- key = key .replace ("em1data" , "em1" )
460-
461- component = key .split (":" )[0 ]
462- component_id = key .split (":" )[- 1 ]
465+ _ , component , component_id = get_rpc_key (get_rpc_key_normalized (key ))
463466
464467 if component in ("cct" , "rgb" , "rgbw" ):
465468 return f"{ device .name } { component .upper ()} light { component_id } "
@@ -528,7 +531,7 @@ def get_rpc_key_instances(
528531
529532def get_rpc_key_ids (keys_dict : dict [str , Any ], key : str ) -> list [int ]:
530533 """Return list of key ids for RPC device from a dict."""
531- return [int ( k . split ( ":" )[ 1 ] ) for k in keys_dict if k .startswith (f"{ key } :" )]
534+ return [get_rpc_key_id ( k ) for k in keys_dict if k .startswith (f"{ key } :" )]
532535
533536
534537def get_rpc_key_by_role (keys_dict : dict [str , Any ], role : str ) -> str | None :
@@ -810,11 +813,10 @@ def is_rpc_exclude_from_relay(
810813 settings : dict [str , Any ], status : dict [str , Any ], channel : str
811814) -> bool :
812815 """Return true if rpc channel should be excludeed from switch platform."""
813- ch = int (channel .split (":" )[1 ])
814816 if is_rpc_thermostat_internal_actuator (status ):
815817 return True
816818
817- return is_rpc_channel_type_light (settings , ch )
819+ return is_rpc_channel_type_light (settings , get_rpc_key_id ( channel ) )
818820
819821
820822def get_shelly_air_lamp_life (lamp_seconds : int ) -> float :
@@ -836,7 +838,7 @@ async def get_rpc_scripts_event_types(
836838 if script_name in ignore_scripts :
837839 continue
838840
839- script_id = int (script . split ( ":" )[ - 1 ] )
841+ script_id = get_rpc_key_id (script )
840842 script_events [script_id ] = await get_rpc_script_event_types (device , script_id )
841843
842844 return script_events
@@ -867,14 +869,8 @@ def get_rpc_device_info(
867869 if key is None :
868870 return DeviceInfo (connections = {(CONNECTION_NETWORK_MAC , mac )})
869871
870- # workaround for Pro EM50
871- key = key .replace ("em1data" , "em1" )
872- # workaround for Pro 3EM
873- key = key .replace ("emdata" , "em" )
874-
875- key_parts = key .split (":" )
876- component = key_parts [0 ]
877- idx = key_parts [1 ] if len (key_parts ) > 1 else None
872+ key = get_rpc_key_normalized (key )
873+ has_id , component , _ = get_rpc_key (key )
878874
879875 if emeter_phase is not None :
880876 return DeviceInfo (
@@ -893,7 +889,7 @@ def get_rpc_device_info(
893889 component not in (* All_LIGHT_TYPES , "cover" , "em1" , "switch" )
894890 and get_irrigation_zone_id (device , key ) is None
895891 )
896- or idx is None
892+ or not has_id
897893 or get_rpc_number_of_channels (device , component ) < 2
898894 ):
899895 return DeviceInfo (connections = {(CONNECTION_NETWORK_MAC , mac )})
0 commit comments