Skip to content

Move reservation CRUD helpers into core library#78

Merged
eman merged 8 commits intomainfrom
reservations
Feb 17, 2026
Merged

Move reservation CRUD helpers into core library#78
eman merged 8 commits intomainfrom
reservations

Conversation

@eman
Copy link
Owner

@eman eman commented Feb 17, 2026

Summary

Extracts the single-entry reservation CRUD helpers from the CLI into a new public module src/nwp500/reservations.py, making them available to all library users.

Changes

New: src/nwp500/reservations.py

Four public async functions:

  • fetch_reservations(mqtt, device) — fetch the full schedule; returns None on timeout
  • add_reservation(mqtt, device, *, enabled, days, hour, minute, mode, temperature) — append a new entry
  • delete_reservation(mqtt, device, index) — remove an entry by 1-based index
  • update_reservation(mqtt, device, index, *, ...) — partial in-place update (only supplied fields change)

Functions raise ValueError for out-of-range arguments and TimeoutError (mutating helpers) when the device doesn't respond. fetch_reservations returns None on timeout.

Additional improvements over the original CLI logic:

  • Uses specific MQTT response topic (cmd/{type}/{client_id}/res/rsv/rd) instead of a wildcard pattern
  • Unsubscribes in a finally block to prevent subscription leaks
  • Restores the previous unit system after parsing inside the MQTT callback
  • Range-validates hour/minute/mode in both add_reservation and update_reservation

src/nwp500/__init__.py

All four functions exported and listed in __all__.

src/nwp500/cli/handlers.py

The three handler functions now delegate to the library. All inline logic, the private _fetch_reservations wrapper, and the _RAW_RESERVATION_FIELDS constant have been removed.

docs/guides/scheduling.rst

Replaced the "Library Helpers (Needed)" stub with a real documented section showing all four functions with usage examples.

tests/test_reservations.py (new)

19 tests covering:

  • fetch_reservations: success, timeout, wrong-topic filtering
  • add_reservation: success, invalid hour/minute/mode, timeout
  • delete_reservation: success, disables-when-empty, invalid index, timeout
  • update_reservation: temperature change, field preservation, invalid index/hour/minute/mode, timeout

eman and others added 4 commits February 17, 2026 13:41
Add comprehensive documentation covering:
- Low-level MQTT client methods (update_reservations, request_reservations)
- High-level CLI helpers (reservations add/update/delete/get)
- Read-modify-write pattern implementation
- Why CLI helpers are recommended for typical use cases
- Example code for both SDK and CLI approaches

This clarifies that the CLI provides the primary public interface
for managing reservations programmatically.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Show three levels of abstraction:
1. Low-level update_reservations() method (exists)
2. CLI helpers (exist)
3. Library helpers needed - add_reservation(), update_reservation(),
   delete_reservation(), get_reservations()

Remove prescriptive language about what should be primary interface.
Extract add_reservation(), delete_reservation(), update_reservation(),
and fetch_reservations() from the CLI into a new public module
(src/nwp500/reservations.py).

- New module raises ValueError/TimeoutError instead of logging errors,
  which is appropriate for a library API
- All four functions are exported from nwp500.__init__ and listed in __all__
- CLI handlers now delegate to the library functions; all inline logic removed
- docs/guides/scheduling.rst: replace the 'Library Helpers (Needed)' stub
  with a real documented section showing all four functions with examples
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extracts reservation schedule CRUD logic from the CLI into a reusable, public library module so non-CLI consumers can fetch and mutate device reservations via MQTT.

Changes:

  • Added src/nwp500/reservations.py with async helpers for fetch/add/delete/update using a read-modify-write pattern.
  • Updated CLI reservation handlers to delegate to the new library helpers.
  • Exported the helpers from nwp500.__init__ and expanded scheduling documentation with usage examples.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

File Description
src/nwp500/reservations.py New public async API for reservation schedule CRUD over MQTT.
src/nwp500/cli/handlers.py Replaces inline reservation logic with calls into the new library module.
src/nwp500/init.py Re-exports the reservation helpers as part of the public package surface.
docs/guides/scheduling.rst Documents low-level and new high-level reservation helper usage.

eman added 3 commits February 17, 2026 14:49
- fetch_reservations: use specific response topic instead of wildcard,
  unsubscribe in finally block, restore previous unit system after parsing
- fetch_reservations: remove error logging on timeout (returns None silently)
- update_reservation: add range validation for hour/minute/mode when provided
- docs: remove non-existent path= kwarg from subscribe_device_feature call
- docs: clarify that fetch_reservations returns None on timeout while
  mutating helpers raise TimeoutError
- tests: add test_reservations.py covering fetch success/timeout,
  add/delete/update success paths, index and range validation,
  and timeout propagation for all three mutating helpers
@eman eman merged commit e1f1bfe into main Feb 17, 2026
11 checks passed
@eman eman deleted the reservations branch February 17, 2026 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants