pytuya-pilot-wire 0.5.2__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.
Files changed (36) hide show
  1. pytuya_pilot_wire-0.5.2/LICENSE +21 -0
  2. pytuya_pilot_wire-0.5.2/PKG-INFO +282 -0
  3. pytuya_pilot_wire-0.5.2/README.md +244 -0
  4. pytuya_pilot_wire-0.5.2/pyproject.toml +61 -0
  5. pytuya_pilot_wire-0.5.2/setup.cfg +4 -0
  6. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/__init__.py +36 -0
  7. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/bridge.py +563 -0
  8. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/channels/__init__.py +5 -0
  9. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/channels/developer.py +228 -0
  10. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/channels/lan.py +119 -0
  11. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/channels/sharing.py +437 -0
  12. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/dialect.py +267 -0
  13. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/dps.py +151 -0
  14. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/errors.py +35 -0
  15. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/gate.py +348 -0
  16. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/model.py +211 -0
  17. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/mqtt.py +112 -0
  18. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/py.typed +0 -0
  19. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/router.py +270 -0
  20. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire/schedule.py +197 -0
  21. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire.egg-info/PKG-INFO +282 -0
  22. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire.egg-info/SOURCES.txt +34 -0
  23. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire.egg-info/dependency_links.txt +1 -0
  24. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire.egg-info/requires.txt +23 -0
  25. pytuya_pilot_wire-0.5.2/src/pytuya_pilot_wire.egg-info/top_level.txt +1 -0
  26. pytuya_pilot_wire-0.5.2/tests/test_bridge.py +606 -0
  27. pytuya_pilot_wire-0.5.2/tests/test_developer.py +164 -0
  28. pytuya_pilot_wire-0.5.2/tests/test_dialect.py +200 -0
  29. pytuya_pilot_wire-0.5.2/tests/test_dps.py +99 -0
  30. pytuya_pilot_wire-0.5.2/tests/test_gate.py +426 -0
  31. pytuya_pilot_wire-0.5.2/tests/test_lan.py +117 -0
  32. pytuya_pilot_wire-0.5.2/tests/test_model.py +113 -0
  33. pytuya_pilot_wire-0.5.2/tests/test_mqtt.py +89 -0
  34. pytuya_pilot_wire-0.5.2/tests/test_router.py +162 -0
  35. pytuya_pilot_wire-0.5.2/tests/test_schedule.py +97 -0
  36. pytuya_pilot_wire-0.5.2/tests/test_sharing.py +339 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Remi Poittevin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,282 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytuya-pilot-wire
3
+ Version: 0.5.2
4
+ Summary: Tuya pilot-wire thermostats for electric radiators: data points, weekly-schedule codec, channels and guarded writes (unofficial).
5
+ Author: aghulas
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/aghulas/pytuya-pilot-wire
8
+ Project-URL: Repository, https://github.com/aghulas/pytuya-pilot-wire
9
+ Project-URL: Issues, https://github.com/aghulas/pytuya-pilot-wire/issues
10
+ Keywords: tuya,pilot-wire,fil-pilote,thermostat,electric-heating,home-assistant,mqtt
11
+ Classifier: Development Status :: 2 - Pre-Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Home Automation
16
+ Classifier: Typing :: Typed
17
+ Requires-Python: >=3.11
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Provides-Extra: sharing
21
+ Requires-Dist: tuya-device-sharing-sdk>=0.2.1; extra == "sharing"
22
+ Provides-Extra: lan
23
+ Requires-Dist: tinytuya>=1.15; extra == "lan"
24
+ Provides-Extra: developer
25
+ Requires-Dist: tinytuya>=1.15; extra == "developer"
26
+ Provides-Extra: mqtt
27
+ Requires-Dist: paho-mqtt<3,>=2.0; extra == "mqtt"
28
+ Provides-Extra: all
29
+ Requires-Dist: pytuya-pilot-wire[developer,lan,mqtt,sharing]; extra == "all"
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytuya-pilot-wire[all]; extra == "dev"
32
+ Requires-Dist: ruff; extra == "dev"
33
+ Requires-Dist: mypy; extra == "dev"
34
+ Requires-Dist: pytest; extra == "dev"
35
+ Requires-Dist: pytest-cov; extra == "dev"
36
+ Requires-Dist: types-requests; extra == "dev"
37
+ Dynamic: license-file
38
+
39
+ # pytuya-pilot-wire
40
+
41
+ Pure-Python library for **Tuya pilot-wire thermostats** driving electric radiators (category
42
+ `wk`). It is the Tuya-facing layer of a three-part project:
43
+
44
+ | Repository | Role |
45
+ |---|---|
46
+ | **`pytuya-pilot-wire`** (this one) | data points, codecs, Tuya channels, guarded writes; no Home Assistant dependency |
47
+ | [`tuyapilot2mqtt`](https://github.com/aghulas/tuyapilot2mqtt) | bridge and Home Assistant add-on: Tuya gateway → MQTT, Zigbee2MQTT dialect |
48
+ | [`ha-tuya-pilot-wire`](https://github.com/aghulas/ha-tuya-pilot-wire) | Home Assistant integration (Companion); holds the [scope](https://github.com/aghulas/ha-tuya-pilot-wire/blob/main/docs/scope.md), architecture and roadmap |
49
+
50
+ Unofficial: not affiliated with Tuya.
51
+
52
+ ## Status
53
+
54
+ Pre-alpha. On PyPI from 0.5.2; install it with the extras of the channels used:
55
+
56
+ ```bash
57
+ pip install "pytuya-pilot-wire[all]"
58
+ ```
59
+
60
+ Every version is also a tag of this repository
61
+ (`pip install "pytuya-pilot-wire[all] @ git+https://github.com/aghulas/pytuya-pilot-wire@v0.5.2"`).
62
+
63
+ Changes: [`CHANGELOG.md`](https://github.com/aghulas/pytuya-pilot-wire/blob/main/CHANGELOG.md).
64
+
65
+ | Module | Content | State |
66
+ |---|---|---|
67
+ | `dps` | data-point table of the "FP Thermostat" (product `fu1bfwza`); decoding of a LAN reading (by number) and of a cloud reading (by code) | done, checked against real readings |
68
+ | `schedule` | weekly-schedule codec (28 data points, 15-minute step), with the read-modify-write rule on headers | done, format checked on real hardware (read, then written and restored) |
69
+ | `model` | `Device`, `DeviceSpec` (the device's own bounds, modes, fault names), `Reading`, `DeviceState` (values merged across channels, each with its channel and time) | done |
70
+ | `dialect` | codec to the Zigbee2MQTT dialect of the [MQTT contract](https://github.com/aghulas/tuyapilot2mqtt/blob/main/docs/mqtt-contract.md): state, descriptor, availability, `/set` commands | done; property names provisional, all in `dialect.PROPERTIES` |
71
+ | `channels.sharing` | device-sharing API (Smart Life account, QR-code login): discovery, instruction sets, status reads | reads checked on real hardware (2026-11-27) |
72
+ | `channels.lan` | LAN protocol 3.3 through the gateway: every data point of each sub-device, read only | reads checked on real hardware (2026-11-27) |
73
+ | `channels.developer` | developer API (shadow properties): settings, weekly schedule, LAN inventory (gateway key, node ids) | reads checked on real hardware (2026-11-27) |
74
+ | `router` | each operation goes to the channel proven for it, or is refused; candidates are closed | done |
75
+ | `gate` | write gate: verified products, the device's own bounds and step, fresh read, one send, read-back; disabled by default | done on synthetic tests; **no write was sent to real hardware by this library yet** (milestone M4) |
76
+ | `bridge`, `mqtt` | reusable daemon core (read loop, publication of the contract's topics, `/set` and `bridge/request` handling) and a paho transport | read loop checked on real hardware (2026-11-27), writes disabled |
77
+
78
+ The three channels were read on the reference install on 2026-11-27 and agree with each other on
79
+ every value they share. The comparison with the vendor app is the owner's to make.
80
+
81
+ ## Use
82
+
83
+ ### Data points and schedule
84
+
85
+ ```python
86
+ from pytuya_pilot_wire import dps, schedule
87
+
88
+ dps.decode_lan(dps.PRODUCT_FP_THERMOSTAT, {"16": 195, "50": 200, "124": "closed"})
89
+ # {'temperature': 19.5, 'setpoint': 20.0, 'work_state': 'closed', 'heating': False, 'power': 0.0}
90
+
91
+ dps.decode_codes(dps.PRODUCT_FP_THERMOSTAT, {"temp_current": 195, "mode": "manual"})
92
+ # {'temperature': 19.5, 'mode': 'manual'}
93
+
94
+ schedule.encode_day(
95
+ 1,
96
+ [
97
+ {"time": "00:00", "temperature": 16.0},
98
+ {"time": "07:30", "temperature": 19.5},
99
+ {"time": "17:00", "temperature": 16.0},
100
+ ],
101
+ current=values_read_from_the_device,
102
+ )
103
+ # {'week_program_18_1_1': '…', … four parts, each keeping the header it had}
104
+ ```
105
+
106
+ A data point that is not in the table is ignored; a value that was not read is absent, never
107
+ zero.
108
+
109
+ ### Channels
110
+
111
+ Each channel returns `Reading`s: the decoded values of one device, the channel and the time.
112
+
113
+ ```python
114
+ from pytuya_pilot_wire.channels.sharing import (
115
+ SharingChannel,
116
+ SharingSession,
117
+ request_qr_code,
118
+ poll_login,
119
+ )
120
+ from pytuya_pilot_wire.channels.lan import Gateway, LanChannel
121
+ from pytuya_pilot_wire.channels.developer import DeveloperChannel, DeveloperProject
122
+
123
+ # Device-sharing API. The caller owns the session: log in once by QR code, store the session,
124
+ # and store it again each time the token is renewed (the refresh token rotates).
125
+ qr_text = request_qr_code(user_code) # show as a QR code, scan with the Smart Life app
126
+ session = poll_login(qr_text, user_code) # None until the user confirmed in the app
127
+ sharing = SharingChannel(session, on_token=store_session)
128
+ devices = sharing.discover() # list[Device]; about five requests per device
129
+ spec = sharing.spec(devices[0].dev_id) # DeviceSpec: bounds, modes, writable keys
130
+ readings = sharing.read([d.dev_id for d in devices if d.supported]) # one request
131
+
132
+ # Developer API: settings, weekly schedule, and what the LAN channel needs.
133
+ developer = DeveloperChannel(DeveloperProject(access_id, access_secret, region="eu"))
134
+ inventory = developer.lan_inventory() # gateway keys, sub-devices with node ids
135
+ developer.read(device) # eco / frost / holiday setpoints, night mode, …
136
+ developer.read_schedule(device.dev_id) # {"monday": [{"time": "00:00", "temperature": 16.0}], …}
137
+
138
+ # LAN: sub-devices through the gateway, read only. The gateway address comes from the caller.
139
+ lan = LanChannel(Gateway(gateway.dev_id, "gateway-address", gateway.local_key))
140
+ lan.read(inventory.devices) # voltage, current, energy, load state, …
141
+ ```
142
+
143
+ Errors derive from `PilotWireError`: `AuthenticationError` (new session or key needed),
144
+ `ChannelUnavailable` (network, gateway silent, dependency missing), `ChannelError` (anything else
145
+ the channel answered).
146
+
147
+ The sharing channel replaces the SDK's HTTP session with a guarded one: allow-list of routes,
148
+ request counters (`sharing.counters`), minimum interval between requests. Sessions, keys and
149
+ project credentials are hidden from `repr`, and the local key and addresses the sharing API
150
+ returns never leave the channel.
151
+
152
+ ### Device model and Zigbee2MQTT dialect
153
+
154
+ ```python
155
+ from pytuya_pilot_wire import DeviceState, dialect
156
+
157
+ state = DeviceState(device)
158
+ for reading in readings:
159
+ state.apply(reading) # returns the keys that changed
160
+
161
+ dialect.encode_state(state, spec)
162
+ # {"local_temperature": 19.5, "current_heating_setpoint": 20.0, "system_mode": "heat",
163
+ # "preset": "manual", "running_state": "idle", "power": 0.0, "fault": [],
164
+ # "tuya": {"mode": "manual", "sources": {"local_temperature": {"channel": "sharing", "at": "…"}}}}
165
+
166
+ names = dialect.friendly_names(devices) # topic-safe and unique
167
+ dialect.encode_descriptor(device, names[device.dev_id]) # entry of bridge/devices
168
+ dialect.decode_command({"current_heating_setpoint": 19.5})
169
+ # [Command(key='setpoint', value=19.5, property='current_heating_setpoint')]
170
+ ```
171
+
172
+ `decode_command` only translates. Whether a command has a proven route and an acceptable value is
173
+ the business of the router and the write gate.
174
+
175
+ ### Router
176
+
177
+ ```python
178
+ from pytuya_pilot_wire import Channel
179
+ from pytuya_pilot_wire.router import Operation, Router
180
+
181
+ router = Router([Channel.SHARING, Channel.LAN]) # the channels that are configured
182
+ router.route(device, Operation.SETPOINT) # Channel.SHARING
183
+ router.route(device, Operation.CHILD_LOCK) # RouteRefused: candidate route, closed …
184
+ router.route(device, Operation.SCHEDULE) # RouteRefused: the developer channel is not available
185
+ router.read_keys(device, Channel.LAN) # the keys a LAN reading is trusted for
186
+ ```
187
+
188
+ | Operation | Route | State |
189
+ |---|---|---|
190
+ | read state | sharing API | proven |
191
+ | read power and energy | LAN | proven |
192
+ | read eco / frost setpoints, night mode | developer API, else LAN | proven |
193
+ | read weekly schedule | developer API | proven |
194
+ | on / off, mode `manual` and `program`, manual setpoint (in `manual` mode only) | sharing API | proven (2026-10-31) |
195
+ | mode `holiday` and `eco`, child lock, window detection, temperature correction, holiday setpoint | sharing API | proven (2026-11-28, trials 3a, 3c, 4a, 4b, 5a, 5b) |
196
+ | weekly schedule | developer API | proven (2026-11-14) |
197
+ | mode frost protection (`hors`, reported outside the declared list) | — | no route: the sharing API refused it (trial 3b) |
198
+ | eco / frost setpoints | — | no route: the sharing API does not declare them writable |
199
+ | night mode | — | no route |
200
+
201
+ There is no fallback: when the proven channel is missing the operation is refused. A candidate
202
+ opens only for the single device designated for its supervised trial, named after the write
203
+ (`Router(channels, trials={"child_lock": dev_id, "mode:holiday": dev_id})`, see
204
+ `router.write_name`); it becomes proven by a change to the table that records the trial. The
205
+ mode has a route per value (`router.MODE_ROUTES`): `router.route(device, Operation.MODE, "eco")`.
206
+
207
+ ### Write gate
208
+
209
+ ```python
210
+ from pytuya_pilot_wire.gate import WriteGate
211
+
212
+ gate = WriteGate(router, sharing=sharing, developer=developer, enabled=True) # disabled by default
213
+ result = gate.write(device, spec, "setpoint", 19.5)
214
+ result.confirmed # True once the device reports 19.5; False after 20 s without it
215
+ result.reading # what was read back: update the state from this, never from the request
216
+
217
+ gate.write_schedule(device, {"monday": [{"time": "00:00", "temperature": 16.0}]})
218
+ ```
219
+
220
+ A write is refused (`WriteRefused`, `RouteRefused`) before anything is sent when: the gate is
221
+ disabled; the product is not verified for writes; there is no proven route; the device does not
222
+ declare the setting writable; the value is outside the device's own bounds or step, above the
223
+ 30 °C setpoint ceiling, or not one of its declared modes (frost protection excepted, once its
224
+ route is open); the device is offline; or, for a
225
+ setpoint, the device is in any mode but `manual` (in `program` the running schedule slot
226
+ overwrites a written setpoint; in frost protection the device stores it and does not apply it;
227
+ in `eco` and `holiday` nothing shows it would be kept). Switch the device to `manual` first.
228
+ The command is sent once and never retried. A schedule write is read-modify-write: each part
229
+ keeps the header read from the device, only the parts that differ are written, and the schedule
230
+ is read back.
231
+
232
+ ### Daemon core
233
+
234
+ `Bridge` is everything a bridge daemon does between the channels and MQTT; the daemon itself
235
+ only loads configuration and secrets and builds the channels.
236
+
237
+ ```python
238
+ import threading
239
+ from pytuya_pilot_wire.bridge import Bridge, BridgeConfig
240
+ from pytuya_pilot_wire.mqtt import PahoTransport
241
+
242
+ config = BridgeConfig(prefix="tuyapilot2mqtt", sharing_interval=60, writes_enabled=False)
243
+ transport = PahoTransport("localhost", 1883, username=user, password=password, prefix=config.prefix)
244
+ bridge = Bridge(transport, sharing=sharing, lan=lan, developer=developer, config=config)
245
+ transport.on_connect = bridge.announce # bridge/state back to online after a reconnection
246
+ transport.connect()
247
+ bridge.run(threading.Event()) # serves until the event is set
248
+ ```
249
+
250
+ It publishes `bridge/state`, `bridge/info` (versions, state of each channel, routing table),
251
+ `bridge/devices`, each device's state and availability, and answers `<name>/set` (on
252
+ `bridge/response/device/set`) and `bridge/request/schedule/get` / `schedule/set`. Any channel may
253
+ be absent. Rules it keeps:
254
+
255
+ - every Tuya call runs on the thread that calls `run`; MQTT callbacks only queue;
256
+ - a reading is trusted for the keys proven for its channel, a property that was not read is
257
+ absent, and a state changes only from a reading;
258
+ - a `/set` payload is validated as a whole before anything is sent;
259
+ - a channel whose credentials are refused is paused until `bridge.replace_channel(...)`; any
260
+ other failure is retried with a growing delay (15 minutes at most).
261
+
262
+ `BridgeConfig`: `prefix`, `sharing_interval` (60 s), `lan_interval` (60 s), `developer_interval`
263
+ (1 h), `discovery_interval` (6 h), `stale_after` (600 s without a reading makes a device
264
+ unavailable), `writes_enabled` (false), `names` (`{dev_id: friendly name}`), `trials`,
265
+ `bridge_version`. The transport is anything with `publish(topic, payload, *, retain)` and
266
+ `subscribe(topic, callback)`.
267
+
268
+ ## Development
269
+
270
+ ```bash
271
+ python3 -m venv .venv && .venv/bin/pip install -e ".[dev]"
272
+ ```
273
+
274
+ ```bash
275
+ .venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy src && .venv/bin/pytest -q --cov=pytuya_pilot_wire --cov-fail-under=95
276
+ ```
277
+
278
+ Tests use synthetic data only: no real device id, key or reading in this repository.
279
+
280
+ ## License
281
+
282
+ MIT — see [`LICENSE`](https://github.com/aghulas/pytuya-pilot-wire/blob/main/LICENSE).
@@ -0,0 +1,244 @@
1
+ # pytuya-pilot-wire
2
+
3
+ Pure-Python library for **Tuya pilot-wire thermostats** driving electric radiators (category
4
+ `wk`). It is the Tuya-facing layer of a three-part project:
5
+
6
+ | Repository | Role |
7
+ |---|---|
8
+ | **`pytuya-pilot-wire`** (this one) | data points, codecs, Tuya channels, guarded writes; no Home Assistant dependency |
9
+ | [`tuyapilot2mqtt`](https://github.com/aghulas/tuyapilot2mqtt) | bridge and Home Assistant add-on: Tuya gateway → MQTT, Zigbee2MQTT dialect |
10
+ | [`ha-tuya-pilot-wire`](https://github.com/aghulas/ha-tuya-pilot-wire) | Home Assistant integration (Companion); holds the [scope](https://github.com/aghulas/ha-tuya-pilot-wire/blob/main/docs/scope.md), architecture and roadmap |
11
+
12
+ Unofficial: not affiliated with Tuya.
13
+
14
+ ## Status
15
+
16
+ Pre-alpha. On PyPI from 0.5.2; install it with the extras of the channels used:
17
+
18
+ ```bash
19
+ pip install "pytuya-pilot-wire[all]"
20
+ ```
21
+
22
+ Every version is also a tag of this repository
23
+ (`pip install "pytuya-pilot-wire[all] @ git+https://github.com/aghulas/pytuya-pilot-wire@v0.5.2"`).
24
+
25
+ Changes: [`CHANGELOG.md`](https://github.com/aghulas/pytuya-pilot-wire/blob/main/CHANGELOG.md).
26
+
27
+ | Module | Content | State |
28
+ |---|---|---|
29
+ | `dps` | data-point table of the "FP Thermostat" (product `fu1bfwza`); decoding of a LAN reading (by number) and of a cloud reading (by code) | done, checked against real readings |
30
+ | `schedule` | weekly-schedule codec (28 data points, 15-minute step), with the read-modify-write rule on headers | done, format checked on real hardware (read, then written and restored) |
31
+ | `model` | `Device`, `DeviceSpec` (the device's own bounds, modes, fault names), `Reading`, `DeviceState` (values merged across channels, each with its channel and time) | done |
32
+ | `dialect` | codec to the Zigbee2MQTT dialect of the [MQTT contract](https://github.com/aghulas/tuyapilot2mqtt/blob/main/docs/mqtt-contract.md): state, descriptor, availability, `/set` commands | done; property names provisional, all in `dialect.PROPERTIES` |
33
+ | `channels.sharing` | device-sharing API (Smart Life account, QR-code login): discovery, instruction sets, status reads | reads checked on real hardware (2026-11-27) |
34
+ | `channels.lan` | LAN protocol 3.3 through the gateway: every data point of each sub-device, read only | reads checked on real hardware (2026-11-27) |
35
+ | `channels.developer` | developer API (shadow properties): settings, weekly schedule, LAN inventory (gateway key, node ids) | reads checked on real hardware (2026-11-27) |
36
+ | `router` | each operation goes to the channel proven for it, or is refused; candidates are closed | done |
37
+ | `gate` | write gate: verified products, the device's own bounds and step, fresh read, one send, read-back; disabled by default | done on synthetic tests; **no write was sent to real hardware by this library yet** (milestone M4) |
38
+ | `bridge`, `mqtt` | reusable daemon core (read loop, publication of the contract's topics, `/set` and `bridge/request` handling) and a paho transport | read loop checked on real hardware (2026-11-27), writes disabled |
39
+
40
+ The three channels were read on the reference install on 2026-11-27 and agree with each other on
41
+ every value they share. The comparison with the vendor app is the owner's to make.
42
+
43
+ ## Use
44
+
45
+ ### Data points and schedule
46
+
47
+ ```python
48
+ from pytuya_pilot_wire import dps, schedule
49
+
50
+ dps.decode_lan(dps.PRODUCT_FP_THERMOSTAT, {"16": 195, "50": 200, "124": "closed"})
51
+ # {'temperature': 19.5, 'setpoint': 20.0, 'work_state': 'closed', 'heating': False, 'power': 0.0}
52
+
53
+ dps.decode_codes(dps.PRODUCT_FP_THERMOSTAT, {"temp_current": 195, "mode": "manual"})
54
+ # {'temperature': 19.5, 'mode': 'manual'}
55
+
56
+ schedule.encode_day(
57
+ 1,
58
+ [
59
+ {"time": "00:00", "temperature": 16.0},
60
+ {"time": "07:30", "temperature": 19.5},
61
+ {"time": "17:00", "temperature": 16.0},
62
+ ],
63
+ current=values_read_from_the_device,
64
+ )
65
+ # {'week_program_18_1_1': '…', … four parts, each keeping the header it had}
66
+ ```
67
+
68
+ A data point that is not in the table is ignored; a value that was not read is absent, never
69
+ zero.
70
+
71
+ ### Channels
72
+
73
+ Each channel returns `Reading`s: the decoded values of one device, the channel and the time.
74
+
75
+ ```python
76
+ from pytuya_pilot_wire.channels.sharing import (
77
+ SharingChannel,
78
+ SharingSession,
79
+ request_qr_code,
80
+ poll_login,
81
+ )
82
+ from pytuya_pilot_wire.channels.lan import Gateway, LanChannel
83
+ from pytuya_pilot_wire.channels.developer import DeveloperChannel, DeveloperProject
84
+
85
+ # Device-sharing API. The caller owns the session: log in once by QR code, store the session,
86
+ # and store it again each time the token is renewed (the refresh token rotates).
87
+ qr_text = request_qr_code(user_code) # show as a QR code, scan with the Smart Life app
88
+ session = poll_login(qr_text, user_code) # None until the user confirmed in the app
89
+ sharing = SharingChannel(session, on_token=store_session)
90
+ devices = sharing.discover() # list[Device]; about five requests per device
91
+ spec = sharing.spec(devices[0].dev_id) # DeviceSpec: bounds, modes, writable keys
92
+ readings = sharing.read([d.dev_id for d in devices if d.supported]) # one request
93
+
94
+ # Developer API: settings, weekly schedule, and what the LAN channel needs.
95
+ developer = DeveloperChannel(DeveloperProject(access_id, access_secret, region="eu"))
96
+ inventory = developer.lan_inventory() # gateway keys, sub-devices with node ids
97
+ developer.read(device) # eco / frost / holiday setpoints, night mode, …
98
+ developer.read_schedule(device.dev_id) # {"monday": [{"time": "00:00", "temperature": 16.0}], …}
99
+
100
+ # LAN: sub-devices through the gateway, read only. The gateway address comes from the caller.
101
+ lan = LanChannel(Gateway(gateway.dev_id, "gateway-address", gateway.local_key))
102
+ lan.read(inventory.devices) # voltage, current, energy, load state, …
103
+ ```
104
+
105
+ Errors derive from `PilotWireError`: `AuthenticationError` (new session or key needed),
106
+ `ChannelUnavailable` (network, gateway silent, dependency missing), `ChannelError` (anything else
107
+ the channel answered).
108
+
109
+ The sharing channel replaces the SDK's HTTP session with a guarded one: allow-list of routes,
110
+ request counters (`sharing.counters`), minimum interval between requests. Sessions, keys and
111
+ project credentials are hidden from `repr`, and the local key and addresses the sharing API
112
+ returns never leave the channel.
113
+
114
+ ### Device model and Zigbee2MQTT dialect
115
+
116
+ ```python
117
+ from pytuya_pilot_wire import DeviceState, dialect
118
+
119
+ state = DeviceState(device)
120
+ for reading in readings:
121
+ state.apply(reading) # returns the keys that changed
122
+
123
+ dialect.encode_state(state, spec)
124
+ # {"local_temperature": 19.5, "current_heating_setpoint": 20.0, "system_mode": "heat",
125
+ # "preset": "manual", "running_state": "idle", "power": 0.0, "fault": [],
126
+ # "tuya": {"mode": "manual", "sources": {"local_temperature": {"channel": "sharing", "at": "…"}}}}
127
+
128
+ names = dialect.friendly_names(devices) # topic-safe and unique
129
+ dialect.encode_descriptor(device, names[device.dev_id]) # entry of bridge/devices
130
+ dialect.decode_command({"current_heating_setpoint": 19.5})
131
+ # [Command(key='setpoint', value=19.5, property='current_heating_setpoint')]
132
+ ```
133
+
134
+ `decode_command` only translates. Whether a command has a proven route and an acceptable value is
135
+ the business of the router and the write gate.
136
+
137
+ ### Router
138
+
139
+ ```python
140
+ from pytuya_pilot_wire import Channel
141
+ from pytuya_pilot_wire.router import Operation, Router
142
+
143
+ router = Router([Channel.SHARING, Channel.LAN]) # the channels that are configured
144
+ router.route(device, Operation.SETPOINT) # Channel.SHARING
145
+ router.route(device, Operation.CHILD_LOCK) # RouteRefused: candidate route, closed …
146
+ router.route(device, Operation.SCHEDULE) # RouteRefused: the developer channel is not available
147
+ router.read_keys(device, Channel.LAN) # the keys a LAN reading is trusted for
148
+ ```
149
+
150
+ | Operation | Route | State |
151
+ |---|---|---|
152
+ | read state | sharing API | proven |
153
+ | read power and energy | LAN | proven |
154
+ | read eco / frost setpoints, night mode | developer API, else LAN | proven |
155
+ | read weekly schedule | developer API | proven |
156
+ | on / off, mode `manual` and `program`, manual setpoint (in `manual` mode only) | sharing API | proven (2026-10-31) |
157
+ | mode `holiday` and `eco`, child lock, window detection, temperature correction, holiday setpoint | sharing API | proven (2026-11-28, trials 3a, 3c, 4a, 4b, 5a, 5b) |
158
+ | weekly schedule | developer API | proven (2026-11-14) |
159
+ | mode frost protection (`hors`, reported outside the declared list) | — | no route: the sharing API refused it (trial 3b) |
160
+ | eco / frost setpoints | — | no route: the sharing API does not declare them writable |
161
+ | night mode | — | no route |
162
+
163
+ There is no fallback: when the proven channel is missing the operation is refused. A candidate
164
+ opens only for the single device designated for its supervised trial, named after the write
165
+ (`Router(channels, trials={"child_lock": dev_id, "mode:holiday": dev_id})`, see
166
+ `router.write_name`); it becomes proven by a change to the table that records the trial. The
167
+ mode has a route per value (`router.MODE_ROUTES`): `router.route(device, Operation.MODE, "eco")`.
168
+
169
+ ### Write gate
170
+
171
+ ```python
172
+ from pytuya_pilot_wire.gate import WriteGate
173
+
174
+ gate = WriteGate(router, sharing=sharing, developer=developer, enabled=True) # disabled by default
175
+ result = gate.write(device, spec, "setpoint", 19.5)
176
+ result.confirmed # True once the device reports 19.5; False after 20 s without it
177
+ result.reading # what was read back: update the state from this, never from the request
178
+
179
+ gate.write_schedule(device, {"monday": [{"time": "00:00", "temperature": 16.0}]})
180
+ ```
181
+
182
+ A write is refused (`WriteRefused`, `RouteRefused`) before anything is sent when: the gate is
183
+ disabled; the product is not verified for writes; there is no proven route; the device does not
184
+ declare the setting writable; the value is outside the device's own bounds or step, above the
185
+ 30 °C setpoint ceiling, or not one of its declared modes (frost protection excepted, once its
186
+ route is open); the device is offline; or, for a
187
+ setpoint, the device is in any mode but `manual` (in `program` the running schedule slot
188
+ overwrites a written setpoint; in frost protection the device stores it and does not apply it;
189
+ in `eco` and `holiday` nothing shows it would be kept). Switch the device to `manual` first.
190
+ The command is sent once and never retried. A schedule write is read-modify-write: each part
191
+ keeps the header read from the device, only the parts that differ are written, and the schedule
192
+ is read back.
193
+
194
+ ### Daemon core
195
+
196
+ `Bridge` is everything a bridge daemon does between the channels and MQTT; the daemon itself
197
+ only loads configuration and secrets and builds the channels.
198
+
199
+ ```python
200
+ import threading
201
+ from pytuya_pilot_wire.bridge import Bridge, BridgeConfig
202
+ from pytuya_pilot_wire.mqtt import PahoTransport
203
+
204
+ config = BridgeConfig(prefix="tuyapilot2mqtt", sharing_interval=60, writes_enabled=False)
205
+ transport = PahoTransport("localhost", 1883, username=user, password=password, prefix=config.prefix)
206
+ bridge = Bridge(transport, sharing=sharing, lan=lan, developer=developer, config=config)
207
+ transport.on_connect = bridge.announce # bridge/state back to online after a reconnection
208
+ transport.connect()
209
+ bridge.run(threading.Event()) # serves until the event is set
210
+ ```
211
+
212
+ It publishes `bridge/state`, `bridge/info` (versions, state of each channel, routing table),
213
+ `bridge/devices`, each device's state and availability, and answers `<name>/set` (on
214
+ `bridge/response/device/set`) and `bridge/request/schedule/get` / `schedule/set`. Any channel may
215
+ be absent. Rules it keeps:
216
+
217
+ - every Tuya call runs on the thread that calls `run`; MQTT callbacks only queue;
218
+ - a reading is trusted for the keys proven for its channel, a property that was not read is
219
+ absent, and a state changes only from a reading;
220
+ - a `/set` payload is validated as a whole before anything is sent;
221
+ - a channel whose credentials are refused is paused until `bridge.replace_channel(...)`; any
222
+ other failure is retried with a growing delay (15 minutes at most).
223
+
224
+ `BridgeConfig`: `prefix`, `sharing_interval` (60 s), `lan_interval` (60 s), `developer_interval`
225
+ (1 h), `discovery_interval` (6 h), `stale_after` (600 s without a reading makes a device
226
+ unavailable), `writes_enabled` (false), `names` (`{dev_id: friendly name}`), `trials`,
227
+ `bridge_version`. The transport is anything with `publish(topic, payload, *, retain)` and
228
+ `subscribe(topic, callback)`.
229
+
230
+ ## Development
231
+
232
+ ```bash
233
+ python3 -m venv .venv && .venv/bin/pip install -e ".[dev]"
234
+ ```
235
+
236
+ ```bash
237
+ .venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy src && .venv/bin/pytest -q --cov=pytuya_pilot_wire --cov-fail-under=95
238
+ ```
239
+
240
+ Tests use synthetic data only: no real device id, key or reading in this repository.
241
+
242
+ ## License
243
+
244
+ MIT — see [`LICENSE`](https://github.com/aghulas/pytuya-pilot-wire/blob/main/LICENSE).
@@ -0,0 +1,61 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pytuya-pilot-wire"
7
+ version = "0.5.2"
8
+ description = "Tuya pilot-wire thermostats for electric radiators: data points, weekly-schedule codec, channels and guarded writes (unofficial)."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "aghulas" }]
14
+ keywords = ["tuya", "pilot-wire", "fil-pilote", "thermostat", "electric-heating", "home-assistant", "mqtt"]
15
+ classifiers = [
16
+ "Development Status :: 2 - Pre-Alpha",
17
+ "Intended Audience :: Developers",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Topic :: Home Automation",
21
+ "Typing :: Typed",
22
+ ]
23
+ dependencies = []
24
+
25
+ [project.optional-dependencies]
26
+ sharing = ["tuya-device-sharing-sdk>=0.2.1"]
27
+ lan = ["tinytuya>=1.15"]
28
+ developer = ["tinytuya>=1.15"]
29
+ mqtt = ["paho-mqtt>=2.0,<3"]
30
+ all = ["pytuya-pilot-wire[sharing,lan,developer,mqtt]"]
31
+ dev = ["pytuya-pilot-wire[all]", "ruff", "mypy", "pytest", "pytest-cov", "types-requests"]
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/aghulas/pytuya-pilot-wire"
35
+ Repository = "https://github.com/aghulas/pytuya-pilot-wire"
36
+ Issues = "https://github.com/aghulas/pytuya-pilot-wire/issues"
37
+
38
+ [tool.setuptools.packages.find]
39
+ where = ["src"]
40
+
41
+ [tool.setuptools.package-data]
42
+ pytuya_pilot_wire = ["py.typed"]
43
+
44
+ [tool.ruff]
45
+ target-version = "py311"
46
+ line-length = 100
47
+
48
+ [tool.ruff.lint]
49
+ select = ["E", "F", "I", "UP", "B", "SIM", "FURB"]
50
+
51
+ [tool.mypy]
52
+ python_version = "3.11"
53
+ strict = true
54
+
55
+ [[tool.mypy.overrides]]
56
+ module = ["tuya_sharing.*", "tinytuya.*"]
57
+ ignore_missing_imports = true
58
+ follow_untyped_imports = false
59
+
60
+ [tool.pytest.ini_options]
61
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,36 @@
1
+ """Tuya pilot-wire thermostats: data points, codecs, device model and channels.
2
+
3
+ No Home Assistant import. The channel clients need optional dependencies (extras ``sharing``,
4
+ ``lan``, ``developer``), imported only when a channel is used.
5
+ """
6
+
7
+ from .errors import (
8
+ AuthenticationError,
9
+ ChannelError,
10
+ ChannelUnavailable,
11
+ DialectError,
12
+ PilotWireError,
13
+ RouteRefused,
14
+ WriteRefused,
15
+ )
16
+ from .model import Bounds, Channel, Device, DeviceSpec, DeviceState, Reading, Sourced
17
+
18
+ __version__ = "0.5.2"
19
+
20
+ __all__ = [
21
+ "AuthenticationError",
22
+ "Bounds",
23
+ "Channel",
24
+ "ChannelError",
25
+ "ChannelUnavailable",
26
+ "Device",
27
+ "DeviceSpec",
28
+ "DeviceState",
29
+ "DialectError",
30
+ "PilotWireError",
31
+ "Reading",
32
+ "RouteRefused",
33
+ "Sourced",
34
+ "WriteRefused",
35
+ "__version__",
36
+ ]