@@ -390,6 +390,65 @@ async def async_main(
390390 logger .exception ("Error stopping health check service" )
391391
392392
393+ def _resolve_device_config (
394+ cfg : configparser .ConfigParser , args : argparse .Namespace
395+ ) -> tuple [list [str ], list [str ], bool ]:
396+ """Derive device_types, device_ids and skip_test from *cfg* and CLI *args*."""
397+ device_types = (
398+ args .device_types
399+ if args .device_types is not None
400+ else [
401+ dt .strip ()
402+ for dt in cfg .get ("GENERAL" , "DEVICE_TYPE" , fallback = "shellypro3em" ).split (
403+ ","
404+ )
405+ if dt .strip ()
406+ ]
407+ )
408+ skip_test = (
409+ args .skip_powermeter_test
410+ if args .skip_powermeter_test is not None
411+ else cfg .getboolean ("GENERAL" , "SKIP_POWERMETER_TEST" , fallback = False )
412+ )
413+
414+ device_ids : list [str ] = list (args .device_ids ) if args .device_ids is not None else []
415+ if not device_ids :
416+ cfg_device_ids = cfg .get ("GENERAL" , "DEVICE_IDS" , fallback = "" ).strip ()
417+ if cfg_device_ids :
418+ device_ids = [
419+ did .strip () for did in cfg_device_ids .split ("," ) if did .strip ()
420+ ]
421+ while len (device_ids ) < len (device_types ):
422+ device_type = device_types [len (device_ids )]
423+ if device_type in ["shellypro3em" , "shellyemg3" , "shellyproem50" ]:
424+ device_ids .append (f"{ device_type } -ec4609c439c{ len (device_ids ) + 1 } " )
425+ else :
426+ device_ids .append (f"device-{ len (device_ids ) + 1 } " )
427+
428+ if "shellypro3em" in device_types :
429+ shellypro3em_index = device_types .index ("shellypro3em" )
430+ device_types [shellypro3em_index ] = "shellypro3em_old"
431+ device_types .append ("shellypro3em_new" )
432+ device_ids .append (device_ids [shellypro3em_index ])
433+
434+ ct_ports = []
435+ for device_type in device_types :
436+ if device_type in ["ct002" , "ct003" ]:
437+ section = get_ct_section (device_type , cfg )
438+ ct_ports .append (cfg .getint (section , "UDP_PORT" , fallback = UDP_PORT ))
439+ if len (ct_ports ) != len (set (ct_ports )):
440+ raise ValueError (
441+ "Multiple CT002/CT003 devices are configured with the same UDP port. "
442+ "Set UDP_PORT in [CT002]/[CT003] to avoid conflicts."
443+ )
444+
445+ logger .info (f"Device Types: { device_types } " )
446+ logger .info (f"Device IDs: { device_ids } " )
447+ logger .info (f"Skip Test: { skip_test } " )
448+
449+ return device_types , device_ids , skip_test
450+
451+
393452def main ():
394453 parser = argparse .ArgumentParser (description = "Power meter device emulator" )
395454 parser .add_argument (
@@ -444,61 +503,7 @@ def main():
444503 "Git commit not logged (set GIT_COMMIT_SHA at image build for CI images)"
445504 )
446505
447- # Load general settings
448- device_types = (
449- args .device_types
450- if args .device_types is not None
451- else [
452- dt .strip ()
453- for dt in cfg .get ("GENERAL" , "DEVICE_TYPE" , fallback = "shellypro3em" ).split (
454- ","
455- )
456- if dt .strip ()
457- ]
458- )
459- skip_test = (
460- args .skip_powermeter_test
461- if args .skip_powermeter_test is not None
462- else cfg .getboolean ("GENERAL" , "SKIP_POWERMETER_TEST" , fallback = False )
463- )
464-
465- device_ids = args .device_ids if args .device_ids is not None else []
466- # Load device IDs from config if not provided via CLI
467- if not device_ids :
468- cfg_device_ids = cfg .get ("GENERAL" , "DEVICE_IDS" , fallback = "" ).strip ()
469- if cfg_device_ids :
470- device_ids = [
471- did .strip () for did in cfg_device_ids .split ("," ) if did .strip ()
472- ]
473- # Fill missing device IDs with default format
474- while len (device_ids ) < len (device_types ):
475- device_type = device_types [len (device_ids )]
476- if device_type in ["shellypro3em" , "shellyemg3" , "shellyproem50" ]:
477- device_ids .append (f"{ device_type } -ec4609c439c{ len (device_ids ) + 1 } " )
478- else :
479- device_ids .append (f"device-{ len (device_ids ) + 1 } " )
480-
481- # For backward compatibility, replace shellypro3em with shellypro3em_old and shellypro3em_new
482- if "shellypro3em" in device_types :
483- shellypro3em_index = device_types .index ("shellypro3em" )
484- device_types [shellypro3em_index ] = "shellypro3em_old"
485- device_types .append ("shellypro3em_new" )
486- device_ids .append (device_ids [shellypro3em_index ])
487-
488- ct_ports = []
489- for device_type in device_types :
490- if device_type in ["ct002" , "ct003" ]:
491- section = get_ct_section (device_type , cfg )
492- ct_ports .append (cfg .getint (section , "UDP_PORT" , fallback = UDP_PORT ))
493- if len (ct_ports ) != len (set (ct_ports )):
494- raise ValueError (
495- "Multiple CT002/CT003 devices are configured with the same UDP port. "
496- "Set UDP_PORT in [CT002]/[CT003] to avoid conflicts."
497- )
498-
499- logger .info (f"Device Types: { device_types } " )
500- logger .info (f"Device IDs: { device_ids } " )
501- logger .info (f"Skip Test: { skip_test } " )
506+ device_types , device_ids , skip_test = _resolve_device_config (cfg , args )
502507
503508 # Apply command line throttling override if specified
504509 if args .throttle_interval is not None :
@@ -586,6 +591,7 @@ def _restart_handler(signum, frame):
586591 logger .info ("Restarting service…" )
587592 cfg = configparser .ConfigParser (dict_type = OrderedDict , interpolation = None )
588593 cfg .read (args .config )
594+ device_types , device_ids , skip_test = _resolve_device_config (cfg , args )
589595 except RuntimeError as exc :
590596 logger .error ("%s" , exc )
591597 exit (1 )
0 commit comments