Skip to content

Refuse orders an endpoint cannot sort by - #1567

Merged
frenck merged 1 commit into
mainfrom
frenck/validate-order
Oct 1, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/validate-order

Conversation

@frenck

@frenck frenck commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Breaking change

Passing an order that an endpoint cannot sort by now raises a ValueError before any request is sent:

  • countries(), languages() and tags() accept only Order.NAME and Order.STATION_COUNT. Anything else used to fail anyway, after five retries.
  • stations() and search() refuse Order.CODE and Order.STATION_COUNT. Those used to return a list that was not sorted at all, so code that passed them got a wrong result without noticing.

To adapt, use one of the orders the endpoint supports; the error message lists them. Home Assistant only uses supported combinations.

Proposed change

Found in a review of the library, and checked against the live API for every Order value on every endpoint. All list and search methods forwarded any Order unchanged, but the endpoints do not all support all of them. The API documentation agrees:

Endpoint Supported orders Anything else
countrycodes, languages, tags name, stationcount HTTP 500
stations, stations/search all except code and stationcount HTTP 200, silently unsorted

Since #1554 a 5xx is retried, so languages(order=Order.VOTES) took around 15 seconds of retries before raising a connection error that hid the real problem.

The supported orders now live in const.py (LIST_ORDERS, STATION_ORDERS), and every method checks its order before sending a request:

ValueError: Cannot order by Order.VOTES here, use one of: Order.NAME, Order.STATION_COUNT

The tests run every Order value against every endpoint. They spell out what the API accepts instead of importing the library's own constants, so a mistake in those constants fails the tests.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: fixes # None
  • This PR is related to: None
  • Link to a separate documentation pull request: None

Checklist

  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and poetry run pytest passes locally. A pull request cannot be merged unless CI is green.
  • poetry run prek run --all-files passes (lint, format, and type checks).
  • No commented-out or dead code is left in the pull request.

@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Oct 1, 2026
@frenck
frenck force-pushed the frenck/validate-order branch from c69ea3b to 8dad861 Compare October 1, 2026 11:22
The countries, languages and tags lists can only be sorted by name or by
station count; the API answers any other order with a server error, which
the client then retried five times. Station lists silently ignore an
order by code or station count, returning an unsorted list.

Raise a ValueError before sending the request instead.
@frenck
frenck force-pushed the frenck/validate-order branch from 8dad861 to e7cebb2 Compare October 1, 2026 11:26
@frenck
frenck merged commit dc4d23b into main Oct 1, 2026
30 checks passed
@frenck
frenck deleted the frenck/validate-order branch October 1, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant