ucapi-framework 0.1.0__tar.gz

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,194 @@
1
+ Metadata-Version: 2.4
2
+ Name: ucapi-framework
3
+ Version: 0.1.0
4
+ Summary: ucapi framework that provides core functionalities for building integrations.
5
+ Requires-Python: >=3.11
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: pyee>=13.0.0
8
+ Requires-Dist: ucapi>=0.3.2
9
+ Requires-Dist: aiohttp>=3.9.0
10
+
11
+ [![Tests](https://github.com/YOUR_USERNAME/ucapi-framework/actions/workflows/test.yml/badge.svg)](https://github.com/YOUR_USERNAME/ucapi-framework/actions/workflows/test.yml)
12
+
13
+ # UCAPI Framework
14
+
15
+ A framework for building Unfolded Circle Remote integrations that handles the repetitive parts of integration development so you can focus on what's important.
16
+
17
+ ## What This Solves
18
+
19
+ Building an Unfolded Circle Remote integration typically involves:
20
+ - Writing 200+ lines of setup flow routing logic
21
+ - Manually managing configuration updates and persistence
22
+ - Implementing device lifecycle management (connect/disconnect/reconnect)
23
+ - Wiring up Remote event handlers
24
+ - Managing global state for devices and entities
25
+ - Handling entity registration and state synchronization
26
+
27
+ This framework provides tested implementations of all these patterns, reducing a simple integration from ~1500 lines of boilerplate to ~400 lines of device-specific code. It even adds features, like back and restore, for free.
28
+
29
+ ## Core Features
30
+
31
+ ### Standard Setup Flow with Extension Points
32
+
33
+ The setup flow handles the common pattern: configuration mode → discovery/manual entry → device selection. But every integration has unique needs, so there are extension points at key moments:
34
+
35
+ - **Pre-discovery screens** - Collect API credentials or server addresses before running discovery
36
+ - **Post-selection screens** - Gather device-specific settings after the user picks a device
37
+ - **Custom discovery fields** - Add extra fields to the discovery screen (zones, profiles, etc.)
38
+
39
+ The framework handles all the routing, state management, duplicate checking, and configuration persistence. You just implement the screens you need.
40
+
41
+ **Reduction**: Setup flow code goes from ~200 lines to ~50 lines.
42
+
43
+ ### Device Connection Patterns
44
+
45
+ Four base classes cover the common connection patterns:
46
+
47
+ **StatelessHTTPDevice** - For REST APIs. You implement `verify_connection()` to test reachability. No connection management needed.
48
+
49
+ **PollingDevice** - For devices that need periodic state checks. You set a poll interval and implement `poll_device()`. Automatic reconnection on errors.
50
+
51
+ **WebSocketDevice** - For WebSocket connections. You implement `create_websocket()` and `handle_message()`. Framework manages the connection lifecycle, reconnection, and cleanup.
52
+
53
+ **PersistentConnectionDevice** - For TCP, serial, or custom protocols. You implement `establish_connection()`, `receive_data()`, and `close_connection()`. Framework handles the receive loop and error recovery.
54
+
55
+ All connection management, error handling, reconnection logic, and cleanup happens automatically.
56
+
57
+ **Reduction**: Device implementation goes from ~100 lines of connection boilerplate to ~30 lines of business logic.
58
+
59
+ ### Configuration Management
60
+
61
+ Configuration is just a dataclass. The framework handles JSON serialization, CRUD operations, and persistence:
62
+
63
+ ```python
64
+ @dataclass
65
+ class MyDeviceConfig:
66
+ device_id: str
67
+ name: str
68
+ host: str
69
+
70
+ config = BaseDeviceManager("config.json", MyDeviceConfig)
71
+ ```
72
+
73
+ You get full CRUD operations: `add_or_update()`, `get()`, `remove()`, `all()`, `clear()`. Plus automatic backup/restore functionality for free. The framework handles all the file I/O, error handling, and atomic writes.
74
+
75
+ Full type safety means IDE autocomplete works everywhere. No more dict manipulation or manual JSON handling.
76
+
77
+ **Reduction**: Configuration management goes from ~80 lines to ~15 lines.
78
+
79
+ ### Driver Integration
80
+
81
+ The driver coordinates everything - device lifecycle, entity management, and Remote events. You implement four required methods that define your integration's specifics:
82
+
83
+ - **`device_from_entity_id()`** - Extract device ID from entity ID
84
+ - **`get_entity_ids_for_device()`** - Map device to its entities
85
+ - **`map_device_state()`** - Convert device state to entity state
86
+ - **`create_entities()`** - Instantiate entity objects
87
+
88
+ Everything else is automatic. The framework handles Remote connection events (connect, disconnect, standby), entity subscriptions, device lifecycle management, and state synchronization. You can override the event handlers if needed, but the defaults work for most cases.
89
+
90
+ Device events (like state changes) automatically propagate to entity state updates. The framework maintains the connection between your devices and your remote.
91
+
92
+ **Reduction**: Driver code goes from ~300 lines to ~90 lines.
93
+
94
+ ### Discovery (Optional)
95
+
96
+ If your devices support network discovery, the framework provides implementations for common protocols:
97
+
98
+ **SSDPDiscovery** - For UPnP/SSDP devices. Define your service type and implement `create_discovered_device()` to convert SSDP responses into device configs.
99
+
100
+ **ZeroconfDiscovery** - For mDNS/Bonjour devices. Same pattern: service type + conversion method.
101
+
102
+ **NetworkProbeDiscovery** - For devices that need active probing. Scans local network ranges and calls your `probe_host()` method for each IP.
103
+
104
+ All discovery classes handle the protocol details, timeouts, and error handling. Dependencies are lazy-loaded, so you only install what you use (ssdpy, zeroconf, etc.). If your integration doesn't support discovery, just return an empty list from `discover_devices()` and focus on manual entry.
105
+
106
+ ### Event System
107
+
108
+ The driver base class automatically wires up Remote events (connect, disconnect, standby, subscribe/unsubscribe) with sensible defaults. You can override any of them, but the defaults handle most cases.
109
+
110
+ Device events (state changes, errors) automatically propagate to entity state updates. You just emit events from your device and the framework keeps the Remote in sync.
111
+
112
+ ## How It Works
113
+
114
+ You inherit from base classes and implement a few required methods:
115
+
116
+ **Driver** - Map between device states and entity states. Create entity instances.
117
+
118
+ **Device** - Implement your connection pattern (verify, poll, handle messages, etc.).
119
+
120
+ **Setup Flow** - Define how to discover devices and create configurations from user input.
121
+
122
+ **Config** - Just a dataclass.
123
+
124
+ The framework handles everything else: lifecycle management, event routing, state synchronization, configuration persistence, error handling, and reconnection logic.
125
+
126
+ ## Architecture
127
+
128
+ The framework is layered:
129
+
130
+ ```
131
+ Your Integration (device logic, API calls, protocol handling)
132
+
133
+ BaseIntegrationDriver (lifecycle, events, entity management)
134
+
135
+ Device Interfaces (connection patterns, error handling)
136
+
137
+ Setup Flow + Config Manager (user interaction, persistence)
138
+ ```
139
+
140
+ Each layer handles its responsibility and provides clean extension points. You only touch the top layer.
141
+
142
+ ## Generic Type System
143
+
144
+ The framework uses bounded generics (`DeviceT`, `ConfigT`) so your IDE knows exactly what types you're working with:
145
+
146
+ ```python
147
+ class MyDriver(BaseIntegrationDriver[MyDevice, MyDeviceConfig]):
148
+ def get_device(self, device_id: str) -> MyDevice | None:
149
+ device = super().get_device(device_id)
150
+ # IDE knows device is MyDevice, full autocomplete available
151
+ ```
152
+
153
+ No casting, no generic types, just full type safety throughout.
154
+
155
+ ## Discovery Support
156
+
157
+ Optional discovery implementations for common protocols:
158
+
159
+ - **SSDPDiscovery** - For UPnP/SSDP devices
160
+ - **ZeroconfDiscovery** - For mDNS/Bonjour devices
161
+ - **NetworkProbeDiscovery** - For scanning IP ranges
162
+
163
+ Lazy imports mean you only need the dependencies if you use them.
164
+
165
+ ## Real-World Example
166
+
167
+ See the PSN integration in this repository:
168
+
169
+ - `intg-psn/driver.py` - 90 lines (was 300)
170
+ - `intg-psn/psn.py` - 140 lines (was 240)
171
+ - `intg-psn/setup_flow.py` - 50 lines (was 250)
172
+ - `intg-psn/config.py` - 15 lines (was 95)
173
+
174
+ Total: ~295 lines of integration code vs ~885 lines previously. And the new code is type-safe, testable, and maintainable.
175
+
176
+ ## Migration
177
+
178
+ If you have an existing integration, see [MIGRATION_GUIDE.md](MIGRATION_GUIDE.md) for step-by-step instructions with before/after examples.
179
+
180
+ ## Requirements
181
+
182
+ - Python 3.11+
183
+ - ucapi
184
+ - pyee
185
+
186
+ Optional (only if you use them):
187
+ - aiohttp (for HTTP devices)
188
+ - websockets (for WebSocket devices)
189
+ - ssdpy (for SSDP discovery)
190
+ - zeroconf (for mDNS discovery)
191
+
192
+ ## License
193
+
194
+ Mozilla Public License Version 2.0
@@ -0,0 +1,184 @@
1
+ [![Tests](https://github.com/YOUR_USERNAME/ucapi-framework/actions/workflows/test.yml/badge.svg)](https://github.com/YOUR_USERNAME/ucapi-framework/actions/workflows/test.yml)
2
+
3
+ # UCAPI Framework
4
+
5
+ A framework for building Unfolded Circle Remote integrations that handles the repetitive parts of integration development so you can focus on what's important.
6
+
7
+ ## What This Solves
8
+
9
+ Building an Unfolded Circle Remote integration typically involves:
10
+ - Writing 200+ lines of setup flow routing logic
11
+ - Manually managing configuration updates and persistence
12
+ - Implementing device lifecycle management (connect/disconnect/reconnect)
13
+ - Wiring up Remote event handlers
14
+ - Managing global state for devices and entities
15
+ - Handling entity registration and state synchronization
16
+
17
+ This framework provides tested implementations of all these patterns, reducing a simple integration from ~1500 lines of boilerplate to ~400 lines of device-specific code. It even adds features, like back and restore, for free.
18
+
19
+ ## Core Features
20
+
21
+ ### Standard Setup Flow with Extension Points
22
+
23
+ The setup flow handles the common pattern: configuration mode → discovery/manual entry → device selection. But every integration has unique needs, so there are extension points at key moments:
24
+
25
+ - **Pre-discovery screens** - Collect API credentials or server addresses before running discovery
26
+ - **Post-selection screens** - Gather device-specific settings after the user picks a device
27
+ - **Custom discovery fields** - Add extra fields to the discovery screen (zones, profiles, etc.)
28
+
29
+ The framework handles all the routing, state management, duplicate checking, and configuration persistence. You just implement the screens you need.
30
+
31
+ **Reduction**: Setup flow code goes from ~200 lines to ~50 lines.
32
+
33
+ ### Device Connection Patterns
34
+
35
+ Four base classes cover the common connection patterns:
36
+
37
+ **StatelessHTTPDevice** - For REST APIs. You implement `verify_connection()` to test reachability. No connection management needed.
38
+
39
+ **PollingDevice** - For devices that need periodic state checks. You set a poll interval and implement `poll_device()`. Automatic reconnection on errors.
40
+
41
+ **WebSocketDevice** - For WebSocket connections. You implement `create_websocket()` and `handle_message()`. Framework manages the connection lifecycle, reconnection, and cleanup.
42
+
43
+ **PersistentConnectionDevice** - For TCP, serial, or custom protocols. You implement `establish_connection()`, `receive_data()`, and `close_connection()`. Framework handles the receive loop and error recovery.
44
+
45
+ All connection management, error handling, reconnection logic, and cleanup happens automatically.
46
+
47
+ **Reduction**: Device implementation goes from ~100 lines of connection boilerplate to ~30 lines of business logic.
48
+
49
+ ### Configuration Management
50
+
51
+ Configuration is just a dataclass. The framework handles JSON serialization, CRUD operations, and persistence:
52
+
53
+ ```python
54
+ @dataclass
55
+ class MyDeviceConfig:
56
+ device_id: str
57
+ name: str
58
+ host: str
59
+
60
+ config = BaseDeviceManager("config.json", MyDeviceConfig)
61
+ ```
62
+
63
+ You get full CRUD operations: `add_or_update()`, `get()`, `remove()`, `all()`, `clear()`. Plus automatic backup/restore functionality for free. The framework handles all the file I/O, error handling, and atomic writes.
64
+
65
+ Full type safety means IDE autocomplete works everywhere. No more dict manipulation or manual JSON handling.
66
+
67
+ **Reduction**: Configuration management goes from ~80 lines to ~15 lines.
68
+
69
+ ### Driver Integration
70
+
71
+ The driver coordinates everything - device lifecycle, entity management, and Remote events. You implement four required methods that define your integration's specifics:
72
+
73
+ - **`device_from_entity_id()`** - Extract device ID from entity ID
74
+ - **`get_entity_ids_for_device()`** - Map device to its entities
75
+ - **`map_device_state()`** - Convert device state to entity state
76
+ - **`create_entities()`** - Instantiate entity objects
77
+
78
+ Everything else is automatic. The framework handles Remote connection events (connect, disconnect, standby), entity subscriptions, device lifecycle management, and state synchronization. You can override the event handlers if needed, but the defaults work for most cases.
79
+
80
+ Device events (like state changes) automatically propagate to entity state updates. The framework maintains the connection between your devices and your remote.
81
+
82
+ **Reduction**: Driver code goes from ~300 lines to ~90 lines.
83
+
84
+ ### Discovery (Optional)
85
+
86
+ If your devices support network discovery, the framework provides implementations for common protocols:
87
+
88
+ **SSDPDiscovery** - For UPnP/SSDP devices. Define your service type and implement `create_discovered_device()` to convert SSDP responses into device configs.
89
+
90
+ **ZeroconfDiscovery** - For mDNS/Bonjour devices. Same pattern: service type + conversion method.
91
+
92
+ **NetworkProbeDiscovery** - For devices that need active probing. Scans local network ranges and calls your `probe_host()` method for each IP.
93
+
94
+ All discovery classes handle the protocol details, timeouts, and error handling. Dependencies are lazy-loaded, so you only install what you use (ssdpy, zeroconf, etc.). If your integration doesn't support discovery, just return an empty list from `discover_devices()` and focus on manual entry.
95
+
96
+ ### Event System
97
+
98
+ The driver base class automatically wires up Remote events (connect, disconnect, standby, subscribe/unsubscribe) with sensible defaults. You can override any of them, but the defaults handle most cases.
99
+
100
+ Device events (state changes, errors) automatically propagate to entity state updates. You just emit events from your device and the framework keeps the Remote in sync.
101
+
102
+ ## How It Works
103
+
104
+ You inherit from base classes and implement a few required methods:
105
+
106
+ **Driver** - Map between device states and entity states. Create entity instances.
107
+
108
+ **Device** - Implement your connection pattern (verify, poll, handle messages, etc.).
109
+
110
+ **Setup Flow** - Define how to discover devices and create configurations from user input.
111
+
112
+ **Config** - Just a dataclass.
113
+
114
+ The framework handles everything else: lifecycle management, event routing, state synchronization, configuration persistence, error handling, and reconnection logic.
115
+
116
+ ## Architecture
117
+
118
+ The framework is layered:
119
+
120
+ ```
121
+ Your Integration (device logic, API calls, protocol handling)
122
+
123
+ BaseIntegrationDriver (lifecycle, events, entity management)
124
+
125
+ Device Interfaces (connection patterns, error handling)
126
+
127
+ Setup Flow + Config Manager (user interaction, persistence)
128
+ ```
129
+
130
+ Each layer handles its responsibility and provides clean extension points. You only touch the top layer.
131
+
132
+ ## Generic Type System
133
+
134
+ The framework uses bounded generics (`DeviceT`, `ConfigT`) so your IDE knows exactly what types you're working with:
135
+
136
+ ```python
137
+ class MyDriver(BaseIntegrationDriver[MyDevice, MyDeviceConfig]):
138
+ def get_device(self, device_id: str) -> MyDevice | None:
139
+ device = super().get_device(device_id)
140
+ # IDE knows device is MyDevice, full autocomplete available
141
+ ```
142
+
143
+ No casting, no generic types, just full type safety throughout.
144
+
145
+ ## Discovery Support
146
+
147
+ Optional discovery implementations for common protocols:
148
+
149
+ - **SSDPDiscovery** - For UPnP/SSDP devices
150
+ - **ZeroconfDiscovery** - For mDNS/Bonjour devices
151
+ - **NetworkProbeDiscovery** - For scanning IP ranges
152
+
153
+ Lazy imports mean you only need the dependencies if you use them.
154
+
155
+ ## Real-World Example
156
+
157
+ See the PSN integration in this repository:
158
+
159
+ - `intg-psn/driver.py` - 90 lines (was 300)
160
+ - `intg-psn/psn.py` - 140 lines (was 240)
161
+ - `intg-psn/setup_flow.py` - 50 lines (was 250)
162
+ - `intg-psn/config.py` - 15 lines (was 95)
163
+
164
+ Total: ~295 lines of integration code vs ~885 lines previously. And the new code is type-safe, testable, and maintainable.
165
+
166
+ ## Migration
167
+
168
+ If you have an existing integration, see [MIGRATION_GUIDE.md](MIGRATION_GUIDE.md) for step-by-step instructions with before/after examples.
169
+
170
+ ## Requirements
171
+
172
+ - Python 3.11+
173
+ - ucapi
174
+ - pyee
175
+
176
+ Optional (only if you use them):
177
+ - aiohttp (for HTTP devices)
178
+ - websockets (for WebSocket devices)
179
+ - ssdpy (for SSDP discovery)
180
+ - zeroconf (for mDNS discovery)
181
+
182
+ ## License
183
+
184
+ Mozilla Public License Version 2.0
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "ucapi-framework"
3
+ version = "0.1.0"
4
+ description = "ucapi framework that provides core functionalities for building integrations."
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = [
8
+ "pyee>=13.0.0",
9
+ "ucapi>=0.3.2",
10
+ "aiohttp>=3.9.0",
11
+ ]
12
+
13
+ [dependency-groups]
14
+ dev = [
15
+ "pytest>=9.0.1",
16
+ "pytest-asyncio>=0.23.0",
17
+ "pytest-cov>=4.1.0",
18
+ ]
19
+
20
+ [tool.pytest.ini_options]
21
+ testpaths = ["tests"]
22
+ python_files = ["test_*.py"]
23
+ python_classes = ["Test*"]
24
+ python_functions = ["test_*"]
25
+ asyncio_mode = "auto"
26
+ addopts = [
27
+ "-v",
28
+ "--tb=short",
29
+ "--strict-markers",
30
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+