conson-xp 1.49.0__py3-none-any.whl → 1.50.1__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.
@@ -0,0 +1,308 @@
1
+ """Conbus export service for exporting device configurations."""
2
+
3
+ import asyncio
4
+ import logging
5
+ from pathlib import Path
6
+ from queue import SimpleQueue
7
+ from typing import Any, Optional, Tuple
8
+
9
+ import yaml
10
+ from psygnal import Signal
11
+
12
+ from xp.models.actiontable.actiontable_type import ActionTableType, ActionTableType2
13
+ from xp.models.conbus.conbus_export import ConbusExportResponse
14
+ from xp.models.config.conson_module_config import (
15
+ ConsonModuleConfig,
16
+ ConsonModuleListConfig,
17
+ )
18
+ from xp.services.conbus.actiontable.actiontable_download_service import (
19
+ ActionTableDownloadService,
20
+ )
21
+
22
+
23
+ class ConbusActiontableExportService:
24
+ """
25
+ Service for exporting Conbus device configurations.
26
+
27
+ Discovers all devices on the Conbus network and queries their configuration
28
+ datapoints to generate a structured export file compatible with conson.yml format.
29
+
30
+ Attributes:
31
+ download_service: Download service for exporting device configurations.
32
+ export_result: Final export result.
33
+ export_status: Export status (OK, FAILED_TIMEOUT, etc.).
34
+ on_progress: Signal emitted on device discovery (serial, current, total).
35
+ on_device_actiontable_exported: Signal emitted when device export completes.
36
+ on_finish: Signal emitted when export finishes.
37
+ ACTIONTABLE_SEQUENCE: Sequence of actiontable to query for each device.
38
+ """
39
+
40
+ # Signals (class attributes)
41
+ on_progress: Signal = Signal(str, int, int) # serial, current, total
42
+ on_device_actiontable_exported: Signal = Signal(
43
+ ConsonModuleConfig, ActionTableType, str
44
+ )
45
+ on_finish: Signal = Signal(ConbusExportResponse)
46
+
47
+ ACTIONTABLE_SEQUENCE = [
48
+ ActionTableType2.ACTIONTABLE,
49
+ ActionTableType2.MSACTIONTABLE,
50
+ ]
51
+
52
+ def __init__(
53
+ self,
54
+ download_service: ActionTableDownloadService,
55
+ module_list: ConsonModuleListConfig,
56
+ ) -> None:
57
+ """
58
+ Initialize the Conbus export service.
59
+
60
+ Args:
61
+ download_service: Protocol for downloading actiontables.
62
+ module_list: module to export.
63
+ """
64
+ self.logger = logging.getLogger(__name__)
65
+ self.download_service = download_service
66
+ self._module_list: ConsonModuleListConfig = module_list
67
+ # State management
68
+ self.device_queue: SimpleQueue[Tuple[ConsonModuleConfig, ActionTableType]] = (
69
+ SimpleQueue()
70
+ ) # FIFO
71
+ for module in self._module_list.root:
72
+ self.device_queue.put((module, ActionTableType.ACTIONTABLE))
73
+ if module.module_type == "xp20":
74
+ self.device_queue.put((module, ActionTableType.MSACTIONTABLE_XP20))
75
+ if module.module_type == "xp24":
76
+ self.device_queue.put((module, ActionTableType.MSACTIONTABLE_XP24))
77
+ if module.module_type == "xp33":
78
+ self.device_queue.put((module, ActionTableType.MSACTIONTABLE_XP33))
79
+
80
+ self.current_module: Optional[ConsonModuleConfig] = None
81
+ self.curent_actiontable_type: Optional[ActionTableType] = None
82
+ self.export_result = ConbusExportResponse(success=False)
83
+ self.export_status = "OK"
84
+ # Connect protocol signals
85
+ self._connect_signals()
86
+
87
+ def on_module_actiontable_received(
88
+ self, actiontable: Any, short_actiontable: list[str]
89
+ ) -> None:
90
+ """
91
+ Handle actiontable received event.
92
+
93
+ Args:
94
+ actiontable: Full actiontable data.
95
+ short_actiontable: Short representation of the actiontable.
96
+ """
97
+ if not self.curent_actiontable_type:
98
+ self._fail("Invalid state (curent_actiontable_type)")
99
+ return
100
+
101
+ if not self.current_module:
102
+ self._fail("Invalid state (current_module)")
103
+ return
104
+
105
+ if self.curent_actiontable_type == ActionTableType.ACTIONTABLE:
106
+ self.current_module.action_table = short_actiontable
107
+ elif self.curent_actiontable_type == ActionTableType.MSACTIONTABLE_XP20:
108
+ self.current_module.xp20_msaction_table = short_actiontable
109
+ elif self.curent_actiontable_type == ActionTableType.MSACTIONTABLE_XP24:
110
+ self.current_module.xp24_msaction_table = short_actiontable
111
+ elif self.curent_actiontable_type == ActionTableType.MSACTIONTABLE_XP33:
112
+ self.current_module.xp33_msaction_table = short_actiontable
113
+
114
+ self.on_device_actiontable_exported.emit(
115
+ self.current_module, self.curent_actiontable_type, actiontable
116
+ )
117
+
118
+ def on_module_finish(self) -> None:
119
+ """Handle module export completion."""
120
+ self._save_action_table()
121
+ has_next_module = self.configure()
122
+ if not has_next_module:
123
+ self._succeed()
124
+
125
+ def on_module_progress(self) -> None:
126
+ """Handle module progress event and emit progress signal."""
127
+ serial_number = (
128
+ self.current_module.serial_number if self.current_module else "UNKNOWN"
129
+ )
130
+ total_modules = len(self._module_list.root)
131
+ current_index = total_modules - self.device_queue.qsize()
132
+
133
+ self.on_progress.emit(serial_number, current_index, total_modules)
134
+
135
+ def on_module_error(self, error_message: str) -> None:
136
+ """
137
+ Handle module error event.
138
+
139
+ Args:
140
+ error_message: Error message from module.
141
+ """
142
+ self._fail(error_message)
143
+
144
+ def _save_action_table(self) -> None:
145
+ """Write export to YAML file."""
146
+ self.logger.info("Saving action table")
147
+
148
+ if not self._module_list:
149
+ self._fail("FAILED_NO_DEVICES")
150
+ return
151
+
152
+ try:
153
+ # Write to file
154
+ path = "export.yml"
155
+ output_path = Path(path)
156
+
157
+ # Use Pydantic's model_dump to serialize, excluding only internal fields
158
+ data = self._module_list.model_dump(
159
+ exclude={
160
+ "root": {
161
+ "__all__": {
162
+ "enabled",
163
+ "conbus_ip",
164
+ "conbus_port",
165
+ "action_table",
166
+ }
167
+ }
168
+ },
169
+ exclude_none=True,
170
+ )
171
+
172
+ # Export as list at root level (not wrapped in 'root:' key)
173
+ modules_list = data.get("root", [])
174
+
175
+ with output_path.open("w") as f:
176
+ # Dump each module separately with blank lines between them
177
+ for i, module in enumerate(modules_list):
178
+ # Add blank line before each module except the first
179
+ if i > 0:
180
+ f.write("\n")
181
+
182
+ # Dump single item as list element
183
+ yaml_str = yaml.safe_dump(
184
+ [module],
185
+ default_flow_style=False,
186
+ sort_keys=False,
187
+ allow_unicode=True,
188
+ )
189
+ # Remove the trailing newline and write
190
+ f.write(yaml_str.rstrip("\n") + "\n")
191
+
192
+ self.logger.info(f"Export written to {path}")
193
+ self.export_result.output_file = path
194
+
195
+ except Exception as e:
196
+ self._fail(f"Failed to create export: {e}")
197
+
198
+ def configure(self) -> bool:
199
+ """
200
+ Configure export service.
201
+
202
+ Returns:
203
+ True if there is a module to export, False otherwise.
204
+ """
205
+ self.download_service.reset()
206
+ (self.current_module, self.curent_actiontable_type) = (
207
+ self.device_queue.get_nowait()
208
+ )
209
+ if not (self.current_module or self.curent_actiontable_type):
210
+ self.logger.error("No module to export")
211
+ return False
212
+
213
+ self.download_service.configure(
214
+ self.current_module.serial_number,
215
+ self.curent_actiontable_type,
216
+ )
217
+ self.download_service.do_connect()
218
+ return True
219
+
220
+ def set_event_loop(self, event_loop: asyncio.AbstractEventLoop) -> None:
221
+ """
222
+ Set event loop for async operations.
223
+
224
+ Args:
225
+ event_loop: Event loop to use.
226
+ """
227
+ self.logger.debug("Set event loop")
228
+ self.download_service.set_event_loop(event_loop)
229
+
230
+ def set_timeout(self, timeout_seconds: float) -> None:
231
+ """
232
+ Set timeout.
233
+
234
+ Args:
235
+ timeout_seconds: Timeout in seconds.
236
+ """
237
+ self.download_service.set_timeout(timeout_seconds)
238
+
239
+ def start_reactor(self) -> None:
240
+ """Start the reactor."""
241
+ self.download_service.start_reactor()
242
+
243
+ def stop_reactor(self) -> None:
244
+ """Stop the reactor."""
245
+ self.download_service.stop_reactor()
246
+
247
+ def __enter__(self) -> "ConbusActiontableExportService":
248
+ """
249
+ Enter context manager.
250
+
251
+ Returns:
252
+ Self for context manager protocol.
253
+ """
254
+ # Reset state for reuse
255
+ self.export_result = ConbusExportResponse(success=False)
256
+ self.export_status = "OK"
257
+ self._connect_signals()
258
+ return self
259
+
260
+ def __exit__(
261
+ self, _exc_type: Optional[type], _exc_val: Optional[Exception], _exc_tb: Any
262
+ ) -> None:
263
+ """Exit context manager and disconnect signals."""
264
+ self._disconnect_signals()
265
+ self.stop_reactor()
266
+
267
+ def _connect_signals(self) -> None:
268
+ """Connect download service signals to handlers."""
269
+ self.download_service.on_actiontable_received.connect(
270
+ self.on_module_actiontable_received
271
+ )
272
+ self.download_service.on_finish.connect(self.on_module_finish)
273
+ self.download_service.on_progress.connect(self.on_module_progress)
274
+ self.download_service.on_error.connect(self.on_module_error)
275
+
276
+ def _disconnect_signals(self) -> None:
277
+ """Disconnect download service signals from handlers."""
278
+ self.download_service.on_actiontable_received.connect(
279
+ self.on_module_actiontable_received
280
+ )
281
+ self.download_service.on_finish.disconnect(self.on_module_finish)
282
+ self.download_service.on_progress.disconnect(self.on_module_progress)
283
+ self.download_service.on_error.disconnect(self.on_module_error)
284
+
285
+ self.on_progress.disconnect()
286
+ self.on_device_actiontable_exported.disconnect()
287
+ self.on_finish.disconnect()
288
+
289
+ def _fail(self, error: str) -> None:
290
+ """
291
+ Handle export failure.
292
+
293
+ Args:
294
+ error: Error message.
295
+ """
296
+ self.logger.error(error)
297
+ self.export_result.success = False
298
+ self.export_result.error = error
299
+ self.export_result.export_status = "FAILED"
300
+ self.on_finish.emit(self.export_result)
301
+
302
+ def _succeed(self) -> None:
303
+ """Handle export success."""
304
+ self.logger.info("Export succeed")
305
+ self.export_result.success = True
306
+ self.export_result.error = None
307
+ self.export_result.export_status = "OK"
308
+ self.on_finish.emit(self.export_result)
@@ -57,16 +57,19 @@ class ConbusExportService:
57
57
  DataPointType.AUTO_REPORT_STATUS,
58
58
  ]
59
59
 
60
- def __init__(self, conbus_protocol: ConbusEventProtocol) -> None:
60
+ def __init__(
61
+ self, conbus_protocol: ConbusEventProtocol, telegram_service: TelegramService
62
+ ) -> None:
61
63
  """
62
64
  Initialize the Conbus export service.
63
65
 
64
66
  Args:
65
67
  conbus_protocol: Protocol for Conbus communication.
68
+ telegram_service: TelegramService for telegram parsing.
66
69
  """
67
70
  self.logger = logging.getLogger(__name__)
68
71
  self.conbus_protocol = conbus_protocol
69
- self.telegram_service = TelegramService()
72
+ self.telegram_service = telegram_service
70
73
 
71
74
  # State management
72
75
  self.discovered_devices: list[str] = []
xp/utils/dependencies.py CHANGED
@@ -51,9 +51,6 @@ from xp.services.conbus.conbus_output_service import ConbusOutputService
51
51
  from xp.services.conbus.conbus_raw_service import ConbusRawService
52
52
  from xp.services.conbus.conbus_receive_service import ConbusReceiveService
53
53
  from xp.services.conbus.conbus_scan_service import ConbusScanService
54
- from xp.services.conbus.msactiontable.msactiontable_upload_service import (
55
- MsActionTableUploadService,
56
- )
57
54
  from xp.services.conbus.write_config_service import WriteConfigService
