conson-xp 1.29.0__py3-none-any.whl → 1.32.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: conson-xp
3
- Version: 1.29.0
3
+ Version: 1.32.0
4
4
  Summary: XP Protocol Communication Tools
5
5
  Author-Email: ldvchosal <ldvchosal@github.com>
6
6
  License: MIT License
@@ -325,6 +325,7 @@ xp conbus event
325
325
  xp conbus event list
326
326
  xp conbus event raw
327
327
 
328
+ xp conbus export
328
329
 
329
330
  xp conbus lightlevel
330
331
  xp conbus lightlevel get
@@ -1,8 +1,8 @@
1
- conson_xp-1.29.0.dist-info/METADATA,sha256=k_I_mYoEaWaCsYx4kk3JOZ5FaBuAFkggSTKk28JMyGk,10312
2
- conson_xp-1.29.0.dist-info/WHEEL,sha256=tsUv_t7BDeJeRHaSrczbGeuK-TtDpGsWi_JfpzD255I,90
3
- conson_xp-1.29.0.dist-info/entry_points.txt,sha256=1OcdIcDM1hz3ljCXgybaPUh1IOFEwkaTgLIW9u9zGeg,50
4
- conson_xp-1.29.0.dist-info/licenses/LICENSE,sha256=rxj6woMM-r3YCyGq_UHFtbh7kHTAJgrccH6O-33IDE4,1419
5
- xp/__init__.py,sha256=CTv0SXJngPEN2pQxHNESqU87b-94c8Ke6ShS2yRsqDA,181
1
+ conson_xp-1.32.0.dist-info/METADATA,sha256=7pNhSUQaahCS55NfZHY_NR7e7d3VZ73oDUXwcCirr4A,10329
2
+ conson_xp-1.32.0.dist-info/WHEEL,sha256=tsUv_t7BDeJeRHaSrczbGeuK-TtDpGsWi_JfpzD255I,90
3
+ conson_xp-1.32.0.dist-info/entry_points.txt,sha256=1OcdIcDM1hz3ljCXgybaPUh1IOFEwkaTgLIW9u9zGeg,50
4
+ conson_xp-1.32.0.dist-info/licenses/LICENSE,sha256=rxj6woMM-r3YCyGq_UHFtbh7kHTAJgrccH6O-33IDE4,1419
5
+ xp/__init__.py,sha256=WvYNIdtUKhtQhMR_bDasGkHpOfRlda8a-wO0v7kBE5c,181
6
6
  xp/cli/__init__.py,sha256=QjnKB1KaI2aIyKlzrnvCwfbBuUj8HNgwNMvNJVQofbI,81
7
7
  xp/cli/__main__.py,sha256=l2iKwMdat5rTGd3JWs-uGksnYYDDffp_Npz05QdKEeU,117
8
8
  xp/cli/commands/__init__.py,sha256=noh8fdZAWq-ihJEboP8WugbIgq4LJ3jUWMRA7720xWE,4909
@@ -16,6 +16,7 @@ xp/cli/commands/conbus/conbus_custom_commands.py,sha256=lICT93ijMdhVRm8KjNMLo7kQ
16
16
  xp/cli/commands/conbus/conbus_datapoint_commands.py,sha256=r36OuTjREtbGKL-bskAGa0-WLw7x06td6woZn3GYJNA,3630
17
17
  xp/cli/commands/conbus/conbus_discover_commands.py,sha256=MnTCzvERO5xerfs0fuuIBoo1O9h_0IfoJ6snLGVl0lA,1899
18
18
  xp/cli/commands/conbus/conbus_event_commands.py,sha256=7URf-2u8Kzcy0chLYShbZfCbKawf--i-8U88AjhxleQ,3177
19
+ xp/cli/commands/conbus/conbus_export_commands.py,sha256=s3jgg3Wqi1P6rYujpE_9aPA47S4UBQfrZPTr9vzH-UA,2951
19
20
  xp/cli/commands/conbus/conbus_lightlevel_commands.py,sha256=FpCwogdxa7yFUjlrxM7e8Q2Ut32tKAHabngQQChvtJI,6763
20
21
  xp/cli/commands/conbus/conbus_linknumber_commands.py,sha256=KitaGDM5HpwVUz8rLpO8VZUypUTcAg3Bzl0DVm6gnSk,3391
21
22
  xp/cli/commands/conbus/conbus_modulenumber_commands.py,sha256=L7-6y3rDllOjQ9g6Bk_RiTKIhAOHVPLdxWif9exkngs,3463
@@ -43,7 +44,7 @@ xp/cli/commands/telegram/telegram_version_commands.py,sha256=WQyx1-B9yJ8V9WrFyBp
43
44
  xp/cli/commands/term/__init__.py,sha256=1NNST_8YJfj5LCujQISwQflK6LyEn7mDmZpMpvI9d-o,116
44
45
  xp/cli/commands/term/term.py,sha256=gjvsv2OE-F_KNWQrWi04fXQ5cGo0l8P-Ortbb5KTA-A,309
45
46
  xp/cli/commands/term/term_commands.py,sha256=CwqnLPEi7LuC7bCo7kIGKMZoVICY0nu42k8C554A1TA,1206
46
- xp/cli/main.py,sha256=ap5jU0DrSnrCKDKqGXcz9N-sngZodyyN-5ReWE8Fh1s,1817
47
+ xp/cli/main.py,sha256=3bksBas1mI-PZQGVxDJ7aoAG09L-cMQMK9Uf7y6_aF8,1963
47
48
  xp/cli/utils/__init__.py,sha256=gTGIj60Uai0iE7sr9_TtEpl04fD7krtTzbbigXUsUVU,46
48
49
  xp/cli/utils/click_tree.py,sha256=ilmM2IMa_c-TqUMsv2alrZXuS0BNhvVlrBlSfyN8lzM,1670
49
50
  xp/cli/utils/datapoint_type_choice.py,sha256=HcydhlqxZ7YyorEeTjFGkypF2JnYNPvOzkl1rhZ93Fc,1666
@@ -73,6 +74,7 @@ xp/models/conbus/conbus_datapoint.py,sha256=4ncR-vB2lRzRBAA30rYn8eguyTxsZoOKrrXt
73
74
  xp/models/conbus/conbus_discover.py,sha256=nxxUEKfEsH1kd0BF8ovMs7zLujRhrq1oL9ZJtysPr5o,2238
74
75
  xp/models/conbus/conbus_event_list.py,sha256=M8aHRHVB5VDIjqMzjO86xlERt7AMdfjIjt1b70RF52Y,958
75
76
  xp/models/conbus/conbus_event_raw.py,sha256=i5gc7z-0yeunWOZ4rw3AiBt4MANezmhBQKjOOQk3oDc,1567
77
+ xp/models/conbus/conbus_export.py,sha256=m2zrkpVifC9EZZBlJGFaaVyGq8w1a1nbapQM1n9mJo8,1078
76
78
  xp/models/conbus/conbus_lightlevel.py,sha256=GQGhzrCBEJROosNHInXIzBy6MD2AskEIMoFEGgZ60-0,1695
77
79
  xp/models/conbus/conbus_linknumber.py,sha256=uFzKzfB06oIzZEKCb5X2JEI80JjMPFuYglsT1W1k8j4,1815
78
80
  xp/models/conbus/conbus_logger_config.py,sha256=cFWjWn8tc_hPPI2kQAib_Akddar8O-3zkoj6wLBsdUo,3328
@@ -133,6 +135,7 @@ xp/services/conbus/conbus_datapoint_service.py,sha256=SYhHj9RmTmaJ750tyZ1IW2kl7t
133
135
  xp/services/conbus/conbus_discover_service.py,sha256=ZwjYBlgP6FgpHBJk7pcKr4JHfH7WUHDxe4he4F_HblQ,12740
134
136
  xp/services/conbus/conbus_event_list_service.py,sha256=0xyXXNU44epN5bFkU6oiZMyhxfUguul3evqClvPJDcA,3618
135
137
  xp/services/conbus/conbus_event_raw_service.py,sha256=FZFu-LNLInrTKTpiGLyootozvyIF5Si5FMrxNk2ALD0,7000
138
+ xp/services/conbus/conbus_export_service.py,sha256=-EIDiXOitLwXTKCfw1zv4XIuIKWaT-3FkxtQwms7PgM,17920
136
139
  xp/services/conbus/conbus_output_service.py,sha256=mHFOAPx2zo0TStZ3pokp6v94AQjIamcwZDeg5YH_-eo,7240
137
140
  xp/services/conbus/conbus_raw_service.py,sha256=4yZLLTIAOxpgByUTWZXw1ihGa6Xtl98ckj9T7VfprDI,4335
138
141
  xp/services/conbus/conbus_receive_service.py,sha256=7wOaEDrdoXwZE9MeUM89eB3hobYpvtbYk_YLv3MVAtc,5352
@@ -195,10 +198,10 @@ xp/term/widgets/protocol_log.py,sha256=CJUpckWj7GC1kVqixDadteyGnI4aHyzd4kkH-pSbz
195
198
  xp/term/widgets/status_footer.py,sha256=bxrcqKzJ9V0aPSn_WwraVpJz7NxBUh3yIjA3fwv5nVA,3256
