Skip to content

feat: add holiday mode services and sensors - #222

Draft
nsleigh wants to merge 4 commits into
Pyhass:masterfrom
nsleigh:feature/holiday-mode
Draft

nsleigh wants to merge 4 commits into
Pyhass:masterfrom
nsleigh:feature/holiday-mode

Conversation

@nsleigh

@nsleigh nsleigh commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What does this change?

Adds Hive's whole-home holiday mode to the integration, using the hub holiday API from Pyhass/Pyhive#149 (backported to 1.0.x in Pyhass/Pyhive#194).

Services (in services.py, registered once in async_setup alongside core's boost services)

  • hive.set_holiday_mode: start, end and temperature (7–35 °C). Start and end are truncated to the minute, as in the Hive app. Times without an offset use Home Assistant's time zone. An end before (or in the same minute as) the start, or in the past, is rejected with ServiceValidationError.
  • hive.cancel_holiday_mode
  • Both take an optional config_entry_id, needed only when more than one Hive account is set up.

A rejected request or an API error is raised as HomeAssistantError with a translated message. HiveReauthRequired starts a reauth flow.

Sensors (on the hub device, named through translation_key)

  • Holiday mode: off / scheduled / active, with start, end and temperature attributes
  • Holiday date start and Holiday date end: timestamps

The shared state lives in holiday.py:

  • The three sensors share one cached fetch and poll every 30 minutes. They refresh on their own dispatcher signal, not the one every Hive entity listens on.
  • A service call drops the cache and refreshes straight away, then a few more times over five minutes, because Hive's backend can take a while to change state.
  • Whenever a fetch shows a holiday scheduled or active, a refresh is set for its start and end, with the same catch-ups once each has passed. This works however the holiday was set (these services or the Hive app) and survives restarts.
  • Each call replaces the previous timers, and unloading the entry cancels them.
  • If Hive omits start, end or temperature from a response, the last known values are kept.

__init__.py is unchanged from master.

Library pin: manifest.json pins pyhive-integration==1.0.11 as a placeholder for the release that includes the backport. I'll update it to whatever version that ends up being. Until that's on PyPI, installing the requirements (and so CI) will fail, so this stays a draft. The current-temperature attributes are back on getTargetTemperature, which 1.0.x has.

I've been running this on my own system, with the 1.0.x backport bundled. I tested set, cancel, the date checks, and a holiday ending on its own after an HA restart. The sensors followed the Hive app each time.

Related issues

Relates to #73 (closed as not planned).

Checklist

  • Tests added or updated. tests/test_holiday_mode.py covers:

    • sensor states, partial responses and unavailability
    • both services and their error handling (including ClientError/TimeoutError and reauth)
    • temperature and date validation, and naive local times
    • two config entries
    • the stale-cache case
    • catch-up timer replacement, and refreshes at the start and end

    conftest.py gives the mocked Hive a hub and a holiday response.

  • script/test: 124 passed on pytest-homeassistant 2026.11.2 with the 1.0.x backport installed.

  • Lint passes (ruff check and format, gen_translations.py --check).

  • version not bumped.

🤖 Generated with Claude Code

Rebased onto master (2026.11.1) as a single commit.

- hive.set_holiday_mode (start, end, temperature 7-35) and
  hive.cancel_holiday_mode services, with catch-up refreshes over the
  following five minutes and cleanup on unload.
- Hub-level Holiday Mode (off/scheduled/active), Holiday Date Start and
  Holiday Date End sensors sharing one cached fetch, polled every 30 min;
  a failed fetch leaves them unavailable instead of dropping them.
- Requires pyhive-integration 2.0.0b5 (holiday mode API, Pyhive#149, plus
  the SSO quote fix, Pyhive#169). 2.0.0b5 has no getTargetTemperature alias,
  so the current-temperature attributes call get_target_temperature.
- strings.json, services.yaml and icons.json updated; en.json regenerated.
- tests/test_holiday_mode.py; existing sensor tests account for the
  holiday sensors. 106 passed on pytest-homeassistant 2026.11.2 with
  pyhive-integration 2.0.0b5; script/lint --check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nsleigh
nsleigh force-pushed the feature/holiday-mode branch from f884845 to 0b2e622 Compare November 22, 2026 07:13
@nsleigh

nsleigh commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 2026.11.1 and bumped the pin to pyhive-integration==2.0.0b5, which is the first beta with both the holiday API (Pyhass/Pyhive#149) and the SSO quote fix (Pyhass/Pyhive#169). Tests and lint pass.

This is still a draft only because master is on 1.0.10. Is there a rough plan for a stable 2.0? If it's some way off, I'm happy to backport #149 to 1.0.x (like #170 did for the SSO fix) so this can pin a stable release instead. Whichever suits you better.

@KJonline

KJonline commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

@nsleigh there is still more fixes that need to go into the Beta. I would back port it. Then I will pick it up from master and merge it back into dev where the 2.0 changes are.

@KJonline

Copy link
Copy Markdown
Contributor

Specifically on this PR please could we move any service code into the services file now either keep the import from core and add your changes underneath them or copy the content of services.py from core and add your changes to it that way. if the import can be kept and your changes are added below it then that would be the best way. That way we don't add too much extra code to init.py which is mainly just start up code

@KJonline KJonline added the claude-review Run the Claude Code review on a fork PR label Nov 22, 2026
@KJonline

Copy link
Copy Markdown
Contributor

@claude Review this PR

@claude

claude Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Claude finished @KJonline's task in 26s —— View job


Review of PR #222: holiday mode services and sensors

I read the diff against origin/master. I did not run the tests or lint. The design is sound overall: the shared fetch cache, keeping the last known values on partial responses, and going unavailable instead of failing entity setup are all good. There is one structural change to make, which KJonline already asked for, and a few smaller points.

Needs changing

  1. Move the service code into services.py.

    • custom_components/hive/services.py currently contains only from homeassistant.components.hive.services import *. That is the pattern KJonline asked for: keep the core import and add the holiday code below it.
    • The schema, both handlers and the catch-up scheduling add about 100 lines to __init__.py, which is meant to be start-up code.
    • __init__.py already calls async_setup_services, which comes from core. Core's version registers the boost services, so register the holiday services alongside them rather than in async_setup_entry.
  2. Service registration and removal are per-entry.

    • The services are registered in async_setup_entry and removed in an async_on_unload callback. Unloading one config entry removes the services for every entry. A second entry would also re-register them, and the closures would point at whichever entry registered last.
    • Registering once in async_setup (what async_setup_services is for) fixes both. The handlers would then pick the entry from the call, or from hass.config_entries.async_loaded_entries(DOMAIN). If you do keep removal, only do it when the last entry unloads.
    • Hassfest and the Home Assistant quality scale also want service actions registered in async_setup.
  3. No check that end is after start.

    • SET_HOLIDAY_MODE_SCHEMA only validates the temperature range. An end before start, or in the past, goes straight to Hive.
    • Raise ServiceValidationError after truncating to the minute. It is also worth confirming what the library does with naive datetimes. cv.datetime can return a naive value, and the handler uses it with dt_util.as_utc, which assumes local time.

Smaller points

  • Timers pile up. entry.async_on_unload(async_track_point_in_utc_time(...)) registers a new unload callback on every service call, and fired timers are never released. Over a long uptime the entry's callback list grows (4 to 5 per call). Keep a single set of handles on the entry, cancel the old ones before scheduling new ones, and have the unload callback cancel whatever is left.
  • The 5-second cache can serve stale data after a service call. _HiveHolidayModeData returns the cached result if the last fetch was under 5 seconds ago. A service call shortly after a poll dispatches a refresh that gets the pre-change data. The catch-up refreshes at 30s and later recover, but the state is wrong for that time. Invalidating the cache in the service handlers, for example data.invalidate() before the dispatch, would be more reliable. The data object currently lives only inside sensor.py, so it would need to be reachable from services.py, for example on runtime_data.
  • async_dispatcher_send(hass, DOMAIN) is the same signal the other Hive entities listen on. Every holiday service call refreshes all Hive entities, not only these three. This is probably fine, but a dedicated signal would be cheaper and clearer.
  • Hardcoded names and icons.
    • _attr_name and _attr_icon are set directly on the sensors. Elsewhere in the integration, strings.json and icons.json hold these through translation_key.
    • The off/scheduled/active states have no translations either.
    • This works as it is, but it is inconsistent with the rest of the integration and would block localisation.
  • except (TimeoutError, ClientError, HiveApiError) in async_update. The service handlers catch HTTPException (from aiohttp.web_exceptions, which is a server-side type), HiveApiError and HiveReauthRequired. For a client call, aiohttp.ClientError and TimeoutError are the likely failures, so the handlers may let those escape as a raw error instead of HomeAssistantError. Check what set_holiday_mode can actually raise in 2.0.0b5 and align the two catch lists. A HiveReauthRequired in the sensor path is also not handled; it could start a reauth flow rather than only logging a warning.
  • _update_from_result raises NotImplementedError. Use abc.abstractmethod, or type the base as Generic/with a protocol. It is minor.
  • Dependency on a beta. manifest.json pins pyhive-integration==2.0.0b5. Per CLAUDE.md, check_release.py will refuse a stable component release with this pin, so this can only ship in a beta until the 1.0.x backport lands. The backport route KJonline suggested keeps this on a stable pin; after that, change the pin to the backported release.
  • The getTargetTemperature to get_target_temperature rename is in sensor.py. It is needed for 2.0.0b5 but would break against 1.0.x. If you backport [FEATURE] access shared home device details #149 to 1.0.x, check that the snake_case name exists there too, or this change will break the stable pin.
  • Tests. The new tests/test_holiday_mode.py covers states, partial data, errors and unload cleanup. After the move to services.py, add cases for two config entries (unloading one must not remove the services) and for end before start.

Summary

Functionally this looks good. Moving the services into services.py and registering them once in async_setup is the main change. It also resolves the multi-entry problem. The other items are improvements that could be handled in this PR or in follow-ups.

@KJonline KJonline removed the claude-review Run the Claude Code review on a fork PR label Nov 22, 2026
@nsleigh

nsleigh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@KJonline thank you. I will work on both changes.

nsleigh and others added 3 commits November 23, 2026 02:09
Addresses review on Pyhass#222:
- Holiday services now live in services.py, below core's import, and are
  registered once from async_setup alongside core's boost services instead
  of per entry. Unloading one entry no longer removes them; with several
  Hive accounts the optional config_entry_id picks one.
- set_holiday_mode rejects an end before (or in the same minute as) the
  start, or in the past. Naive times use Home Assistant's time zone.
- Shared holiday state moves to holiday.py: the fetch cache is dropped after
  a service call, the catch-up timers replace the previous call's instead of
  piling up, and the sensors refresh on their own signal rather than every
  Hive entity's.
- Errors: catch ClientError/TimeoutError as well; HiveReauthRequired starts
  a reauth flow from both the services and the sensors. Messages use
  translation keys.
- Sensor names, icons and states move to strings.json/icons.json via
  translation_key; _update_from_result is an abstractmethod.
- Pin pyhive-integration==1.0.11 (holiday mode backport, Pyhass/Pyhive
  1.0.x) and go back to getTargetTemperature, which 1.0.x provides.
- Tests for two entries, the date checks, naive times, reauth, the cache
  and timer replacement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The sensors only refreshed around a service call, so a holiday ending (or
starting after a restart, or set from the Hive app) showed the old state
until the 30-minute poll. Each fetch that finds holiday mode scheduled or
active now sets a refresh for its start and end; once either has passed,
the catch-up refreshes run, because Hive can take a while to flip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nsleigh

nsleigh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. Both done:

  • Backport: Backport holiday mode (#149) to 1.0.x Pyhive#194 backports [FEATURE] access shared home device details #149 to master, with the same method names as dev. manifest.json here pins pyhive-integration==1.0.11 as a placeholder. I'll change it to the real version once it's released, so I'm leaving this as a draft until then.
  • services.py: the holiday services now sit below the core import in services.py and are registered once from async_setup, alongside core's boost services. __init__.py is back to exactly what's on master.

I also worked through the review points:

  • Unloading one entry no longer removes the services. There's an optional config_entry_id for more than one account.
  • end must be after start and in the future.
  • Naive times use HA's time zone.
  • Catch-up timers replace each other instead of piling up.
  • A service call drops the cache.
  • The sensors use their own signal.
  • ClientError/TimeoutError are caught, and HiveReauthRequired starts reauth.
  • Names, icons, states and messages use translation keys.

On the error types: HTTPException stays in the list, because pyhive 1.0.x raises aiohttp.web_exceptions.HTTPError itself when a request fails.

While testing I found the sensors only refreshed around a service call. A holiday ending on its own, or one set in the Hive app, showed the old state until the 30-minute poll. They now refresh at the holiday's start and end too, and that survives restarts.

I've tested all of this live on my own Hive with the backport bundled in. 124 tests pass and lint is clean.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
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