Skip to content

Remove unnecessary startup deferral in sensor async_added_to_hass#17

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-sensor-update-deferral
Draft

Remove unnecessary startup deferral in sensor async_added_to_hass#17
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-sensor-update-deferral

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

The initial entity update in async_added_to_hass was deferred until EVENT_HOMEASSISTANT_STARTED when HA wasn't fully running. The deferral is unnecessary — async_update is fast (date arithmetic + todo list sync) and doesn't block startup. The deferred path also used asyncio.run_coroutine_threadsafe, which is designed for cross-thread scheduling and is incorrect when called from within the event loop.

Changes

  • sensor.py: Replace the CoreState.running conditional and async_listen_once/run_coroutine_threadsafe deferral with a direct await self.async_update() call. Remove now-unused imports: asyncio, CoreState, EVENT_HOMEASSISTANT_STARTED.

Before:

if self.hass.state == CoreState.running:
    await self.async_update()
else:
    self.hass.bus.async_listen_once(
        EVENT_HOMEASSISTANT_STARTED,
        lambda _event: asyncio.run_coroutine_threadsafe(
            self.async_update_ha_state(force_refresh=True), self.hass.loop
        )
    )

After:

await self.async_update()
  • test/test_sensor.py: Add TestTaskTrackerSensorAddedToHass covering: immediate update call, update called regardless of hass.state, state restoration, event listener registration, and absence of async_listen_once registration.
Original prompt

This section details on the original issue you should resolve

<issue_title>Sensor update deferral necessary?</issue_title>
<issue_description>In sensor.pys async_added_to_hass the entity update is deferred to after Home Assistant is fully started. Is this sensible or should the update be so fast that this is unnecessary even when a lot of tasks are configured?</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: gensyn <36128035+gensyn@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix sensor update deferral in async_added_to_hass Remove unnecessary startup deferral in sensor async_added_to_hass Mar 15, 2026
Copilot AI requested a review from gensyn March 15, 2026 08:34
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.

Sensor update deferral necessary?

2 participants