-
Notifications
You must be signed in to change notification settings - Fork 0
Add confirmed reservation/TOU write + canonical schedule comparison #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c47d412
Add confirmed reservation/TOU write helpers and canonical schedule co…
eman 7c13dca
Match confirmed writes against sent content, not first response
eman b2cd0e4
Redact MAC address in confirmed-write unsubscribe failure logs
eman 6a727f5
Fix pyright type errors in confirmed-write helpers (CI failure)
eman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """ | ||
| TOU (Time-of-Use) schedule management helpers. | ||
|
|
||
| Companion to :mod:`nwp500.reservations`: the device protocol requires | ||
| sending the full TOU period list for every change, and confirming that a | ||
| write landed requires waiting for the device's ``tou/rd`` echo rather than | ||
| just the MQTT publish packet id. | ||
|
|
||
| All functions are ``async`` and require a connected :class:`NavienMqttClient`. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from .converters import device_bool_from_python | ||
| from .models import TOUPeriod, TOUReservationSchedule | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .models import Device | ||
| from .mqtt import NavienMqttClient | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def configure_tou_schedule_confirmed( | ||
| mqtt: NavienMqttClient, | ||
| device: Device, | ||
| controller_serial_number: str, | ||
| periods: Sequence[dict[str, Any]], | ||
| *, | ||
| enabled: bool = True, | ||
| timeout: float = 10.0, | ||
| ) -> TOUReservationSchedule | None: | ||
| """Write the TOU schedule and confirm the device applied it. | ||
|
|
||
| Sends ``configure_tou_schedule`` and waits for the device's ``tou/rd`` | ||
| echo, returning the parsed :class:`TOUReservationSchedule` the device | ||
| now holds. Compare it against the desired program with | ||
| :meth:`TOUReservationSchedule.canonical`, e.g.:: | ||
|
|
||
| confirmed = await configure_tou_schedule_confirmed( | ||
| mqtt, device, serial, periods | ||
| ) | ||
| assert confirmed is not None | ||
| assert confirmed.canonical() == desired_schedule.canonical() | ||
|
|
||
| Args: | ||
| mqtt: Connected MQTT client. | ||
| device: Target device. | ||
| controller_serial_number: Controller serial number. | ||
| periods: List of raw TOU period dicts to write. | ||
| enabled: Whether TOU is enabled (default: True). | ||
| timeout: Seconds to wait for the confirming response. | ||
|
|
||
| Returns: | ||
| The :class:`TOUReservationSchedule` the device echoed back after | ||
| the write, or ``None`` if no matching response arrived within | ||
| ``timeout``. | ||
|
|
||
| Note: | ||
| The device protocol has no request/response correlation id on | ||
| ``tou/rd``, so a response is only accepted once its | ||
| :meth:`~nwp500.models.TOUReservationSchedule.canonical` form | ||
| matches what was just written. This avoids resolving on a | ||
| stale/unrelated ``tou/rd`` message (e.g. from a concurrent read or | ||
| a previous configure) that happens to arrive in the same window. | ||
| """ | ||
| expected = TOUReservationSchedule( | ||
| reservationUse=device_bool_from_python(enabled), | ||
| reservation=[TOUPeriod(**period) for period in periods], | ||
| ).canonical() | ||
|
|
||
| future: asyncio.Future[TOUReservationSchedule] = ( | ||
| asyncio.get_running_loop().create_future() | ||
| ) | ||
|
|
||
| def on_schedule(schedule: TOUReservationSchedule) -> None: | ||
| if not future.done() and schedule.canonical() == expected: | ||
| future.set_result(schedule) | ||
|
|
||
| await mqtt.subscribe_tou_response(device, on_schedule) | ||
| try: | ||
| await mqtt.configure_tou_schedule( | ||
| device, controller_serial_number, periods, enabled=enabled | ||
| ) | ||
| try: | ||
| return await asyncio.wait_for(future, timeout=timeout) | ||
| except TimeoutError: | ||
| return None | ||
| finally: | ||
| try: | ||
| await mqtt.unsubscribe_tou_response(device, on_schedule) | ||
| except Exception: | ||
| from .mqtt.utils import redact_mac | ||
|
|
||
| _logger.warning( | ||
| "Failed to unsubscribe TOU response handler for device %s", | ||
| redact_mac(device.device_info.mac_address), | ||
|
eman marked this conversation as resolved.
Dismissed
|
||
| exc_info=True, | ||
| ) | ||
|
|
||
|
|
||
| __all__ = ["configure_tou_schedule_confirmed"] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.