196
199
  xp/utils/__init__.py,sha256=_avMF_UOkfR3tNeDIPqQ5odmbq5raKkaq1rZ9Cn1CJs,332
197
200
  xp/utils/checksum.py,sha256=HDpiQxmdIedbCbZ4o_Box0i_Zig417BtCV_46ZyhiTk,1711
198
- xp/utils/dependencies.py,sha256=UmVAEpGqEG6Li0h6u6I-mFgBTu6dsTeWjWUnfaGFofQ,24227
201
+ xp/utils/dependencies.py,sha256=d91Xt4PwnyeMB_tLB-hNDpm95QGMg5uiq52yvOM9BBE,24557
199
202
  xp/utils/event_helper.py,sha256=W-A_xmoXlpWZBbJH6qdaN50o3-XrwFsDgvAGMJDiAgo,1001
200
203
  xp/utils/logging.py,sha256=rZDXwlBrYK8A6MPq5StsMNpgsRowzJXM6fvROPwJdGM,3750
201
204
  xp/utils/serialization.py,sha256=RWHHk86feaB4ZP7rjE4qOWK0900yg2joUBDkP76gfOY,4618
202
205
  xp/utils/state_machine.py,sha256=Oe2sLwCh9z_vr1tF6X0ZRGTeuckRQAGzmef7xc9CNdc,2413
203
206
  xp/utils/time_utils.py,sha256=dEyViDlAG9GWU-J3D_YVa-sGma6yiyyMTgN4h2x3PY4,3781
204
- conson_xp-1.29.0.dist-info/RECORD,,
207
+ conson_xp-1.32.0.dist-info/RECORD,,
xp/__init__.py CHANGED
@@ -3,7 +3,7 @@
3
3
  conson-xp package.
4
4
  """
5
5
 
6
- __version__ = "1.29.0"
6
+ __version__ = "1.32.0"
7
7
  __manufacturer__ = "salchichon"
8
8
  __model__ = "xp.cli"
9
9
  __serial__ = "2025.09.23.000"
@@ -0,0 +1,88 @@
1
+ """Conbus export CLI command."""
2
+
3
+ from contextlib import suppress
4
+
5
+ import click
6
+
7
+ from xp.cli.commands.conbus.conbus import conbus
8
+ from xp.cli.utils.decorators import connection_command
9
+ from xp.models.conbus.conbus_export import ConbusExportResponse
10
+ from xp.models.homekit.homekit_conson_config import ConsonModuleConfig
11
+ from xp.services.conbus.conbus_export_service import ConbusExportService
12
+
13
+
14
+ @conbus.command("export")
15
+ @click.pass_context
16
+ @connection_command()
17
+ def export_conbus_config(ctx: click.Context) -> None:
18
+ r"""Export Conbus device configuration to YAML file.
19
+
20
+ Discovers all devices on the Conbus network and queries their configuration
21
+ datapoints to generate a complete export.yml file in conson.yml format.
22
+
23
+ Args:
24
+ ctx: Click context object.
25
+
26
+ Examples:
27
+ \b
28
+ # Export to export.yml in current directory
29
+ xp conbus export
30
+ """
31
+
32
+ def on_progress(serial_number: str, current: int, total: int) -> None:
33
+ """Handle progress updates during export.
34
+
35
+ Args:
36
+ serial_number: Serial number of discovered device.
37
+ current: Current device number.
38
+ total: Total devices discovered.
39
+ """
40
+ click.echo(f"Querying device {current}/{total}: {serial_number}...")
41
+
42
+ def on_device_exported(module: ConsonModuleConfig) -> None:
43
+ """Handle device export completion.
44
+
45
+ Args:
46
+ module: Exported module configuration.
47
+ """
48
+ module_type = module.module_type or "UNKNOWN"
49
+ module_code = (
50
+ module.module_type_code if module.module_type_code is not None else "?"
51
+ )
52
+ click.echo(f" ✓ Module type: {module_type} ({module_code})")
53
+
54
+ if module.link_number is not None:
55
+ click.echo(f" ✓ Link number: {module.link_number}")
56
+ if module.sw_version:
57
+ click.echo(f" ✓ Software version: {module.sw_version}")
58
+
59
+ def on_finish(result: ConbusExportResponse) -> None:
60
+ """Handle export completion.
61
+
62
+ Args:
63
+ result: Export result.
64
+
65
+ Raises:
66
+ ClickException: When export fails with error message from result.
67
+ """
68
+ # Try to stop reactor (may already be stopped)
69
+ with suppress(Exception):
70
+ service.stop_reactor()
71
+
72
+ if result.success:
73
+ click.echo(
74
+ f"\nExport complete: {result.output_file} ({result.device_count} devices)"
75
+ )
76
+ else:
77
+ click.echo(f"Error: {result.error}", err=True)
78
+ raise click.ClickException(result.error or "Export failed")
79
+
80
+ service: ConbusExportService = (
81
+ ctx.obj.get("container").get_container().resolve(ConbusExportService)
82
+ )
83
+ with service:
84
+ service.on_progress.connect(on_progress)
85
+ service.on_device_exported.connect(on_device_exported)
86
+ service.on_finish.connect(on_finish)
87
+ service.set_timeout(5)
88
+ service.start_reactor()
xp/cli/main.py CHANGED
@@ -4,11 +4,13 @@ import click
4
4
  from click_help_colors import HelpColorsGroup
5
5
 
6
6
  from xp.cli.commands import homekit
7
+
8
+ # Import all conbus command modules to register their commands
9
+ from xp.cli.commands.conbus import conbus_discover_commands # noqa: F401
10
+ from xp.cli.commands.conbus import conbus_export_commands # noqa: F401
7
11
  from xp.cli.commands.conbus.conbus import conbus
8
12
  from xp.cli.commands.file_commands import file
9
13
  from xp.cli.commands.module_commands import module
10
-
11
- # Import all conbus command modules to register their commands
12
14
  from xp.cli.commands.reverse_proxy_commands import reverse_proxy
13
15
  from xp.cli.commands.server.server_commands import server
14
16
 
@@ -0,0 +1,31 @@
1
+ """Conbus export response model."""
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Optional
5
+
6
+ from xp.models.homekit.homekit_conson_config import ConsonModuleListConfig
7
+
8
+
9
+ @dataclass
10
+ class ConbusExportResponse:
11
+ """Response from Conbus export operation.
12
+
13
+ Attributes:
14
+ success: Whether the operation was successful.
15
+ config: Exported module configuration list.
16
+ device_count: Number of devices exported.
17
+ output_file: Path to output file.
18
+ export_status: Export status (OK, FAILED_TIMEOUT, FAILED_NO_DEVICES, etc.).
19
+ error: Error message if operation failed.
20
+ sent_telegrams: List of telegrams sent during export.
21
+ received_telegrams: List of telegrams received during export.
22
+ """
23
+
24
+ success: bool
25
+ config: Optional[ConsonModuleListConfig] = None
26
+ device_count: int = 0
27
+ output_file: str = "export.yml"
28
+ export_status: str = "OK"
29
+ error: Optional[str] = None
30
+ sent_telegrams: list[str] = field(default_factory=list)
31
+ received_telegrams: list[str] = field(default_factory=list)
@@ -0,0 +1,460 @@
1
+ """Conbus export service for exporting device configurations."""
2
+
3
+ import asyncio
4
+ import logging
5
+ from pathlib import Path
6
+ from typing import Any, Optional
7
+
8
+ import yaml
9
+ from psygnal import Signal
10
+
11
+ from xp.models.conbus.conbus_export import ConbusExportResponse
12
+ from xp.models.homekit.homekit_conson_config import (
13
+ ConsonModuleConfig,
14
+ ConsonModuleListConfig,
15
+ )
16
+ from xp.models.protocol.conbus_protocol import TelegramReceivedEvent
17
+ from xp.models.telegram.datapoint_type import DataPointType
18
+ from xp.models.telegram.reply_telegram import ReplyTelegram
19
+ from xp.models.telegram.system_function import SystemFunction
20
+ from xp.models.telegram.telegram_type import TelegramType
21
+ from xp.services.protocol.conbus_event_protocol import ConbusEventProtocol
22
+ from xp.services.telegram.telegram_service import TelegramService
23
+
24
+
25
+ class ConbusExportService:
26
+ """Service for exporting Conbus device configurations.
27
+
28
+ Discovers all devices on the Conbus network and queries their configuration
29
+ datapoints to generate a structured export file compatible with conson.yml format.
30
+
31
+ Attributes:
32
+ conbus_protocol: Protocol for Conbus communication.
33
+ discovered_devices: List of discovered device serial numbers.
34
+ device_configs: Partial device configurations being built.
35
+ device_datapoints_received: Set of datapoints received per device.
36
+ export_result: Final export result.
37
+ export_status: Export status (OK, FAILED_TIMEOUT, etc.).
38
+ on_progress: Signal emitted on device discovery (serial, current, total).
39
+ on_device_exported: Signal emitted when device export completes.
40
+ on_finish: Signal emitted when export finishes.
41
+ DATAPOINT_SEQUENCE: Sequence of 7 datapoints to query for each device.
42
+ """
43
+
44
+ # Signals (class attributes)
45
+ on_progress: Signal = Signal(str, int, int) # serial, current, total
46
+ on_device_exported: Signal = Signal(ConsonModuleConfig)
47
+ on_finish: Signal = Signal(ConbusExportResponse)
48
+
49
+ # Datapoint sequence to query for each device
50
+ DATAPOINT_SEQUENCE = [
51
+ DataPointType.MODULE_TYPE,
52
+ DataPointType.MODULE_TYPE_CODE,
53
+ DataPointType.LINK_NUMBER,
54
+ DataPointType.MODULE_NUMBER,
55
+ DataPointType.SW_VERSION,
56
+ DataPointType.HW_VERSION,
57
+ DataPointType.AUTO_REPORT_STATUS,
58
+ ]
59
+
60
+ def __init__(self, conbus_protocol: ConbusEventProtocol) -> None:
61
+ """Initialize the Conbus export service.
62
+
63
+ Args:
64
+ conbus_protocol: Protocol for Conbus communication.
65
+ """
66
+ self.logger = logging.getLogger(__name__)
67
+ self.conbus_protocol = conbus_protocol
68
+ self.telegram_service = TelegramService()
69
+
70
+ # State management
71
+ self.discovered_devices: list[str] = []
72
+ self.device_configs: dict[str, dict[str, Any]] = {}
73
+ self.device_datapoints_received: dict[str, set[str]] = {}
74
+ self.export_result = ConbusExportResponse(success=False)
75
+ self.export_status = "OK"
76
+ self._finalized = False # Track if export has been finalized
77
+
78
+ # Connect protocol signals
79
+ self.conbus_protocol.on_connection_made.connect(self.connection_made)
80
+ self.conbus_protocol.on_telegram_sent.connect(self.telegram_sent)
81
+ self.conbus_protocol.on_telegram_received.connect(self.telegram_received)
82
+ self.conbus_protocol.on_timeout.connect(self.timeout)
83
+ self.conbus_protocol.on_failed.connect(self.failed)
84
+
85
+ def connection_made(self) -> None:
86
+ """Handle connection established event."""
87
+ self.logger.debug("Connection established, starting discovery")
88
+
89
+ # Send DISCOVERY telegram
90
+ self.conbus_protocol.send_telegram(
91
+ telegram_type=TelegramType.SYSTEM,
92
+ serial_number="0000000000",
93
+ system_function=SystemFunction.DISCOVERY,
94
+ data_value="00",
95
+ )
96
+
97
+ def telegram_sent(self, telegram: str) -> None:
98
+ """Handle telegram sent event.
99
+
100
+ Args:
101
+ telegram: Telegram that was sent.
102
+ """
103
+ self.export_result.sent_telegrams.append(telegram)
104
+
105
+ def telegram_received(self, event: TelegramReceivedEvent) -> None:
106
+ """Handle telegram received event.
107
+
108
+ Args:
109
+ event: Telegram received event.
110
+ """
111
+ self.export_result.received_telegrams.append(event.telegram)
112
+
113
+ # Only process valid reply telegrams
114
+ if not event.checksum_valid or event.telegram_type != TelegramType.REPLY.value:
115
+ return
116
+
117
+ # Parse telegram using TelegramService
118
+ try:
119
+ parsed: ReplyTelegram = self.telegram_service.parse_reply_telegram(
120
+ event.frame
121
+ )
122
+ except Exception as e:
123
+ self.logger.debug(f"Failed to parse telegram: {e}")
124
+ return
125
+
126
+ # Check for discovery response (F01D)
127
+ if parsed.system_function == SystemFunction.DISCOVERY:
128
+ self._handle_discovery_response(parsed.serial_number)
129
+
130
+ # Check for datapoint response (F02D)
131
+ elif parsed.system_function == SystemFunction.READ_DATAPOINT:
132
+ if parsed.datapoint_type and parsed.data_value:
133
+ self._handle_datapoint_response(
134
+ parsed.serial_number, parsed.datapoint_type.value, parsed.data_value
135
+ )
136
+
137
+ def _handle_discovery_response(self, serial_number: str) -> None:
138
+ """Handle discovery response and query all datapoints.
139
+
140
+ Args:
141
+ serial_number: Serial number of discovered device.
142
+ """
143
+ if serial_number in self.discovered_devices:
144
+ self.logger.debug(f"Ignoring duplicate discovery: {serial_number}")
145
+ return
146
+
147
+ self.logger.debug(f"Device discovered: {serial_number}")
148
+ self.discovered_devices.append(serial_number)
149
+ self.device_configs[serial_number] = {"serial_number": serial_number}
150
+ self.device_datapoints_received[serial_number] = set()
151
+
152
+ # Emit progress signal
153
+ current = len(self.discovered_devices)
154
+ total = current # We don't know total until timeout
155
+ self.on_progress.emit(serial_number, current, total)
156
+
157
+ # Send all datapoint queries immediately (protocol handles throttling)
158
+ self.logger.debug(
159
+ f"Sending {len(self.DATAPOINT_SEQUENCE)} queries for {serial_number}"
160
+ )
161
+ for datapoint in self.DATAPOINT_SEQUENCE:
162
+ self.conbus_protocol.send_telegram(
163
+ telegram_type=TelegramType.SYSTEM,
164
+ serial_number=serial_number,
165
+ system_function=SystemFunction.READ_DATAPOINT,
166
+ data_value=datapoint.value,
167
+ )
168
+
169
+ def _handle_datapoint_response(
170
+ self, serial_number: str, datapoint_code: str, value: str
171
+ ) -> None:
172
+ """Handle datapoint response and store value.
173
+
174
+ Args:
175
+ serial_number: Serial number of device.
176
+ datapoint_code: Datapoint type code.
177
+ value: Datapoint value.
178
+ """
179
+ if serial_number not in self.device_configs:
180
+ self.logger.warning(
181
+ f"Received datapoint for unknown device: {serial_number}"
182
+ )
183
+ return
184
+
185
+ self.logger.debug(f"Datapoint {datapoint_code}={value} for {serial_number}")
186
+
187
+ # Store value in device config
188
+ datapoint = DataPointType.from_code(datapoint_code)
189
+ if datapoint:
190
+ self._store_datapoint_value(serial_number, datapoint, value)
191
+ self.device_datapoints_received[serial_number].add(datapoint_code)
192
+ self._check_device_complete(serial_number)
193
+ else:
194
+ self.logger.warning(f"Unknown datapoint code: {datapoint_code}")
195
+
196
+ def _store_datapoint_value(
197
+ self, serial_number: str, datapoint: DataPointType, value: str
198
+ ) -> None:
199
+ """Store datapoint value in device config.
200
+
201
+ Args:
202
+ serial_number: Serial number of device.
203
+ datapoint: Datapoint type.
204
+ value: Datapoint value.
205
+ """
206
+ config = self.device_configs[serial_number]
207
+
208
+ if datapoint == DataPointType.MODULE_TYPE:
209
+ config["module_type"] = value
210
+ elif datapoint == DataPointType.MODULE_TYPE_CODE:
211
+ try:
212
+ config["module_type_code"] = int(value)
213
+ except ValueError:
214
+ self.logger.warning(f"Invalid module_type_code: {value}")
215
+ elif datapoint == DataPointType.LINK_NUMBER:
216
+ try:
217
+ config["link_number"] = int(value)
218
+ except ValueError:
219
+ self.logger.warning(f"Invalid link_number: {value}")
220
+ elif datapoint == DataPointType.MODULE_NUMBER:
221
+ try:
222
+ config["module_number"] = int(value)
223
+ except ValueError:
224
+ self.logger.warning(f"Invalid module_number: {value}")
225
+ elif datapoint == DataPointType.SW_VERSION:
226
+ config["sw_version"] = value
227
+ elif datapoint == DataPointType.HW_VERSION:
228
+ config["hw_version"] = value
229
+ elif datapoint == DataPointType.AUTO_REPORT_STATUS:
230
+ config["auto_report_status"] = value
231
+
232
+ def _check_device_complete(self, serial_number: str) -> None:
233
+ """Check if device has all datapoints and emit completion signal.
234
+
235
+ Args:
236
+ serial_number: Serial number of device.
237
+ """
238
+ received = self.device_datapoints_received[serial_number]
239
+ expected = {dp.value for dp in self.DATAPOINT_SEQUENCE}
240
+
241
+ if received == expected:
242
+ self.logger.debug(f"Device {serial_number} complete (7/7 datapoints)")
243
+ config = self.device_configs[serial_number]
244
+
245
+ # Build ConsonModuleConfig with name based on link_number
246
+ try:
247
+ # Add required 'name' field as A{link_number}
248
+ if "name" not in config:
249
+ link_number = config.get("link_number", 0)
250
+ config["name"] = f"A{link_number}"
251
+ module_config = ConsonModuleConfig(**config)
252
+ self.on_device_exported.emit(module_config)
253
+ except Exception as e:
254
+ self.logger.error(f"Failed to build config for {serial_number}: {e}")
255
+
256
+ # Check if all devices complete
257
+ if all(
258
+ len(self.device_datapoints_received[sn]) == len(self.DATAPOINT_SEQUENCE)
259
+ for sn in self.discovered_devices
260
+ ):
261
+ self.logger.debug("All devices complete")
262
+ self._finalize_export()
263
+
264
+ def _finalize_export(self) -> None:
265
+ """Finalize export and write file."""
266
+ # Only finalize once
267
+ if self._finalized:
268
+ return
269
+
270
+ self._finalized = True
271
+ self.logger.info("Finalizing export")
272
+
273
+ if not self.discovered_devices:
274
+ self.export_status = "FAILED_NO_DEVICES"
275
+ self.export_result.success = False
276
+ self.export_result.error = "No devices found"
277
+ self.export_result.export_status = self.export_status
278
+ self.on_finish.emit(self.export_result)
279
+ return
280
+
281
+ # Build module list (including partial devices)
282
+ modules = []
283
+ for serial_number in self.discovered_devices:
284
+ config = self.device_configs[serial_number].copy()
285
+ try:
286
+ # Add required 'name' field as A{link_number} if not present
287
+ if "name" not in config:
288
+ link_number = config.get("link_number", 0)
289
+ config["name"] = f"A{link_number}"
290
+ # Only include fields that were received
291
+ module_config = ConsonModuleConfig(**config)
292
+ modules.append(module_config)
293
+ except Exception as e:
294
+ self.logger.warning(f"Partial device {serial_number}: {e}")
295
+
296
+ # Sort modules by link_number
297
+ modules.sort(key=lambda m: m.link_number if m.link_number is not None else 999)
298
+
299
+ # Create ConsonModuleListConfig
300
+ try:
301
+ module_list = ConsonModuleListConfig(root=modules)
302
+ self.export_result.config = module_list
303
+ self.export_result.device_count = len(modules)
304
+
305
+ # Write to file
306
+ self._write_export_file("export.yml")
307
+
308
+ self.export_result.success = True
309
+ self.export_result.export_status = self.export_status
310
+ self.on_finish.emit(self.export_result)
311
+
312
+ except Exception as e:
313
+ self.logger.error(f"Failed to create export: {e}")
314
+ self.export_status = "FAILED_WRITE"
315
+ self.export_result.success = False
316
+ self.export_result.error = str(e)
317
+ self.export_result.export_status = self.export_status
318
+ self.on_finish.emit(self.export_result)
319
+
320
+ def _write_export_file(self, path: str) -> None:
321
+ """Write export to YAML file.
322
+
323
+ Args:
324
+ path: Output file path.
325
+
326
+ Raises:
327
+ Exception: If file write fails.
328
+ """
329
+ try:
330
+ output_path = Path(path)
331
+
332
+ if self.export_result.config:
333
+ # Use Pydantic's model_dump to serialize, excluding only internal fields
334
+ data = self.export_result.config.model_dump(
335
+ exclude={
336
+ "root": {
337
+ "__all__": {
338
+ "enabled",
339
+ "conbus_ip",
340
+ "conbus_port",
341
+ "action_table",
342
+ }
343
+ }
344
+ },
345
+ exclude_none=True,
346
+ )
347
+
348
+ # Export as list at root level (not wrapped in 'root:' key)
349
+ modules_list = data.get("root", [])
350
+
351
+ with output_path.open("w") as f:
352
+ # Dump each module separately with blank lines between them
353
+ for i, module in enumerate(modules_list):
354
+ # Add blank line before each module except the first
355
+ if i > 0:
356
+ f.write("\n")
357
+
358
+ # Dump single item as list element
359
+ yaml_str = yaml.safe_dump(
360
+ [module],
361
+ default_flow_style=False,
362
+ sort_keys=False,
363
+ allow_unicode=True,
364
+ )
365
+ # Remove the trailing newline and write
366
+ f.write(yaml_str.rstrip("\n") + "\n")
367
+
368
+ self.logger.info(f"Export written to {path}")
369
+ self.export_result.output_file = path
370
+
371
+ except Exception as e:
372
+ self.logger.error(f"Failed to write export file: {e}")
373
+ self.export_status = "FAILED_WRITE"
374
+ raise
375
+
376
+ def timeout(self) -> None:
377
+ """Handle timeout event."""
378
+ timeout = self.conbus_protocol.timeout_seconds
379
+ self.logger.info(f"Export timeout after {timeout}s")
380
+
381
+ # Check if any devices incomplete
382
+ incomplete = [
383
+ sn
384
+ for sn in self.discovered_devices
385
+ if len(self.device_datapoints_received[sn]) < len(self.DATAPOINT_SEQUENCE)
386
+ ]
387
+
388
+ if incomplete:
389
+ self.logger.warning(f"Partial export: {len(incomplete)} incomplete devices")
390
+ self.export_status = "FAILED_TIMEOUT"
391
+
392
+ self._finalize_export()
393
+
394
+ def failed(self, message: str) -> None:
395
+ """Handle connection failure event.
396
+
397
+ Args:
398
+ message: Failure message.
399
+ """
400
+ self.logger.error(f"Connection failed: {message}")
401
+ self.export_status = "FAILED_CONNECTION"
402
+ self.export_result.success = False
403
+ self.export_result.error = message
404
+ self.export_result.export_status = self.export_status
405
+ self.on_finish.emit(self.export_result)
406
+
407
+ def set_timeout(self, timeout_seconds: float) -> None:
408
+ """Set timeout for export operation.
409
+
410
+ Args:
411
+ timeout_seconds: Timeout in seconds.
412
+ """
413
+ self.logger.debug(f"Set timeout: {timeout_seconds}s")
414
+ self.conbus_protocol.timeout_seconds = timeout_seconds
415
+
416
+ def set_event_loop(self, event_loop: asyncio.AbstractEventLoop) -> None:
417
+ """Set event loop for async operations.
418
+
419
+ Args:
420
+ event_loop: Event loop to use.
421
+ """
422
+ self.logger.debug("Set event loop")
423
+ self.conbus_protocol.set_event_loop(event_loop)
424
+
425
+ def start_reactor(self) -> None:
426
+ """Start the reactor."""
427
+ self.conbus_protocol.start_reactor()
428
+
429
+ def stop_reactor(self) -> None:
430
+ """Stop the reactor."""
431
+ self.conbus_protocol.stop_reactor()
432
+
433
+ def __enter__(self) -> "ConbusExportService":
434
+ """Enter context manager.
435
+
436
+ Returns:
437
+ Self for context manager protocol.
438
+ """
439
+ # Reset state for reuse
440
+ self.discovered_devices = []
441
+ self.device_configs = {}
442
+ self.device_datapoints_received = {}
443
+ self.export_result = ConbusExportResponse(success=False)
444
+ self.export_status = "OK"
445
+ self._finalized = False
446
+ return self
447
+
448
+ def __exit__(
449
+ self, _exc_type: Optional[type], _exc_val: Optional[Exception], _exc_tb: Any
450
+ ) -> None:
451
+ """Exit context manager and disconnect signals."""
452
+ self.conbus_protocol.on_connection_made.disconnect(self.connection_made)
453
+ self.conbus_protocol.on_telegram_sent.disconnect(self.telegram_sent)
454
+ self.conbus_protocol.on_telegram_received.disconnect(self.telegram_received)
455
+ self.conbus_protocol.on_timeout.disconnect(self.timeout)
456
+ self.conbus_protocol.on_failed.disconnect(self.failed)
457
+ self.on_progress.disconnect()
458
+ self.on_device_exported.disconnect()
459
+ self.on_finish.disconnect()
460
+ self.stop_reactor()
xp/utils/dependencies.py CHANGED
@@ -47,6 +47,7 @@ from xp.services.conbus.conbus_datapoint_service import (
47
47
  from xp.services.conbus.conbus_discover_service import ConbusDiscoverService
48
48
  from xp.services.conbus.conbus_event_list_service import ConbusEventListService
49
49
  from xp.services.conbus.conbus_event_raw_service import ConbusEventRawService
50
+ from xp.services.conbus.conbus_export_service import ConbusExportService
50
51
  from xp.services.conbus.conbus_output_service import ConbusOutputService
51
52
  from xp.services.conbus.conbus_raw_service import ConbusRawService
52
53
  from xp.services.conbus.conbus_receive_service import ConbusReceiveService
@@ -201,6 +202,14 @@ class ServiceContainer:
201
202
  scope=punq.Scope.singleton,
202
203
  )
203
204
 
205
+ self.container.register(
206
+ ConbusExportService,
207
+ factory=lambda: ConbusExportService(
208
+ conbus_protocol=self.container.resolve(ConbusEventProtocol)
209
+ ),
210
+ scope=punq.Scope.singleton,
211
+ )
212
+
204
213
  # Terminal UI
205
214
  self.container.register(
206
215
  ProtocolMonitorService,