pywmswebcontrolpro 0.3.0__tar.gz → 0.4.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.
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/CHANGELOG.md +18 -1
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/PKG-INFO +19 -10
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/README.md +18 -9
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/store.py +88 -63
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/webcontrol.py +2 -2
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/conftest.py +6 -6
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_rotation.py +3 -3
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_settings.py +123 -43
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/.github/workflows/release.yaml +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/.github/workflows/validate.yaml +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/.gitignore +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/.python-version +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/LICENSE +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/mypy.ini +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pyproject.toml +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pytest.ini +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/__init__.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/action.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/const.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/destination.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/exceptions.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/py.typed +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/refresh.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/room.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/scene.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/pywmswebcontrolpro/travel.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/requirements_lint.txt +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/requirements_test.txt +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/ruff.toml +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/simulator/simulator.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/__init__.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_client.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_configuration.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_import.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_refresh.py +0 -0
- {pywmswebcontrolpro-0.3.0 → pywmswebcontrolpro-0.4.0}/tests/test_travel.py +0 -0
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.4.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
Breaking changes:
|
|
6
|
+
|
|
7
|
+
- The stored data is kept for each destination instead of for the whole hub:
|
|
8
|
+
`<destination id>/settings.json` and `<destination id>/state.json` in the persist
|
|
9
|
+
directory, with an entry for each action below `actions`. The files `settings.json`
|
|
10
|
+
and `state.json` of 0.3.0 in the persist directory itself are not read and not
|
|
11
|
+
removed, travel times and changed rotation limits have to be set again.
|
|
12
|
+
|
|
13
|
+
Changed:
|
|
14
|
+
|
|
15
|
+
- A change only writes the file of its destination.
|
|
16
|
+
- A file without entries is removed, and with the last file the directory of the
|
|
17
|
+
destination.
|
|
18
|
+
- The files of a destination are read when `WebControlPro.refresh()` creates it.
|
|
19
|
+
|
|
20
|
+
## 0.3.0
|
|
4
21
|
|
|
5
22
|
Breaking changes:
|
|
6
23
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: pywmswebcontrolpro
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Python library for the Warema WMS WebControl pro API
|
|
5
5
|
Author-email: Marc Hoersken <info@marc-hoersken.de>
|
|
6
6
|
Maintainer: zomtec
|
|
@@ -74,8 +74,13 @@ cannot encrypt or authenticate the connection. Run the gateway in a trusted netw
|
|
|
74
74
|
|
|
75
75
|
## Settings, movement model and stored state
|
|
76
76
|
|
|
77
|
-
The client keeps the data of
|
|
78
|
-
directory:
|
|
77
|
+
The client keeps the data of the actions in the `persist` directory. Each destination
|
|
78
|
+
has a directory of its own, named by its id, with two files:
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
<persist>/<destination id>/settings.json
|
|
82
|
+
<persist>/<destination id>/state.json
|
|
83
|
+
```
|
|
79
84
|
|
|
80
85
|
- `settings.json`: what was set or learned and cannot be measured again. For a drive
|
|
81
86
|
action (for example `AwningDrive`, `ValanceDrive` or `RollerShutterBlindDrive`) these
|
|
@@ -85,19 +90,23 @@ directory:
|
|
|
85
90
|
- `state.json`: the last known position and tilt of a drive action and the target of a
|
|
86
91
|
running drive.
|
|
87
92
|
|
|
88
|
-
Both files have an entry for each
|
|
93
|
+
Both files have an entry for each action of the destination, and only for data that
|
|
89
94
|
differs from the defaults:
|
|
90
95
|
|
|
91
96
|
```json
|
|
92
|
-
{"version": 1, "
|
|
97
|
+
{"version": 1, "actions": {"0": {"settings": {"travel_time_open": 39.0, "travel_time_close": 39.0}}, "6": {"overwrites": {"minValue": -70}}}}
|
|
93
98
|
```
|
|
94
99
|
|
|
100
|
+
A file without entries is removed, and with the last file the directory of the
|
|
101
|
+
destination.
|
|
102
|
+
|
|
95
103
|
The store of the client (`pywmswebcontrolpro/store.py`) is the only place that reads
|
|
96
|
-
and writes the files. It reads
|
|
97
|
-
action has its stored data from then on.
|
|
98
|
-
atomically, changes that arrive during a write
|
|
99
|
-
A drive only writes `state.json
|
|
100
|
-
|
|
104
|
+
and writes the files. It reads the files of a destination once, when
|
|
105
|
+
`WebControlPro.refresh()` creates it, an action has its stored data from then on.
|
|
106
|
+
Writes are serialized and replace a file atomically, changes that arrive during a write
|
|
107
|
+
are stored together with the next one. A drive only writes the `state.json` of its
|
|
108
|
+
destination. Files of destinations that the hub does not report are not touched.
|
|
109
|
+
Without a persist path nothing is stored.
|
|
101
110
|
|
|
102
111
|
`Action.model` is the movement model of the cover driven by this action: `start_move`,
|
|
103
112
|
`stop`, `set_tilt` and `apply_measurement` calculate the displayed position, which is
|
|
@@ -60,8 +60,13 @@ cannot encrypt or authenticate the connection. Run the gateway in a trusted netw
|
|
|
60
60
|
|
|
61
61
|
## Settings, movement model and stored state
|
|
62
62
|
|
|
63
|
-
The client keeps the data of
|
|
64
|
-
directory:
|
|
63
|
+
The client keeps the data of the actions in the `persist` directory. Each destination
|
|
64
|
+
has a directory of its own, named by its id, with two files:
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
<persist>/<destination id>/settings.json
|
|
68
|
+
<persist>/<destination id>/state.json
|
|
69
|
+
```
|
|
65
70
|
|
|
66
71
|
- `settings.json`: what was set or learned and cannot be measured again. For a drive
|
|
67
72
|
action (for example `AwningDrive`, `ValanceDrive` or `RollerShutterBlindDrive`) these
|
|
@@ -71,19 +76,23 @@ directory:
|
|
|
71
76
|
- `state.json`: the last known position and tilt of a drive action and the target of a
|
|
72
77
|
running drive.
|
|
73
78
|
|
|
74
|
-
Both files have an entry for each
|
|
79
|
+
Both files have an entry for each action of the destination, and only for data that
|
|
75
80
|
differs from the defaults:
|
|
76
81
|
|
|
77
82
|
```json
|
|
78
|
-
{"version": 1, "
|
|
83
|
+
{"version": 1, "actions": {"0": {"settings": {"travel_time_open": 39.0, "travel_time_close": 39.0}}, "6": {"overwrites": {"minValue": -70}}}}
|
|
79
84
|
```
|
|
80
85
|
|
|
86
|
+
A file without entries is removed, and with the last file the directory of the
|
|
87
|
+
destination.
|
|
88
|
+
|
|
81
89
|
The store of the client (`pywmswebcontrolpro/store.py`) is the only place that reads
|
|
82
|
-
and writes the files. It reads
|
|
83
|
-
action has its stored data from then on.
|
|
84
|
-
atomically, changes that arrive during a write
|
|
85
|
-
A drive only writes `state.json
|
|
86
|
-
|
|
90
|
+
and writes the files. It reads the files of a destination once, when
|
|
91
|
+
`WebControlPro.refresh()` creates it, an action has its stored data from then on.
|
|
92
|
+
Writes are serialized and replace a file atomically, changes that arrive during a write
|
|
93
|
+
are stored together with the next one. A drive only writes the `state.json` of its
|
|
94
|
+
destination. Files of destinations that the hub does not report are not touched.
|
|
95
|
+
Without a persist path nothing is stored.
|
|
87
96
|
|
|
88
97
|
`Action.model` is the movement model of the cover driven by this action: `start_move`,
|
|
89
98
|
`stop`, `set_tilt` and `apply_measurement` calculate the displayed position, which is
|
|
@@ -7,6 +7,7 @@ import json
|
|
|
7
7
|
import logging
|
|
8
8
|
import os
|
|
9
9
|
import tempfile
|
|
10
|
+
from collections.abc import Iterable
|
|
10
11
|
from dataclasses import dataclass, fields
|
|
11
12
|
from enum import StrEnum
|
|
12
13
|
from pathlib import Path
|
|
@@ -21,7 +22,7 @@ STORE_VERSION = 1
|
|
|
21
22
|
SETTINGS_FILE = "settings.json"
|
|
22
23
|
STATE_FILE = "state.json"
|
|
23
24
|
|
|
24
|
-
type _Entries = dict[str, dict[str,
|
|
25
|
+
type _Entries = dict[str, dict[str, Any]]
|
|
25
26
|
|
|
26
27
|
|
|
27
28
|
class TravelDirection(StrEnum):
|
|
@@ -74,19 +75,14 @@ def _valid_entries(path: Path, data: Any) -> _Entries:
|
|
|
74
75
|
if not isinstance(data, dict) or data.get("version") != STORE_VERSION:
|
|
75
76
|
_LOGGER.warning("Ignoring file %s with an unknown format", path)
|
|
76
77
|
return {}
|
|
77
|
-
|
|
78
|
-
if not isinstance(
|
|
78
|
+
actions = data.get("actions")
|
|
79
|
+
if not isinstance(actions, dict):
|
|
79
80
|
return {}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if isinstance(entry, dict) and entry
|
|
85
|
-
}
|
|
86
|
-
for dest_id, actions in destinations.items()
|
|
87
|
-
if isinstance(actions, dict)
|
|
81
|
+
return {
|
|
82
|
+
action_id: entry
|
|
83
|
+
for action_id, entry in actions.items()
|
|
84
|
+
if isinstance(entry, dict) and entry
|
|
88
85
|
}
|
|
89
|
-
return {dest_id: actions for dest_id, actions in entries.items() if actions}
|
|
90
86
|
|
|
91
87
|
|
|
92
88
|
async def _read(path: Path) -> _Entries:
|
|
@@ -121,6 +117,13 @@ def _write(path: Path, content: str) -> None:
|
|
|
121
117
|
raise
|
|
122
118
|
|
|
123
119
|
|
|
120
|
+
def _remove(path: Path) -> None:
|
|
121
|
+
path.unlink(missing_ok=True)
|
|
122
|
+
# The directory of a destination only exists while it has a file.
|
|
123
|
+
with contextlib.suppress(OSError):
|
|
124
|
+
path.parent.rmdir()
|
|
125
|
+
|
|
126
|
+
|
|
124
127
|
class _Document:
|
|
125
128
|
"""Content of one file, an entry for each action that has data."""
|
|
126
129
|
|
|
@@ -129,76 +132,99 @@ class _Document:
|
|
|
129
132
|
self.entries: _Entries = {}
|
|
130
133
|
self.dirty = False
|
|
131
134
|
|
|
132
|
-
def get(self,
|
|
135
|
+
def get(self, action_id: int) -> dict[str, Any]:
|
|
133
136
|
"""Return a copy of the entry of an action, empty if there is none."""
|
|
134
|
-
|
|
135
|
-
return copy.deepcopy(entry)
|
|
137
|
+
return copy.deepcopy(self.entries.get(str(action_id), {}))
|
|
136
138
|
|
|
137
|
-
def put(self,
|
|
139
|
+
def put(self, action_id: int, entry: dict[str, Any]) -> None:
|
|
138
140
|
"""Take over the entry of an action, an empty entry removes it."""
|
|
139
|
-
if entry == self.get(
|
|
141
|
+
if entry == self.get(action_id):
|
|
140
142
|
return
|
|
141
143
|
self.dirty = True
|
|
142
144
|
if entry:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
self.entries[str(action_id)] = copy.deepcopy(entry)
|
|
146
|
+
else:
|
|
147
|
+
del self.entries[str(action_id)]
|
|
148
|
+
|
|
149
|
+
def store(self) -> None:
|
|
150
|
+
"""Write the file, a document without entries has no file."""
|
|
151
|
+
if self.path is None:
|
|
145
152
|
return
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
153
|
+
if not self.entries:
|
|
154
|
+
_remove(self.path)
|
|
155
|
+
return
|
|
156
|
+
content = json.dumps({"version": STORE_VERSION, "actions": self.entries})
|
|
157
|
+
_write(self.path, content)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class _DestinationData:
|
|
161
|
+
"""Both documents of one destination."""
|
|
150
162
|
|
|
151
|
-
def
|
|
152
|
-
|
|
153
|
-
|
|
163
|
+
def __init__(self, directory: Path | None) -> None:
|
|
164
|
+
self.settings = _Document(directory / SETTINGS_FILE if directory else None)
|
|
165
|
+
self.state = _Document(directory / STATE_FILE if directory else None)
|
|
166
|
+
self.loaded = False
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def documents(self) -> tuple[_Document, _Document]:
|
|
170
|
+
"""Return the settings and the state."""
|
|
171
|
+
return (self.settings, self.state)
|
|
154
172
|
|
|
155
173
|
|
|
156
174
|
class HubStore:
|
|
157
175
|
"""Stored data of all actions of one hub and the only place that writes it.
|
|
158
176
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
the
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
177
|
+
Each destination has a directory of its own, named by its id, in the given
|
|
178
|
+
directory. The settings, which change rarely and cannot be measured again,
|
|
179
|
+
and the last known state, which changes with every drive, are kept in two
|
|
180
|
+
files of it. They are read once. An action hands its data over and reads
|
|
181
|
+
what was stored, only data that differs from the defaults gets an entry
|
|
182
|
+
and only a destination with entries has a file. Writes are serialized and
|
|
183
|
+
replace a file atomically. A file that cannot be read or written is logged
|
|
184
|
+
and does not raise. Without a directory nothing is stored.
|
|
166
185
|
"""
|
|
167
186
|
|
|
168
187
|
def __init__(self, directory: Path | None) -> None:
|
|
169
|
-
"""Create the store for the files
|
|
170
|
-
self.
|
|
171
|
-
self.
|
|
188
|
+
"""Create the store for the files below the given directory."""
|
|
189
|
+
self._directory = directory
|
|
190
|
+
self._destinations: dict[int, _DestinationData] = {}
|
|
172
191
|
self._lock = asyncio.Lock()
|
|
173
|
-
self._loaded = False
|
|
174
192
|
|
|
175
|
-
|
|
176
|
-
|
|
193
|
+
def _destination(self, dest_id: int) -> _DestinationData:
|
|
194
|
+
if dest_id not in self._destinations:
|
|
195
|
+
directory = self._directory / str(dest_id) if self._directory else None
|
|
196
|
+
self._destinations[dest_id] = _DestinationData(directory)
|
|
197
|
+
return self._destinations[dest_id]
|
|
198
|
+
|
|
199
|
+
async def load(self, dest_ids: Iterable[int]) -> None:
|
|
200
|
+
"""Read the files of the given destinations, those of each only once."""
|
|
177
201
|
async with self._lock:
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
202
|
+
for dest_id in dest_ids:
|
|
203
|
+
destination = self._destination(dest_id)
|
|
204
|
+
if destination.loaded:
|
|
205
|
+
continue
|
|
206
|
+
for document in destination.documents:
|
|
207
|
+
if document.path is not None:
|
|
208
|
+
document.entries = await _read(document.path)
|
|
209
|
+
destination.loaded = True
|
|
184
210
|
|
|
185
211
|
def settings(self, dest_id: int, action_id: int) -> dict[str, Any]:
|
|
186
212
|
"""Return the stored settings of an action, empty if there are none."""
|
|
187
|
-
return self.
|
|
213
|
+
return self._destination(dest_id).settings.get(action_id)
|
|
188
214
|
|
|
189
215
|
def state(self, dest_id: int, action_id: int) -> dict[str, Any]:
|
|
190
216
|
"""Return the stored state of an action, empty if there is none."""
|
|
191
|
-
return self.
|
|
217
|
+
return self._destination(dest_id).state.get(action_id)
|
|
192
218
|
|
|
193
219
|
def set_settings(
|
|
194
220
|
self, dest_id: int, action_id: int, settings: dict[str, Any]
|
|
195
221
|
) -> None:
|
|
196
222
|
"""Take over the settings of an action for the next write."""
|
|
197
|
-
self.
|
|
223
|
+
self._destination(dest_id).settings.put(action_id, settings)
|
|
198
224
|
|
|
199
225
|
def set_state(self, dest_id: int, action_id: int, state: dict[str, Any]) -> None:
|
|
200
226
|
"""Take over the state of an action for the next write."""
|
|
201
|
-
self.
|
|
227
|
+
self._destination(dest_id).state.put(action_id, state)
|
|
202
228
|
|
|
203
229
|
async def flush(self) -> bool:
|
|
204
230
|
"""Write the files that have changes, return False if a write failed.
|
|
@@ -207,18 +233,17 @@ class HubStore:
|
|
|
207
233
|
"""
|
|
208
234
|
written = True
|
|
209
235
|
async with self._lock:
|
|
210
|
-
for
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
written = False
|
|
236
|
+
for destination in list(self._destinations.values()):
|
|
237
|
+
for document in destination.documents:
|
|
238
|
+
if not document.dirty:
|
|
239
|
+
continue
|
|
240
|
+
# Cleared before the write, so a change made during it is
|
|
241
|
+
# written by the next call.
|
|
242
|
+
document.dirty = False
|
|
243
|
+
try:
|
|
244
|
+
await asyncio.to_thread(document.store)
|
|
245
|
+
except OSError as err:
|
|
246
|
+
_LOGGER.warning("Could not write %s: %r", document.path, err)
|
|
247
|
+
document.dirty = True
|
|
248
|
+
written = False
|
|
224
249
|
return written
|
|
@@ -201,10 +201,10 @@ class WebControlPro:
|
|
|
201
201
|
name. Rooms and scenes are created again.
|
|
202
202
|
"""
|
|
203
203
|
config = await self._getConfiguration()
|
|
204
|
-
# New actions read their stored data when they are created.
|
|
205
|
-
await self._store.load()
|
|
206
204
|
known = self._dests
|
|
207
205
|
try:
|
|
206
|
+
# New actions read their stored data when they are created.
|
|
207
|
+
await self._store.load([int(dest["id"]) for dest in config["destinations"]])
|
|
208
208
|
dests = {
|
|
209
209
|
dest["id"]: known.get(dest["id"]) or Destination(self, **dest)
|
|
210
210
|
for dest in config["destinations"]
|
|
@@ -72,14 +72,14 @@ STATUS_EMPTY: dict[str, Any] = {}
|
|
|
72
72
|
STATUS_NO_DETAILS = {"details": []}
|
|
73
73
|
|
|
74
74
|
|
|
75
|
-
def stored_settings(persist: Path) -> Any:
|
|
76
|
-
"""Return the settings in the file
|
|
77
|
-
return json.loads((persist / SETTINGS_FILE).read_text())["
|
|
75
|
+
def stored_settings(persist: Path, dest_id: int = DEST_ID) -> Any:
|
|
76
|
+
"""Return the settings in the file of a destination by action."""
|
|
77
|
+
return json.loads((persist / str(dest_id) / SETTINGS_FILE).read_text())["actions"]
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
def stored_state(persist: Path) -> Any:
|
|
81
|
-
"""Return the states in the file
|
|
82
|
-
return json.loads((persist / STATE_FILE).read_text())["
|
|
80
|
+
def stored_state(persist: Path, dest_id: int = DEST_ID) -> Any:
|
|
81
|
+
"""Return the states in the file of a destination by action."""
|
|
82
|
+
return json.loads((persist / str(dest_id) / STATE_FILE).read_text())["actions"]
|
|
83
83
|
|
|
84
84
|
|
|
85
85
|
class FakeResponse:
|
|
@@ -200,8 +200,8 @@ async def test_rotation_range_can_be_set_and_reset(
|
|
|
200
200
|
|
|
201
201
|
await action.set_rotation_range(minimum=-60)
|
|
202
202
|
assert action.rotation_range == (-60, 75)
|
|
203
|
-
assert stored_settings(tmp_path) == {
|
|
204
|
-
str(
|
|
203
|
+
assert stored_settings(tmp_path, SLAT_DEST_ID) == {
|
|
204
|
+
str(ROTATE_ID): {"overwrites": {"minValue": -60}}
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
await action.set_rotation_range(maximum=70)
|
|
@@ -209,4 +209,4 @@ async def test_rotation_range_can_be_set_and_reset(
|
|
|
209
209
|
|
|
210
210
|
await action.reset_rotation_range()
|
|
211
211
|
assert action.rotation_range == (-75, 75)
|
|
212
|
-
assert
|
|
212
|
+
assert not (tmp_path / str(SLAT_DEST_ID)).exists()
|
|
@@ -24,14 +24,17 @@ from .conftest import (
|
|
|
24
24
|
ACTION_ID,
|
|
25
25
|
DEST_ID,
|
|
26
26
|
SECOND_ACTION_ID,
|
|
27
|
+
FakeGateway,
|
|
27
28
|
stored_settings,
|
|
28
29
|
stored_state,
|
|
29
30
|
)
|
|
30
31
|
|
|
31
32
|
type ControlFactory = Callable[..., WebControlPro]
|
|
32
33
|
|
|
33
|
-
|
|
34
|
+
OTHER_DEST_ID = 99
|
|
34
35
|
ACTION = str(ACTION_ID)
|
|
36
|
+
DEST_SETTINGS = f"{DEST_ID}/{SETTINGS_FILE}"
|
|
37
|
+
DEST_STATE = f"{DEST_ID}/{STATE_FILE}"
|
|
35
38
|
|
|
36
39
|
|
|
37
40
|
async def new_action(
|
|
@@ -45,10 +48,17 @@ async def new_action(
|
|
|
45
48
|
return control.dests[DEST_ID].actions[action_id]
|
|
46
49
|
|
|
47
50
|
|
|
51
|
+
def dest_dir(persist: Path, dest_id: int = DEST_ID) -> Path:
|
|
52
|
+
"""Return the directory with the files of a destination."""
|
|
53
|
+
return persist / str(dest_id)
|
|
54
|
+
|
|
55
|
+
|
|
48
56
|
def write_file(persist: Path, name: str, entry: object) -> None:
|
|
49
57
|
"""Write a stored file with one entry for the test action."""
|
|
50
|
-
(persist
|
|
51
|
-
|
|
58
|
+
directory = dest_dir(persist)
|
|
59
|
+
directory.mkdir(exist_ok=True)
|
|
60
|
+
(directory / name).write_text(
|
|
61
|
+
json.dumps({"version": STORE_VERSION, "actions": {ACTION: entry}})
|
|
52
62
|
)
|
|
53
63
|
|
|
54
64
|
|
|
@@ -63,12 +73,12 @@ def temporary_files(path: Path) -> list[Path]:
|
|
|
63
73
|
|
|
64
74
|
|
|
65
75
|
def count_writes(monkeypatch: pytest.MonkeyPatch) -> list[str]:
|
|
66
|
-
"""Return a list that collects
|
|
76
|
+
"""Return a list that collects every written file as destination/name."""
|
|
67
77
|
writes: list[str] = []
|
|
68
78
|
original = store._write
|
|
69
79
|
|
|
70
80
|
def counting_write(path: Path, content: str) -> None:
|
|
71
|
-
writes.append(path.name)
|
|
81
|
+
writes.append(f"{path.parent.name}/{path.name}")
|
|
72
82
|
original(path, content)
|
|
73
83
|
|
|
74
84
|
monkeypatch.setattr(store, "_write", counting_write)
|
|
@@ -161,8 +171,8 @@ async def test_drive_actions_are_independent(
|
|
|
161
171
|
|
|
162
172
|
assert second.settings == ActionSettings()
|
|
163
173
|
assert second.model.position is None
|
|
164
|
-
assert list(stored_settings(tmp_path)
|
|
165
|
-
assert list(stored_state(tmp_path)
|
|
174
|
+
assert list(stored_settings(tmp_path)) == [ACTION]
|
|
175
|
+
assert list(stored_state(tmp_path)) == [ACTION]
|
|
166
176
|
|
|
167
177
|
|
|
168
178
|
async def test_settings_and_state_are_stored_in_their_own_file(
|
|
@@ -175,11 +185,9 @@ async def test_settings_and_state_are_stored_in_their_own_file(
|
|
|
175
185
|
await action.sync()
|
|
176
186
|
|
|
177
187
|
assert stored_settings(tmp_path) == {
|
|
178
|
-
|
|
179
|
-
ACTION: {"settings": {"travel_time_open": 40.0, "travel_time_close": 0.0}}
|
|
180
|
-
}
|
|
188
|
+
ACTION: {"settings": {"travel_time_open": 40.0, "travel_time_close": 0.0}}
|
|
181
189
|
}
|
|
182
|
-
assert stored_state(tmp_path) == {
|
|
190
|
+
assert stored_state(tmp_path) == {ACTION: {"position": 70}}
|
|
183
191
|
|
|
184
192
|
|
|
185
193
|
async def test_state_change_does_not_write_the_settings(
|
|
@@ -195,7 +203,7 @@ async def test_state_change_does_not_write_the_settings(
|
|
|
195
203
|
action.apply_measurement(0.0, 70, None)
|
|
196
204
|
await action.sync()
|
|
197
205
|
|
|
198
|
-
assert writes == [
|
|
206
|
+
assert writes == [DEST_STATE]
|
|
199
207
|
|
|
200
208
|
|
|
201
209
|
async def test_state_survives_restart(
|
|
@@ -258,7 +266,7 @@ async def test_state_is_written_only_on_change(
|
|
|
258
266
|
await action.sync()
|
|
259
267
|
await action.sync()
|
|
260
268
|
|
|
261
|
-
assert writes == [
|
|
269
|
+
assert writes == [DEST_STATE]
|
|
262
270
|
|
|
263
271
|
|
|
264
272
|
async def test_destination_sync_writes_each_file_once(
|
|
@@ -276,25 +284,83 @@ async def test_destination_sync_writes_each_file_once(
|
|
|
276
284
|
dest.actions[SECOND_ACTION_ID].apply_measurement(0.0, 20, None)
|
|
277
285
|
await dest.sync()
|
|
278
286
|
|
|
279
|
-
assert writes == [
|
|
287
|
+
assert writes == [DEST_STATE]
|
|
280
288
|
assert stored_state(tmp_path) == {
|
|
281
|
-
|
|
289
|
+
ACTION: {"position": 10},
|
|
290
|
+
str(SECOND_ACTION_ID): {"position": 20},
|
|
282
291
|
}
|
|
283
292
|
|
|
284
293
|
|
|
285
|
-
async def
|
|
294
|
+
async def test_each_destination_has_its_own_files(
|
|
295
|
+
make_control: ControlFactory,
|
|
296
|
+
gateway: FakeGateway,
|
|
297
|
+
tmp_path: Path,
|
|
298
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
299
|
+
) -> None:
|
|
300
|
+
"""A change of one destination does not touch the files of another."""
|
|
301
|
+
gateway.config = {
|
|
302
|
+
**gateway.config,
|
|
303
|
+
"destinations": [
|
|
304
|
+
gateway.config["destinations"][0],
|
|
305
|
+
{**gateway.config["destinations"][0], "id": OTHER_DEST_ID},
|
|
306
|
+
],
|
|
307
|
+
}
|
|
308
|
+
control = make_control(persist=str(tmp_path))
|
|
309
|
+
await control.refresh()
|
|
310
|
+
first = control.dests[DEST_ID].actions[ACTION_ID]
|
|
311
|
+
other = control.dests[OTHER_DEST_ID].actions[ACTION_ID]
|
|
312
|
+
await first.set_travel_time(TravelDirection.OPEN, 30)
|
|
313
|
+
writes = count_writes(monkeypatch)
|
|
314
|
+
|
|
315
|
+
await other.set_travel_time(TravelDirection.OPEN, 20)
|
|
316
|
+
|
|
317
|
+
assert writes == [f"{OTHER_DEST_ID}/{SETTINGS_FILE}"]
|
|
318
|
+
assert stored_settings(tmp_path)[ACTION]["settings"]["travel_time_open"] == 30.0
|
|
319
|
+
assert stored_settings(tmp_path, OTHER_DEST_ID) == {
|
|
320
|
+
ACTION: {"settings": {"travel_time_open": 20.0, "travel_time_close": 0.0}}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
async def test_files_of_an_unknown_destination_are_kept(
|
|
286
325
|
make_control: ControlFactory, tmp_path: Path
|
|
287
326
|
) -> None:
|
|
288
|
-
"""Data of a destination that the hub does not report
|
|
289
|
-
|
|
290
|
-
(
|
|
291
|
-
|
|
292
|
-
)
|
|
327
|
+
"""Data of a destination that the hub does not report is not touched."""
|
|
328
|
+
unknown = dest_dir(tmp_path, OTHER_DEST_ID) / SETTINGS_FILE
|
|
329
|
+
unknown.parent.mkdir()
|
|
330
|
+
unknown.write_text("kept")
|
|
293
331
|
action = await new_action(make_control, tmp_path)
|
|
294
332
|
|
|
295
333
|
await action.set_travel_time(TravelDirection.OPEN, 30)
|
|
296
334
|
|
|
297
|
-
assert
|
|
335
|
+
assert unknown.read_text() == "kept"
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
async def test_destination_added_later_reads_its_files(
|
|
339
|
+
make_control: ControlFactory, gateway: FakeGateway, tmp_path: Path
|
|
340
|
+
) -> None:
|
|
341
|
+
"""A destination that appears with a later refresh has its stored data."""
|
|
342
|
+
control = make_control(persist=str(tmp_path))
|
|
343
|
+
await control.refresh()
|
|
344
|
+
await (
|
|
345
|
+
control.dests[DEST_ID]
|
|
346
|
+
.actions[ACTION_ID]
|
|
347
|
+
.set_travel_time(TravelDirection.OPEN, 30)
|
|
348
|
+
)
|
|
349
|
+
other = dest_dir(tmp_path, OTHER_DEST_ID)
|
|
350
|
+
other.mkdir()
|
|
351
|
+
(other / SETTINGS_FILE).write_text((dest_dir(tmp_path) / SETTINGS_FILE).read_text())
|
|
352
|
+
gateway.config = {
|
|
353
|
+
**gateway.config,
|
|
354
|
+
"destinations": [
|
|
355
|
+
gateway.config["destinations"][0],
|
|
356
|
+
{**gateway.config["destinations"][0], "id": OTHER_DEST_ID},
|
|
357
|
+
],
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
await control.refresh()
|
|
361
|
+
|
|
362
|
+
added = control.dests[OTHER_DEST_ID].actions[ACTION_ID]
|
|
363
|
+
assert added.settings.travel_time_open == 30.0
|
|
298
364
|
|
|
299
365
|
|
|
300
366
|
async def test_overwrite_change_is_saved_with_settings(
|
|
@@ -308,16 +374,16 @@ async def test_overwrite_change_is_saved_with_settings(
|
|
|
308
374
|
action["minValue"] = -75
|
|
309
375
|
await action.sync()
|
|
310
376
|
|
|
311
|
-
assert stored_settings(tmp_path)[
|
|
377
|
+
assert stored_settings(tmp_path)[ACTION] == {
|
|
312
378
|
"overwrites": {"minValue": -75},
|
|
313
379
|
"settings": {"travel_time_open": 30.0, "travel_time_close": 0.0},
|
|
314
380
|
}
|
|
315
381
|
|
|
316
382
|
|
|
317
|
-
async def
|
|
383
|
+
async def test_destination_without_data_has_no_file(
|
|
318
384
|
make_control: ControlFactory, tmp_path: Path
|
|
319
385
|
) -> None:
|
|
320
|
-
"""
|
|
386
|
+
"""Removing the last entry removes the file and the empty directory."""
|
|
321
387
|
action = await new_action(make_control, tmp_path)
|
|
322
388
|
action._attrs["minValue"] = 0
|
|
323
389
|
action["minValue"] = -75
|
|
@@ -326,7 +392,22 @@ async def test_removed_overwrite_is_removed_from_the_file(
|
|
|
326
392
|
del action["minValue"]
|
|
327
393
|
await action.sync()
|
|
328
394
|
|
|
329
|
-
assert
|
|
395
|
+
assert is_empty(tmp_path)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
async def test_removed_settings_keep_the_state_file(
|
|
399
|
+
make_control: ControlFactory, tmp_path: Path
|
|
400
|
+
) -> None:
|
|
401
|
+
"""Only the file without entries is removed, the other one stays."""
|
|
402
|
+
action = await new_action(make_control, tmp_path)
|
|
403
|
+
await action.set_travel_time(TravelDirection.OPEN, 30)
|
|
404
|
+
action.apply_measurement(0.0, 70, None)
|
|
405
|
+
await action.sync()
|
|
406
|
+
|
|
407
|
+
await action.set_travel_time(TravelDirection.OPEN, 0)
|
|
408
|
+
|
|
409
|
+
assert not (dest_dir(tmp_path) / SETTINGS_FILE).exists()
|
|
410
|
+
assert stored_state(tmp_path) == {ACTION: {"position": 70}}
|
|
330
411
|
|
|
331
412
|
|
|
332
413
|
async def test_concurrent_writes_keep_a_valid_file(
|
|
@@ -342,11 +423,11 @@ async def test_concurrent_writes_keep_a_valid_file(
|
|
|
342
423
|
*(action.set_travel_time(TravelDirection.OPEN, i) for i in range(1, 41))
|
|
343
424
|
)
|
|
344
425
|
|
|
345
|
-
stored = stored_settings(tmp_path)[
|
|
426
|
+
stored = stored_settings(tmp_path)[ACTION]["settings"]
|
|
346
427
|
assert stored["travel_time_open"] == action.settings.travel_time_open == 40.0
|
|
347
|
-
assert not temporary_files(tmp_path)
|
|
428
|
+
assert not temporary_files(dest_dir(tmp_path))
|
|
348
429
|
# The first change is written at once, all later ones with a second write.
|
|
349
|
-
assert writes == [
|
|
430
|
+
assert writes == [DEST_SETTINGS, DEST_SETTINGS]
|
|
350
431
|
|
|
351
432
|
|
|
352
433
|
@pytest.mark.parametrize(
|
|
@@ -354,22 +435,20 @@ async def test_concurrent_writes_keep_a_valid_file(
|
|
|
354
435
|
[
|
|
355
436
|
"not json",
|
|
356
437
|
"[]",
|
|
357
|
-
'{"version": 1, "
|
|
358
|
-
'{"version": 1, "
|
|
359
|
-
'{"version": 1, "
|
|
360
|
-
'{"version": 1, "
|
|
361
|
-
'{"version": 1, "
|
|
362
|
-
'{"version":
|
|
363
|
-
'{"settings": {"travel_time_open": -5}}}}}',
|
|
364
|
-
'{"version": 2, "destinations": {"7": {"1": '
|
|
365
|
-
'{"settings": {"travel_time_open": 5}}}}}',
|
|
438
|
+
'{"version": 1, "actions": []}',
|
|
439
|
+
'{"version": 1, "actions": {"1": "x"}}',
|
|
440
|
+
'{"version": 1, "actions": {"1": {"settings": "x"}}}',
|
|
441
|
+
'{"version": 1, "actions": {"1": {"overwrites": "x"}}}',
|
|
442
|
+
'{"version": 1, "actions": {"1": {"settings": {"travel_time_open": -5}}}}',
|
|
443
|
+
'{"version": 2, "actions": {"1": {"settings": {"travel_time_open": 5}}}}',
|
|
366
444
|
],
|
|
367
445
|
)
|
|
368
446
|
async def test_unusable_file_falls_back_to_defaults(
|
|
369
447
|
make_control: ControlFactory, tmp_path: Path, content: str
|
|
370
448
|
) -> None:
|
|
371
449
|
"""A broken or invalid file does not break the action."""
|
|
372
|
-
(tmp_path
|
|
450
|
+
dest_dir(tmp_path).mkdir()
|
|
451
|
+
(dest_dir(tmp_path) / SETTINGS_FILE).write_text(content)
|
|
373
452
|
|
|
374
453
|
action = await new_action(make_control, tmp_path)
|
|
375
454
|
await action.sync()
|
|
@@ -409,7 +488,8 @@ async def test_file_that_is_no_text_falls_back_to_defaults(
|
|
|
409
488
|
make_control: ControlFactory, tmp_path: Path
|
|
410
489
|
) -> None:
|
|
411
490
|
"""A file with broken encoding is ignored like broken JSON."""
|
|
412
|
-
(tmp_path
|
|
491
|
+
dest_dir(tmp_path).mkdir()
|
|
492
|
+
(dest_dir(tmp_path) / SETTINGS_FILE).write_bytes(b"\xff\xfe\x00")
|
|
413
493
|
|
|
414
494
|
action = await new_action(make_control, tmp_path)
|
|
415
495
|
await action.sync()
|
|
@@ -454,12 +534,12 @@ async def test_failed_write_is_tried_again_with_the_next_sync(
|
|
|
454
534
|
monkeypatch.setattr(os, "replace", fail)
|
|
455
535
|
await action.set_travel_time(TravelDirection.OPEN, 12.0)
|
|
456
536
|
assert "Could not write" in caplog.text
|
|
457
|
-
assert is_empty(tmp_path)
|
|
537
|
+
assert is_empty(dest_dir(tmp_path))
|
|
458
538
|
|
|
459
539
|
monkeypatch.setattr(os, "replace", replace)
|
|
460
540
|
await action.sync()
|
|
461
541
|
|
|
462
|
-
stored = stored_settings(tmp_path)[
|
|
542
|
+
stored = stored_settings(tmp_path)[ACTION]["settings"]
|
|
463
543
|
assert stored["travel_time_open"] == 12.0
|
|
464
544
|
|
|
465
545
|
|
|
@@ -489,5 +569,5 @@ async def test_change_during_a_write_is_stored_with_the_next_write(
|
|
|
489
569
|
release.set()
|
|
490
570
|
await asyncio.gather(first, second)
|
|
491
571
|
|
|
492
|
-
stored = stored_settings(tmp_path)[
|
|
572
|
+
stored = stored_settings(tmp_path)[ACTION]["settings"]
|
|
493
573
|
assert stored["travel_time_open"] == 20.0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|