58
55
  from xp.services.homekit.homekit_cache_service import HomeKitCacheService
59
56
  from xp.services.homekit.homekit_conbus_service import HomeKitConbusService
@@ -214,7 +211,8 @@ class ServiceContainer:
214
211
  self.container.register(
215
212
  ConbusExportService,
216
213
  factory=lambda: ConbusExportService(
217
- conbus_protocol=self.container.resolve(ConbusEventProtocol)
214
+ conbus_protocol=self.container.resolve(ConbusEventProtocol),
215
+ telegram_service=self.container.resolve(TelegramService),
218
216
  ),
219
217
  scope=punq.Scope.singleton,
220
218
  )
@@ -336,6 +334,9 @@ class ServiceContainer:
336
334
  factory=lambda: ActionTableUploadService(
337
335
  conbus_protocol=self.container.resolve(ConbusEventProtocol),
338
336
  actiontable_serializer=self.container.resolve(ActionTableSerializer),
337
+ xp20ms_serializer=self.container.resolve(Xp20MsActionTableSerializer),
338
+ xp24ms_serializer=self.container.resolve(Xp24MsActionTableSerializer),
339
+ xp33ms_serializer=self.container.resolve(Xp33MsActionTableSerializer),
339
340
  telegram_service=self.container.resolve(TelegramService),
340
341
  conson_config=self.container.resolve(ConsonModuleListConfig),
341
342
  ),
@@ -372,37 +373,6 @@ class ServiceContainer:
372
373
  scope=punq.Scope.singleton,
373
374
  )
374
375
 
375
- self.container.register(
376
- ActionTableDownloadService,
377
- factory=lambda: ActionTableDownloadService(
378
- conbus_protocol=self.container.resolve(ConbusEventProtocol),
379
- actiontable_serializer=self.container.resolve(ActionTableSerializer),
380
- msactiontable_serializer_xp20=self.container.resolve(
381
- Xp20MsActionTableSerializer
382
- ),
383
- msactiontable_serializer_xp24=self.container.resolve(
384
- Xp24MsActionTableSerializer
385
- ),
386
- msactiontable_serializer_xp33=self.container.resolve(
387
- Xp33MsActionTableSerializer
388
- ),
389
- ),
390
- scope=punq.Scope.singleton,
391
- )
392
-
393
- self.container.register(
394
- MsActionTableUploadService,
395
- factory=lambda: MsActionTableUploadService(
396
- conbus_protocol=self.container.resolve(ConbusEventProtocol),
397
- xp20ms_serializer=self.container.resolve(Xp20MsActionTableSerializer),
398
- xp24ms_serializer=self.container.resolve(Xp24MsActionTableSerializer),
399
- xp33ms_serializer=self.container.resolve(Xp33MsActionTableSerializer),
400
- telegram_service=self.container.resolve(TelegramService),
401
- conson_config=self.container.resolve(ConsonModuleListConfig),
402
- ),
403
- scope=punq.Scope.singleton,
404
- )
405
-
406
376
  self.container.register(
407
377
  ConbusCustomService,
408
378
  factory=lambda: ConbusCustomService(
@@ -1,55 +0,0 @@
1
- """Click parameter type for XP module type validation."""
2
-
3
- from typing import Any, Optional
4
-
5
- import click
6
-
7
-
8
- class XpModuleTypeChoice(click.ParamType):
9
- """
10
- Click parameter type for validating XP module types.
11
-
12
- Attributes:
13
- name: The parameter type name.
14
- choices: List of valid module type strings.
15
- """
16
-
17
- name = "xpmoduletype"
18
-
19
- def __init__(self) -> None:
20
- """Initialize the XpModuleTypeChoice parameter type."""
21
- self.choices = ["xp20", "xp24", "xp31", "xp33"]
22
-
23
- def convert(
24
- self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]
25
- ) -> Any:
26
- """
27
- Convert and validate XP module type input.
28
-
29
- Args:
30
- value: The input value to convert.
31
- param: The Click parameter.
32
- ctx: The Click context.
33
-
34
- Returns:
35
- Lowercase module type string if valid, None if input is None.
36
- """
37
- if value is None:
38
- return value
39
-
40
- # Convert to lower for comparison
41
- normalized_value = value.lower()
42
-
43
- if normalized_value in self.choices:
44
- return normalized_value
45
-
46
- # If not found, show error with available choices
47
- choices_list = "\n".join(f" - {choice}" for choice in sorted(self.choices))
48
- self.fail(
49
- f"{value!r} is not a valid choice. " f"Choose from:\n{choices_list}",
50
- param,
51
- ctx,
52
- )
53
-
54
-
55
- XP_MODULE_TYPE = XpModuleTypeChoice()
@@ -1 +0,0 @@
1
- """MsAction table services for Conbus."""