Skip to content

Add entity-change push subscriptions to mobile_app - #174943

Open
hariharanjagan wants to merge 31 commits into
home-assistant:devfrom
hariharanjagan:feature/Widget-Push-Notification
Open

Add entity-change push subscriptions to mobile_app#174943
hariharanjagan wants to merge 31 commits into
home-assistant:devfrom
hariharanjagan:feature/Widget-Push-Notification

Conversation

@hariharanjagan

@hariharanjagan hariharanjagan commented Jun 27, 2026

Copy link
Copy Markdown

Breaking change

Proposed change

Adds an entity-driven push subscription mechanism to the mobile_app integration. A companion app registers a push token together with a set of entity_ids it wants to observe, the integration then tracks state changes on those entities and POSTs a minimal payload to the app's registered push URL whenever one of them changes.

Two webhook commands are exposed on the existing mobile_app webhook: one to register a subscription subscription_id, push_token, entity_ids, optional target and one to remove it.

Registration is idempotent on subscription_id, so re-registering with a rotated token or a changed entity set overwrites the previous state in place rather than creating a duplicate.

Bounds:

entity_ids is validated and capped at 1–50 per subscription duplicate entity_ids are dropped).
Subscriptions are capped at 50 per device, evicting the oldest first. Together these bound how many state listeners a single device can arm. State-change delivery is debounced per subscription (5s trailing edge), so a burst of changes collapses into a single push.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Related Pull Requests

iOS - home-assistant/iOS#4939
FCM - home-assistant/mobile-apps-fcm-push#337

@hariharanjagan
hariharanjagan requested a review from a team as a code owner June 27, 2026 02:38
Copilot AI review requested due to automatic review settings June 27, 2026 02:38

@home-assistant home-assistant Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @hariharanjagan

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant

Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant

Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (mobile_app) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mobile_app can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign mobile_app Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a platform-agnostic push-subscription mechanism to the mobile_app integration. A companion app registers a push token plus a set of entity_ids via two new webhook commands; Core then tracks those entities' state changes and sends a debounced, best-effort silent push to the app's existing push URL. The first consumer is iOS WidgetKit timeline refresh, but the contract is deliberately generic. The implementation closely mirrors the existing live_activity subpackage for persistence/lifecycle, bumping storage minor version 2 → 3 with a seeding migration.

Changes:

  • New push_subscription/ subpackage: webhook handlers (register/remove), storage + per-subscription trailing-edge debounce + listener teardown/restore, and the outgoing silent-push sender.
  • New constants and storage version bump, plus persistence of the subscriptions mapping in savable_state().
  • Lifecycle wiring in __init__.py: seed hass.data keys, restore on entry setup, tear down on unload, drop on removal, and a minor-3 migration step.

Reviewed changes

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

Show a summary per file
File Description
push_subscription/webhook.py Adds register_push_subscription / remove_push_subscription webhook commands with schema validation.
push_subscription/store.py Stores subscriptions, arms/cancels state-change listeners, and implements per-subscription debounce.
push_subscription/notify.py Builds and posts the minimal silent push payload to the device's push URL (fire-and-forget).
push_subscription/__init__.py Package init; imports webhook to register commands and re-exports lifecycle helpers.
helpers.py Persists the subscriptions mapping in savable_state().
const.py New payload/data keys, debounce window constant, and STORAGE_VERSION_MINOR 2 → 3.
__init__.py Seeds new hass.data keys, restores/tears down/removes subscriptions across entry lifecycle, and adds the minor-3 migration.

Comment thread homeassistant/components/mobile_app/push_subscription/notify.py Outdated
Comment thread homeassistant/components/mobile_app/push_subscription/webhook.py
Comment thread homeassistant/components/mobile_app/push_subscription/notify.py Outdated

@home-assistant home-assistant Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @hariharanjagan

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Comment thread homeassistant/components/mobile_app/push_subscription/notify.py Outdated
Comment thread homeassistant/components/mobile_app/__init__.py
@bgoncal

bgoncal commented Jun 27, 2026

Copy link
Copy Markdown
Member

I haven't looked into the PR, but reading the description I was thinking, wouldn’t it make more sense for 1 entity to have N push tokens instead?
When this entity changes, it notifies all clients using each token linked to that entity.

Using the iOS companion app as an example, this would allow to update a widget and a control Center control at the same time in case both use the same entity

Copilot AI review requested due to automatic review settings July 2, 2026 23:58
@hariharanjagan
hariharanjagan marked this pull request as ready for review July 7, 2026 12:09
@home-assistant
home-assistant Bot requested a review from bgoncal July 7, 2026 12:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 7, 2026 12:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread homeassistant/components/mobile_app/push_subscription/webhook.py Outdated
Copilot AI review requested due to automatic review settings July 7, 2026 12:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines +26 to +28
from homeassistant.components.mobile_app.push_subscription.notify import (
_send_subscription_push,
)
Extract the inline 50 in the register schema into PUSH_SUBSCRIPTION_MAX_ENTITY_IDS
so it sits with the other two subscription limits and is self-documenting.
Copilot AI review requested due to automatic review settings July 7, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines +123 to +124
if not hass.is_running:
return
Copilot AI review requested due to automatic review settings July 8, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines +43 to +54
# Patch target for the inner coroutine that performs the HTTP POST, letting the
# debounce/scheduling logic run under test while the network call is stubbed.
SEND_PUSH = (
"homeassistant.components.mobile_app.push_subscription"
".notify._send_subscription_push"
)
# async_get_clientsession as looked up inside notify.py - patched in the two
# direct _send_subscription_push tests so the POST never touches the network.
GET_SESSION = (
"homeassistant.components.mobile_app.push_subscription"
".notify.async_get_clientsession"
)
Copilot AI review requested due to automatic review settings July 9, 2026 17:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines +39 to +44
vol.Required(PUSH_SUBSCRIPTION_ENTITY_IDS): vol.All(
cv.ensure_list,
[cv.entity_id],
vol.Length(min=1, max=PUSH_SUBSCRIPTION_MAX_ENTITY_IDS),
_unique_entity_ids,
),
Comment on lines +3 to +5
A push subscription maps a push token to a set of entity_ids. The integration
owns the mapping and the state tracking; it has no knowledge of what the app
does with the resulting push.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One of my initial comments was to have 1 entity_id link to N push tokens, so if that entity ID updates we can update multiple parts of the app at once, example: Widgets, Watch Complications, Control Center Controls


# Trailing-edge debounce window: a burst of state changes within this many
# seconds collapses to a single push.
PUSH_SUBSCRIPTION_DEBOUNCE_SECONDS = 5.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should debounce be defined by the client? Let's say that one client can handle more frequent updates than other client, shouldn't them be able to control that?

Copilot AI review requested due to automatic review settings July 31, 2026 10:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

homeassistant/components/mobile_app/push_subscription/store.py:115

  • Refresh subscription trackers whenever update_registration changes app_data. A subscription stored while push_url is absent remains listenerless after that webhook later adds the URL, while removing the URL leaves its existing listener active because the update handler only updates the entry and reloads notify; rerun tracker setup/teardown after the registration update.
    # Only arm a listener for registrations that can send a cloud push; others
    # would schedule a debounce timer on every state change that never sends.
    entry = hass.data[DOMAIN][DATA_CONFIG_ENTRIES].get(webhook_id)
    if entry is None or ATTR_PUSH_URL not in entry.data.get(ATTR_APP_DATA, {}):
        return

Comment on lines +123 to +125
if not hass.is_running:
return
_async_schedule_push(hass, webhook_id, sub_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants