nwp500-python 9.12.2__py3-none-any.whl → 9.13.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.
- nwp500/__init__.py +1 -1
- nwp500/ble/mqtt_adapter.py +13 -0
- nwp500/cli/__main__.py +65 -16
- nwp500/cli/calibration.py +147 -18
- nwp500/cli/handlers.py +48 -7
- nwp500/cli/simulate.py +48 -45
- nwp500/converters.py +39 -5
- nwp500/device_capabilities.py +6 -4
- nwp500/models/feature.py +9 -3
- nwp500/models/status.py +27 -12
- nwp500/mqtt/_device_subscriptions.py +13 -13
- nwp500/mqtt/client.py +25 -22
- nwp500/mqtt/connection.py +137 -30
- nwp500/mqtt/control.py +17 -2
- nwp500/mqtt/periodic.py +28 -0
- nwp500/mqtt/subscriptions.py +371 -150
- nwp500/mqtt/utils.py +153 -2
- nwp500/sim/__init__.py +18 -8
- nwp500/sim/api.py +1226 -661
- nwp500/sim/calibration.py +368 -2688
- nwp500/sim/carry.py +492 -0
- nwp500/sim/circulation.py +53 -252
- nwp500/sim/codec.py +15 -1
- nwp500/sim/cold_supply.py +61 -17
- nwp500/sim/controller.py +516 -89
- nwp500/sim/decay.py +106 -893
- nwp500/sim/defaults.py +287 -98
- nwp500/sim/delivery.py +157 -402
- nwp500/sim/draw_ensemble.py +406 -0
- nwp500/sim/draw_forecast.py +15 -10
- nwp500/sim/engine.py +1187 -1030
- nwp500/sim/extensions.py +1 -163
- nwp500/sim/heat_pump_fit.py +1016 -0
- nwp500/sim/heat_pump_recovery.py +61 -211
- nwp500/sim/heating_trajectory.py +58 -305
- nwp500/sim/history.py +249 -0
- nwp500/sim/history_frame.py +97 -0
- nwp500/sim/inputs.py +67 -9
- nwp500/sim/margins.py +272 -0
- nwp500/sim/params.py +601 -545
- nwp500/sim/physics/__init__.py +3 -2
- nwp500/sim/physics/draw.py +39 -410
- nwp500/sim/physics/element.py +20 -484
- nwp500/sim/physics/heat_pump.py +182 -0
- nwp500/sim/physics/stratified.py +1440 -0
- nwp500/sim/physics/water.py +86 -0
- nwp500/sim/profile.py +2 -3
- nwp500/sim/ready.py +111 -409
- nwp500/sim/record.py +22 -3
- nwp500/sim/result.py +74 -21
- nwp500/sim/tank_fit.py +898 -0
- nwp500/sim/tank_physics.py +730 -342
- nwp500/sim/units.py +3 -1
- nwp500/sim_adapters.py +219 -273
- nwp500/sim_calibrate.py +944 -1188
- nwp500/sim_home_assistant.py +155 -141
- nwp500/sim_score.py +2017 -0
- {nwp500_python-9.12.2.dist-info → nwp500_python-9.13.1.dist-info}/METADATA +2 -2
- nwp500_python-9.13.1.dist-info/RECORD +121 -0
- nwp500/sim/book.py +0 -1262
- nwp500/sim/physics/compressor.py +0 -485
- nwp500/sim/physics/refrigerant.py +0 -177
- nwp500/sim/physics/tank.py +0 -140
- nwp500_python-9.12.2.dist-info/RECORD +0 -114
- {nwp500_python-9.12.2.dist-info → nwp500_python-9.13.1.dist-info}/WHEEL +0 -0
- {nwp500_python-9.12.2.dist-info → nwp500_python-9.13.1.dist-info}/entry_points.txt +0 -0
- {nwp500_python-9.12.2.dist-info → nwp500_python-9.13.1.dist-info}/licenses/LICENSE.txt +0 -0
- {nwp500_python-9.12.2.dist-info → nwp500_python-9.13.1.dist-info}/top_level.txt +0 -0
nwp500/__init__.py
CHANGED
|
@@ -25,7 +25,7 @@ finally:
|
|
|
25
25
|
|
|
26
26
|
# The exports load on first use (PEP 562), so a part of the library
|
|
27
27
|
# imports without the others' dependencies: ``nwp500.sim`` needs neither
|
|
28
|
-
# aiohttp nor
|
|
28
|
+
# aiohttp nor awscrt, which the clients load.
|
|
29
29
|
if TYPE_CHECKING:
|
|
30
30
|
from nwp500.api_client import (
|
|
31
31
|
NavienAPIClient,
|
nwp500/ble/mqtt_adapter.py
CHANGED
|
@@ -11,6 +11,7 @@ adapter maps.
|
|
|
11
11
|
|
|
12
12
|
from __future__ import annotations
|
|
13
13
|
|
|
14
|
+
import contextlib
|
|
14
15
|
from collections.abc import Callable
|
|
15
16
|
from dataclasses import dataclass
|
|
16
17
|
from typing import Any
|
|
@@ -155,6 +156,12 @@ class BleMqttAdapter:
|
|
|
155
156
|
self._feature_callbacks.append(callback)
|
|
156
157
|
return 0
|
|
157
158
|
|
|
159
|
+
async def unsubscribe_device_feature(
|
|
160
|
+
self, device: Any, callback: FeatureCallback
|
|
161
|
+
) -> None:
|
|
162
|
+
with contextlib.suppress(ValueError):
|
|
163
|
+
self._feature_callbacks.remove(callback)
|
|
164
|
+
|
|
158
165
|
async def request_device_info(self, device: Any) -> int:
|
|
159
166
|
"""Read the blocks and deliver a :class:`BleDeviceFeature`.
|
|
160
167
|
|
|
@@ -180,6 +187,12 @@ class BleMqttAdapter:
|
|
|
180
187
|
self._status_callbacks.append(callback)
|
|
181
188
|
return 0
|
|
182
189
|
|
|
190
|
+
async def unsubscribe_device_status(
|
|
191
|
+
self, device: Any, callback: StatusCallback
|
|
192
|
+
) -> None:
|
|
193
|
+
with contextlib.suppress(ValueError):
|
|
194
|
+
self._status_callbacks.remove(callback)
|
|
195
|
+
|
|
183
196
|
async def request_device_status(self, device: Any) -> int:
|
|
184
197
|
"""Deliver the next :class:`~nwp500.ble.RealTimeStatus` block.
|
|
185
198
|
|
nwp500/cli/__main__.py
CHANGED
|
@@ -4,6 +4,7 @@ import asyncio
|
|
|
4
4
|
import functools
|
|
5
5
|
import json
|
|
6
6
|
import logging
|
|
7
|
+
import math
|
|
7
8
|
import sys
|
|
8
9
|
from collections.abc import Awaitable, Callable
|
|
9
10
|
from datetime import UTC, datetime
|
|
@@ -58,9 +59,9 @@ async def _detect_unit_system(
|
|
|
58
59
|
if not future.done():
|
|
59
60
|
future.set_result(status)
|
|
60
61
|
|
|
61
|
-
await mqtt.subscribe_device_status(device, _on_status)
|
|
62
|
-
await mqtt.request_device_status(device)
|
|
63
62
|
try:
|
|
63
|
+
await mqtt.subscribe_device_status(device, _on_status)
|
|
64
|
+
await mqtt.request_device_status(device)
|
|
64
65
|
status = await asyncio.wait_for(future, timeout=5.0)
|
|
65
66
|
if status.temperature_type == TemperatureType.CELSIUS:
|
|
66
67
|
_logger.info("Auto-detected metric unit system from device")
|
|
@@ -72,6 +73,10 @@ async def _detect_unit_system(
|
|
|
72
73
|
"Timed out detecting unit system, defaulting to us_customary"
|
|
73
74
|
)
|
|
74
75
|
return "us_customary"
|
|
76
|
+
finally:
|
|
77
|
+
await handlers.unsubscribe_quietly(
|
|
78
|
+
mqtt.unsubscribe_device_status, device, _on_status
|
|
79
|
+
)
|
|
75
80
|
|
|
76
81
|
|
|
77
82
|
def _parse_filter_hours(
|
|
@@ -1134,14 +1139,14 @@ _tz_option = click.option(
|
|
|
1134
1139
|
@click.option(
|
|
1135
1140
|
"--bath",
|
|
1136
1141
|
type=float,
|
|
1137
|
-
help="The bath readiness is judged against,
|
|
1138
|
-
"(default the parameters', 28.7 gal)",
|
|
1142
|
+
help="The bath readiness is judged against, as the meter reads it, "
|
|
1143
|
+
"preferred unit (default the parameters', 28.7 gal)",
|
|
1139
1144
|
)
|
|
1140
1145
|
@click.option(
|
|
1141
1146
|
"--shower",
|
|
1142
1147
|
type=float,
|
|
1143
|
-
help="A shower,
|
|
1144
|
-
"12 gal)",
|
|
1148
|
+
help="A shower, as the meter reads it, preferred unit (default the "
|
|
1149
|
+
"parameters', 12 gal)",
|
|
1145
1150
|
)
|
|
1146
1151
|
@click.option(
|
|
1147
1152
|
"--format",
|
|
@@ -1174,7 +1179,7 @@ def simulate_run(ctx: click.Context, output_format: str, **given: Any) -> None:
|
|
|
1174
1179
|
live = await sim_cli.compose_live(
|
|
1175
1180
|
mqtt, ctx.obj["api"], device, inputs, now
|
|
1176
1181
|
)
|
|
1177
|
-
keeper = await sim_cli.
|
|
1182
|
+
keeper = await sim_cli.read_tank(inputs, now)
|
|
1178
1183
|
except sim_cli.InputError as e:
|
|
1179
1184
|
raise click.UsageError(str(e)) from None
|
|
1180
1185
|
walk(live, keeper)
|
|
@@ -1254,6 +1259,15 @@ async def simulate_record(
|
|
|
1254
1259
|
await handle_recording(mqtt, device, history, every)
|
|
1255
1260
|
|
|
1256
1261
|
|
|
1262
|
+
def _finite_weeks(
|
|
1263
|
+
ctx: click.Context, param: click.Parameter, value: float
|
|
1264
|
+
) -> float:
|
|
1265
|
+
"""``--tank-weeks`` as a finite number of weeks."""
|
|
1266
|
+
if not math.isfinite(value):
|
|
1267
|
+
raise click.BadParameter(f"{value} is not a number of weeks")
|
|
1268
|
+
return value
|
|
1269
|
+
|
|
1270
|
+
|
|
1257
1271
|
@simulate.command("calibrate") # type: ignore[attr-defined]
|
|
1258
1272
|
@click.argument("history", type=click.Path(exists=True, dir_okay=False))
|
|
1259
1273
|
@click.option(
|
|
@@ -1291,6 +1305,25 @@ async def simulate_record(
|
|
|
1291
1305
|
help="A TOML file naming where the air, its humidity, the hot water "
|
|
1292
1306
|
"demand and the circulation are recorded (Home Assistant)",
|
|
1293
1307
|
)
|
|
1308
|
+
@click.option(
|
|
1309
|
+
"--holdout-from",
|
|
1310
|
+
"holdout_from",
|
|
1311
|
+
metavar="DATE",
|
|
1312
|
+
help="Fit on the history before DATE (ISO 8601, local time without "
|
|
1313
|
+
"an offset) and score the base and the fit on the events from it",
|
|
1314
|
+
)
|
|
1315
|
+
@click.option(
|
|
1316
|
+
"--tank-weeks",
|
|
1317
|
+
"tank_weeks",
|
|
1318
|
+
type=click.FloatRange(min=0.0),
|
|
1319
|
+
callback=_finite_weeks,
|
|
1320
|
+
default=7.0,
|
|
1321
|
+
show_default=True,
|
|
1322
|
+
metavar="WEEKS",
|
|
1323
|
+
help="Fit the tank on its events in the last WEEKS of the history "
|
|
1324
|
+
"(to its last status before --holdout-from), the history before read "
|
|
1325
|
+
"as their lead-in; 0 for the whole history",
|
|
1326
|
+
)
|
|
1294
1327
|
@click.pass_context
|
|
1295
1328
|
def simulate_calibrate(
|
|
1296
1329
|
ctx: click.Context,
|
|
@@ -1301,10 +1334,16 @@ def simulate_calibrate(
|
|
|
1301
1334
|
humidity: str | None,
|
|
1302
1335
|
params: str | None,
|
|
1303
1336
|
sources: str | None,
|
|
1337
|
+
holdout_from: str | None,
|
|
1338
|
+
tank_weeks: float,
|
|
1304
1339
|
) -> None:
|
|
1305
1340
|
"""Fit the heater's parameters to its recorded HISTORY.
|
|
1306
1341
|
|
|
1307
1342
|
Prints what each fit found, and why a term kept the base's value.
|
|
1343
|
+
With --holdout-from, the fit sees only the history before it, and
|
|
1344
|
+
the base and the fit are scored on the recorded events after it.
|
|
1345
|
+
The tank is fitted on the last --tank-weeks of what the fit sees,
|
|
1346
|
+
the heat pump on all of it.
|
|
1308
1347
|
"""
|
|
1309
1348
|
sim_cli = _simulator()
|
|
1310
1349
|
from . import calibration as cal_cli
|
|
@@ -1320,21 +1359,31 @@ def simulate_calibrate(
|
|
|
1320
1359
|
record = asyncio.run(
|
|
1321
1360
|
cal_cli.read_record(sim_cli.read_sources(sources), readings)
|
|
1322
1361
|
)
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
humidity=None
|
|
1362
|
+
inputs: dict[str, Any] = {
|
|
1363
|
+
"air": None if air is None else cal_cli.read_readings(air, "air"),
|
|
1364
|
+
"ambient": ambient,
|
|
1365
|
+
"humidity": None
|
|
1328
1366
|
if humidity is None
|
|
1329
1367
|
else cal_cli.read_readings(humidity, "humidity"),
|
|
1330
|
-
params
|
|
1368
|
+
"params": None
|
|
1331
1369
|
if params is None
|
|
1332
1370
|
else sim_cli.read_document(params, "parameters"),
|
|
1333
|
-
record
|
|
1334
|
-
|
|
1371
|
+
"record": record,
|
|
1372
|
+
"tank_weeks": tank_weeks or None,
|
|
1373
|
+
}
|
|
1374
|
+
held = None
|
|
1375
|
+
if holdout_from is None:
|
|
1376
|
+
done = cal_cli.calibrate(readings, **inputs)
|
|
1377
|
+
else:
|
|
1378
|
+
held = cal_cli.holdout(
|
|
1379
|
+
readings,
|
|
1380
|
+
cal_cli.parse_instant(holdout_from, "--holdout-from"),
|
|
1381
|
+
**inputs,
|
|
1382
|
+
)
|
|
1383
|
+
done = held.done
|
|
1335
1384
|
except sim_cli.InputError as e:
|
|
1336
1385
|
raise click.UsageError(str(e)) from None
|
|
1337
|
-
cal_cli.run(done, output)
|
|
1386
|
+
cal_cli.run(done, output, held=held)
|
|
1338
1387
|
|
|
1339
1388
|
|
|
1340
1389
|
if __name__ == "__main__":
|
nwp500/cli/calibration.py
CHANGED
|
@@ -10,12 +10,14 @@ This module needs the ``sim`` extra.
|
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
import csv
|
|
13
|
+
import functools
|
|
13
14
|
import json
|
|
14
15
|
import logging
|
|
15
16
|
import math
|
|
16
17
|
import sys
|
|
17
18
|
from collections.abc import Sequence
|
|
18
|
-
from
|
|
19
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
20
|
+
from dataclasses import dataclass, replace
|
|
19
21
|
from datetime import datetime
|
|
20
22
|
from pathlib import Path
|
|
21
23
|
from typing import Any, TextIO
|
|
@@ -23,12 +25,14 @@ from typing import Any, TextIO
|
|
|
23
25
|
from rich.console import Console
|
|
24
26
|
from rich.table import Table
|
|
25
27
|
|
|
28
|
+
from nwp500 import sim, status_history
|
|
26
29
|
from nwp500 import sim_calibrate as sc
|
|
27
|
-
from nwp500 import
|
|
30
|
+
from nwp500 import sim_score as ss
|
|
31
|
+
from nwp500.sim import tank_fit as tf
|
|
28
32
|
from nwp500.sim.params import params_to_document
|
|
29
33
|
from nwp500.sim_calibrate import Calibrated, Term, report
|
|
30
34
|
|
|
31
|
-
from .simulate import InputError, read_params
|
|
35
|
+
from .simulate import InputError, local_zone, read_params
|
|
32
36
|
|
|
33
37
|
__author__ = "Emmanuel Levijarvi"
|
|
34
38
|
__copyright__ = "Emmanuel Levijarvi"
|
|
@@ -102,9 +106,12 @@ _OPTION_NOTES = {
|
|
|
102
106
|
sc.DEFAULT_BASE_NOTE: "refitting the 65 gal defaults (give --params "
|
|
103
107
|
"for another tank size)",
|
|
104
108
|
sc.NO_AIR_NOTE: "no air given (--air, --ambient or --sources): the "
|
|
105
|
-
"
|
|
109
|
+
"tank is not fitted",
|
|
106
110
|
}
|
|
107
111
|
|
|
112
|
+
#: The events each of the tank fit's workers takes at a time.
|
|
113
|
+
_CHUNK = 8
|
|
114
|
+
|
|
108
115
|
|
|
109
116
|
def calibrate(
|
|
110
117
|
readings: Sequence[status_history.TimedStatus],
|
|
@@ -114,20 +121,25 @@ def calibrate(
|
|
|
114
121
|
humidity: Sequence[Reading] | None = None,
|
|
115
122
|
params: Any = None,
|
|
116
123
|
record: Any = None,
|
|
124
|
+
tank_weeks: float | None = sc.TANK_WEEKS,
|
|
117
125
|
) -> Calibrated:
|
|
118
126
|
"""``sim_calibrate.calibrate`` with ``params`` a parameters
|
|
119
127
|
document, its errors as ``InputError`` and its notes naming the
|
|
120
|
-
command's options
|
|
128
|
+
command's options, the tank's fit spread over the machine's
|
|
129
|
+
processors. ``tank_weeks`` as for ``sim_calibrate.calibrate``."""
|
|
121
130
|
base = None if params is None else read_params(params, "--params")
|
|
122
131
|
try:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
with ProcessPoolExecutor() as pool:
|
|
133
|
+
done = sc.calibrate(
|
|
134
|
+
readings,
|
|
135
|
+
air=air,
|
|
136
|
+
ambient=ambient,
|
|
137
|
+
humidity=humidity,
|
|
138
|
+
params=base,
|
|
139
|
+
record=record,
|
|
140
|
+
map_=functools.partial(pool.map, chunksize=_CHUNK),
|
|
141
|
+
tank_weeks=tank_weeks,
|
|
142
|
+
)
|
|
131
143
|
except sc.CalibrationInputError as e:
|
|
132
144
|
raise InputError(str(e)) from None
|
|
133
145
|
return replace(
|
|
@@ -135,6 +147,117 @@ def calibrate(
|
|
|
135
147
|
)
|
|
136
148
|
|
|
137
149
|
|
|
150
|
+
def parse_instant(text: str, what: str) -> datetime:
|
|
151
|
+
"""An ISO 8601 date or instant; a date, or an instant without its
|
|
152
|
+
offset, in local time."""
|
|
153
|
+
try:
|
|
154
|
+
at = datetime.fromisoformat(text.strip())
|
|
155
|
+
except ValueError:
|
|
156
|
+
raise InputError(f"{what} {text!r} is not an ISO 8601 date") from None
|
|
157
|
+
return at.astimezone() if at.tzinfo is None else at
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@dataclass(frozen=True)
|
|
161
|
+
class HeldOutScore:
|
|
162
|
+
"""A calibration on the history before ``start``, and the base's
|
|
163
|
+
and the fit's scores on the events from it (``sim_score``)."""
|
|
164
|
+
|
|
165
|
+
start: datetime
|
|
166
|
+
done: Calibrated
|
|
167
|
+
base: ss.Score
|
|
168
|
+
fitted: ss.Score
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def holdout(
|
|
172
|
+
readings: Sequence[status_history.TimedStatus],
|
|
173
|
+
start: datetime,
|
|
174
|
+
*,
|
|
175
|
+
air: Sequence[Reading] | None = None,
|
|
176
|
+
ambient: float | None = None,
|
|
177
|
+
humidity: Sequence[Reading] | None = None,
|
|
178
|
+
params: Any = None,
|
|
179
|
+
record: Any = None,
|
|
180
|
+
tank_weeks: float | None = sc.TANK_WEEKS,
|
|
181
|
+
) -> HeldOutScore:
|
|
182
|
+
"""``calibrate`` on the statuses before ``start``, the tank on its
|
|
183
|
+
last ``tank_weeks`` before it, then the base and the fit scored on
|
|
184
|
+
the events from it. Raises ``InputError`` when
|
|
185
|
+
either side of ``start`` holds no status."""
|
|
186
|
+
before = [r for r in readings if r[0] < start]
|
|
187
|
+
if not before or len(before) == len(readings):
|
|
188
|
+
raise InputError(
|
|
189
|
+
f"--holdout-from {start.isoformat()} leaves no history on one "
|
|
190
|
+
"side of it"
|
|
191
|
+
)
|
|
192
|
+
done = calibrate(
|
|
193
|
+
before,
|
|
194
|
+
air=air,
|
|
195
|
+
ambient=ambient,
|
|
196
|
+
humidity=humidity,
|
|
197
|
+
params=params,
|
|
198
|
+
record=record,
|
|
199
|
+
tank_weeks=tank_weeks,
|
|
200
|
+
)
|
|
201
|
+
base = (
|
|
202
|
+
sim.default_params()
|
|
203
|
+
if params is None
|
|
204
|
+
else read_params(params, "--params")
|
|
205
|
+
)
|
|
206
|
+
try:
|
|
207
|
+
ready = sc.prepare(
|
|
208
|
+
readings, air=air, ambient=ambient, humidity=humidity, record=record
|
|
209
|
+
)
|
|
210
|
+
except sc.CalibrationInputError as e:
|
|
211
|
+
raise InputError(str(e)) from None
|
|
212
|
+
found = ss.events(ready, since=start)
|
|
213
|
+
# Each event starts from the layers the history before it leaves,
|
|
214
|
+
# which needs the air read somewhere in it.
|
|
215
|
+
air_f = ready.frame.get("air_f")
|
|
216
|
+
replayed = (
|
|
217
|
+
tf.tank_record(ready.frame)
|
|
218
|
+
if air_f is not None and bool(air_f.notna().any())
|
|
219
|
+
else None
|
|
220
|
+
)
|
|
221
|
+
# Local days, as ``--holdout-from`` is read.
|
|
222
|
+
tz = local_zone() or "UTC"
|
|
223
|
+
return HeldOutScore(
|
|
224
|
+
start,
|
|
225
|
+
done,
|
|
226
|
+
ss.score(found, base, tz=tz, record=replayed),
|
|
227
|
+
ss.score(found, done.calibration.params, tz=tz, record=replayed),
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def print_score(held: HeldOutScore, console: Console) -> None:
|
|
232
|
+
"""The held-out score as a table on ``console``."""
|
|
233
|
+
events = held.fitted.events
|
|
234
|
+
console.print(
|
|
235
|
+
f"Held out from {held.start.isoformat()}: "
|
|
236
|
+
f"{len(events['recoveries'])} recoveries, "
|
|
237
|
+
f"{len(events['windows'])} quiet windows"
|
|
238
|
+
)
|
|
239
|
+
table = Table(title="Held-out score (lower is better, but a bias)")
|
|
240
|
+
for column in ("Metric", "Events", "Base", "Fitted", "Fitted 90 %"):
|
|
241
|
+
table.add_column(column)
|
|
242
|
+
for name, fitted in held.fitted.metrics.items():
|
|
243
|
+
base = held.base.metrics.get(name)
|
|
244
|
+
table.add_row(
|
|
245
|
+
name,
|
|
246
|
+
str(fitted.n),
|
|
247
|
+
"" if base is None else f"{base.value:.3g}",
|
|
248
|
+
f"{fitted.value:.3g}",
|
|
249
|
+
f"{fitted.lo:.3g} to {fitted.hi:.3g}",
|
|
250
|
+
)
|
|
251
|
+
console.print(table)
|
|
252
|
+
failed = sum(held.fitted.failed.values())
|
|
253
|
+
broken = sum(held.fitted.breaks.values())
|
|
254
|
+
if failed or broken:
|
|
255
|
+
console.print(
|
|
256
|
+
f"Fitted: {failed} events the simulator could not answer, "
|
|
257
|
+
f"{broken} predictions below the air"
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
138
261
|
def _kept(t: Term) -> str:
|
|
139
262
|
if t.fitted:
|
|
140
263
|
return f"fitted: {t.note}" if t.note else "fitted"
|
|
@@ -145,8 +268,7 @@ def print_report(done: Calibrated, console: Console) -> None:
|
|
|
145
268
|
"""The report as tables on ``console``."""
|
|
146
269
|
c = done.calibration
|
|
147
270
|
console.print(
|
|
148
|
-
f"{done.n_statuses} statuses;
|
|
149
|
-
f"{len(c.recoveries)} recoveries found"
|
|
271
|
+
f"{done.n_statuses} statuses; {len(c.recoveries)} recoveries found"
|
|
150
272
|
)
|
|
151
273
|
if done.n_gaps:
|
|
152
274
|
minutes = done.max_gap.total_seconds() / 60
|
|
@@ -181,12 +303,19 @@ def print_report(done: Calibrated, console: Console) -> None:
|
|
|
181
303
|
|
|
182
304
|
|
|
183
305
|
def run(
|
|
184
|
-
done: Calibrated,
|
|
306
|
+
done: Calibrated,
|
|
307
|
+
output: str | Path | None,
|
|
308
|
+
out: TextIO | None = None,
|
|
309
|
+
held: HeldOutScore | None = None,
|
|
185
310
|
) -> None:
|
|
186
|
-
"""Print the report and write the parameters
|
|
311
|
+
"""Print the report, and ``held``'s score, and write the parameters
|
|
312
|
+
to ``output``."""
|
|
187
313
|
for note in done.notes:
|
|
188
314
|
_logger.warning(note)
|
|
189
|
-
|
|
315
|
+
console = Console(file=out or sys.stdout)
|
|
316
|
+
print_report(done, console)
|
|
317
|
+
if held is not None:
|
|
318
|
+
print_score(held, console)
|
|
190
319
|
if output is not None:
|
|
191
320
|
text = json.dumps(params_to_document(done.calibration.params), indent=2)
|
|
192
321
|
Path(output).write_text(text + "\n", encoding="utf-8")
|
nwp500/cli/handlers.py
CHANGED
|
@@ -59,6 +59,9 @@ async def _wait_for_response(
|
|
|
59
59
|
subscribe_func: Callable[
|
|
60
60
|
[Device, Callable[[Any], None]], Coroutine[Any, Any, Any]
|
|
61
61
|
],
|
|
62
|
+
unsubscribe_func: Callable[
|
|
63
|
+
[Device, Callable[[Any], None]], Coroutine[Any, Any, Any]
|
|
64
|
+
],
|
|
62
65
|
device: Device,
|
|
63
66
|
action_func: Callable[[], Coroutine[Any, Any, Any]],
|
|
64
67
|
timeout: float = 10.0,
|
|
@@ -71,15 +74,40 @@ async def _wait_for_response(
|
|
|
71
74
|
if not future.done():
|
|
72
75
|
future.set_result(res)
|
|
73
76
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
try:
|
|
78
|
+
# Inside the try: a subscribe cancelled while its acknowledgement
|
|
79
|
+
# is pending has already registered the callback.
|
|
80
|
+
await subscribe_func(device, callback)
|
|
81
|
+
_logger.info(f"Requesting {action_name}...")
|
|
82
|
+
await action_func()
|
|
83
|
+
try:
|
|
84
|
+
return await asyncio.wait_for(future, timeout=timeout)
|
|
85
|
+
except TimeoutError:
|
|
86
|
+
_logger.error(f"Timed out waiting for {action_name} response.")
|
|
87
|
+
raise
|
|
88
|
+
finally:
|
|
89
|
+
await unsubscribe_quietly(unsubscribe_func, device, callback)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
async def unsubscribe_quietly(
|
|
93
|
+
unsubscribe_func: Callable[
|
|
94
|
+
[Device, Callable[[Any], None]], Coroutine[Any, Any, Any]
|
|
95
|
+
],
|
|
96
|
+
device: Device,
|
|
97
|
+
callback: Callable[[Any], None],
|
|
98
|
+
) -> None:
|
|
99
|
+
"""Remove a reply callback without replacing the caller's outcome.
|
|
77
100
|
|
|
101
|
+
The callback is removed locally before the broker is asked; a failed
|
|
102
|
+
or unacknowledged broker unsubscribe is logged, so it can neither
|
|
103
|
+
hide a reply already received nor replace the request's own error.
|
|
104
|
+
"""
|
|
78
105
|
try:
|
|
79
|
-
|
|
80
|
-
except
|
|
81
|
-
_logger.
|
|
82
|
-
|
|
106
|
+
await unsubscribe_func(device, callback)
|
|
107
|
+
except Exception as e:
|
|
108
|
+
_logger.warning(
|
|
109
|
+
"Failed to unsubscribe a reply callback: %s", type(e).__name__
|
|
110
|
+
)
|
|
83
111
|
|
|
84
112
|
|
|
85
113
|
async def _handle_command_with_status_feedback(
|
|
@@ -94,6 +122,7 @@ async def _handle_command_with_status_feedback(
|
|
|
94
122
|
try:
|
|
95
123
|
status: Any = await _wait_for_response(
|
|
96
124
|
mqtt.subscribe_device_status,
|
|
125
|
+
mqtt.unsubscribe_device_status,
|
|
97
126
|
device,
|
|
98
127
|
action_func,
|
|
99
128
|
action_name=action_name,
|
|
@@ -124,6 +153,7 @@ async def get_controller_serial_number(
|
|
|
124
153
|
try:
|
|
125
154
|
feature: Any = await _wait_for_response(
|
|
126
155
|
mqtt.subscribe_device_feature,
|
|
156
|
+
mqtt.unsubscribe_device_feature,
|
|
127
157
|
device,
|
|
128
158
|
lambda: mqtt.request_device_info(device),
|
|
129
159
|
timeout=timeout,
|
|
@@ -155,6 +185,9 @@ async def _handle_info_request(
|
|
|
155
185
|
subscribe_method: Callable[
|
|
156
186
|
[Device, Callable[[Any], None]], Coroutine[Any, Any, Any]
|
|
157
187
|
],
|
|
188
|
+
unsubscribe_method: Callable[
|
|
189
|
+
[Device, Callable[[Any], None]], Coroutine[Any, Any, Any]
|
|
190
|
+
],
|
|
158
191
|
request_method: Callable[[Device], Coroutine[Any, Any, Any]],
|
|
159
192
|
data_key: str,
|
|
160
193
|
action_name: str,
|
|
@@ -166,6 +199,7 @@ async def _handle_info_request(
|
|
|
166
199
|
if not raw:
|
|
167
200
|
res = await _wait_for_response(
|
|
168
201
|
subscribe_method,
|
|
202
|
+
unsubscribe_method,
|
|
169
203
|
device,
|
|
170
204
|
lambda: request_method(device),
|
|
171
205
|
action_name=action_name,
|
|
@@ -199,6 +233,7 @@ async def handle_status_request(
|
|
|
199
233
|
mqtt,
|
|
200
234
|
device,
|
|
201
235
|
mqtt.subscribe_device_status,
|
|
236
|
+
mqtt.unsubscribe_device_status,
|
|
202
237
|
mqtt.request_device_status,
|
|
203
238
|
"status",
|
|
204
239
|
"device status",
|
|
@@ -215,6 +250,7 @@ async def handle_device_info_request(
|
|
|
215
250
|
mqtt,
|
|
216
251
|
device,
|
|
217
252
|
mqtt.subscribe_device_feature,
|
|
253
|
+
mqtt.unsubscribe_device_feature,
|
|
218
254
|
mqtt.request_device_info,
|
|
219
255
|
"feature",
|
|
220
256
|
"device information",
|
|
@@ -888,6 +924,7 @@ async def handle_get_energy_request(
|
|
|
888
924
|
try:
|
|
889
925
|
res: Any = await _wait_for_response(
|
|
890
926
|
mqtt.subscribe_energy_usage,
|
|
927
|
+
mqtt.unsubscribe_energy_usage,
|
|
891
928
|
device,
|
|
892
929
|
lambda: mqtt.request_energy_usage(device, year, months),
|
|
893
930
|
action_name="energy usage",
|
|
@@ -913,6 +950,7 @@ async def handle_get_energy_monthly_request(
|
|
|
913
950
|
try:
|
|
914
951
|
res: Any = await _wait_for_response(
|
|
915
952
|
mqtt.subscribe_energy_usage_monthly,
|
|
953
|
+
mqtt.unsubscribe_energy_usage_monthly,
|
|
916
954
|
device,
|
|
917
955
|
lambda: mqtt.request_energy_usage_monthly(device, years),
|
|
918
956
|
action_name="monthly energy usage",
|
|
@@ -937,6 +975,7 @@ async def handle_diagnostics_request(
|
|
|
937
975
|
try:
|
|
938
976
|
diagnostics: Any = await _wait_for_response(
|
|
939
977
|
mqtt.subscribe_diagnostics,
|
|
978
|
+
mqtt.unsubscribe_diagnostics,
|
|
940
979
|
device,
|
|
941
980
|
lambda: mqtt.request_diagnostics(device),
|
|
942
981
|
action_name="diagnostics",
|
|
@@ -962,6 +1001,7 @@ async def handle_firmware_download_info_request(
|
|
|
962
1001
|
try:
|
|
963
1002
|
info: Any = await _wait_for_response(
|
|
964
1003
|
mqtt.subscribe_firmware_download_info,
|
|
1004
|
+
mqtt.unsubscribe_firmware_download_info,
|
|
965
1005
|
device,
|
|
966
1006
|
lambda: mqtt.request_firmware_download_info(device),
|
|
967
1007
|
action_name="firmware download info",
|
|
@@ -986,6 +1026,7 @@ async def handle_get_recirculation_schedule_request(
|
|
|
986
1026
|
try:
|
|
987
1027
|
schedule: Any = await _wait_for_response(
|
|
988
1028
|
mqtt.subscribe_recirculation_schedule_response,
|
|
1029
|
+
mqtt.unsubscribe_recirculation_schedule_response,
|
|
989
1030
|
device,
|
|
990
1031
|
lambda: mqtt.request_recirculation_schedule(device),
|
|
991
1032
|
action_name="recirculation schedule",
|