Repository: home-assistant/core
Pull request #144161: Add support for updating ESPHome deep sleep devices
Author: login: bdraco
Labels: name: cla-signed
name: new-feature
name: integration: esphome
name: by-code-owner
name: WTH
description: Issues & PRs generated from the "Month of What the Heck?"
name: Quality Scale: platinum
Created: 2025-05-03T18:43:02Z | Merged: 2025-05-04T01:50:18Z


## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
Updating deep sleep devices is currently a challenge since they can only receive OTA updates while awake. This modification ensures the update entity waits for the device to wake up before sending the OTA. This removes the need to time updates perfectly and hope they upload before the device goes back to sleep.

This is a WTH/Feature request:
https://community.home-assistant.io/t/wth-esphome-cant-handle-firmware-updates-automatically-for-deep-sleep-devices/471258
https://community.home-assistant.io/t/allow-deep-sleep-esphome-devices-to-install-updates-using-ha/791196

## Type of change
<!--
  What type of change does your PR introduce to Home Assistant?
  NOTE: Please, check only 1! box!
  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.
-->

- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] 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
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- 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
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [ ] 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][dev-checklist]
- [ ] I have followed the [perfect PR recommendations][perfect-pr]
- [ ] The code has been formatted using Ruff (`ruff format homeassistant tests`)
- [ ] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

- [ ] The [manifest file][manifest-docs] 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 link to the changelog, or at minimum a diff between library versions is added to the PR description.

<!--
  This project is very active and we have a high turnover of pull requests.

  Unfortunately, the number of incoming pull requests is higher than what our
  reviewers can review and merge so there is a long backlog of pull requests
  waiting for review. You can help here!
  
  By reviewing another pull request, you will help raise the code quality of
  that pull request and the final review will be faster. This way the general
  pace of pull request reviews will go up and your wait time will go down.
  
  When picking a pull request to review, try to choose one that hasn't yet
  been reviewed.

  Thanks for helping out!
-->

To help with the load of incoming pull requests:

- [ ] I have reviewed two other [open pull requests][prs] in this repository.

[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure

<!--
  Thank you for contributing <3

  Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr



Diff:
diff --git a/homeassistant/components/esphome/strings.json b/homeassistant/components/esphome/strings.json
index bc198d514abd70..eab88e8df95f38 100644
--- a/homeassistant/components/esphome/strings.json
+++ b/homeassistant/components/esphome/strings.json
@@ -195,7 +195,10 @@
       "message": "Error compiling {configuration}; Try again in ESPHome dashboard for more information."
     },
     "error_uploading": {
-      "message": "Error during OTA of {configuration}; Try again in ESPHome dashboard for more information."
+      "message": "Error during OTA (Over-The-Air) of {configuration}; Try again in ESPHome dashboard for more information."
+    },
+    "ota_in_progress": {
+      "message": "An OTA (Over-The-Air) update is already in progress for {configuration}."
     }
   }
 }
diff --git a/homeassistant/components/esphome/update.py b/homeassistant/components/esphome/update.py
index 01ac638bdb16c1..d24d89194616c5 100644
--- a/homeassistant/components/esphome/update.py
+++ b/homeassistant/components/esphome/update.py
@@ -125,21 +125,17 @@ def __init__(
                 (dr.CONNECTION_NETWORK_MAC, entry_data.device_info.mac_address)
             }
         )
+        self._install_lock = asyncio.Lock()
+        self._available_future: asyncio.Future[None] | None = None
         self._update_attrs()
 
     @callback
     def _update_attrs(self) -> None:
         """Update the supported features."""
-        # If the device has deep sleep, we can't assume we can install updates
-        # as the ESP will not be connectable (by design).
         coordinator = self.coordinator
         device_info = self._device_info
         # Install support can change at run time
-        if (
-            coordinator.last_update_success
-            and coordinator.supports_update
-            and not device_info.has_deep_sleep
-        ):
+        if coordinator.last_update_success and coordinator.supports_update:
             self._attr_supported_features = UpdateEntityFeature.INSTALL
         else:
             self._attr_supported_features = NO_FEATURES
@@ -178,6 +174,13 @@ def _handle_device_update(
         self, static_info: list[EntityInfo] | None = None
     ) -> None:
         """Handle updated data from the device."""
+        if (
+            self._entry_data.available
+            and self._available_future
+            and not self._available_future.done()
+        ):
+            self._available_future.set_result(None)
+            self._available_future = None
         self._update_attrs()
         self.async_write_ha_state()
 
@@ -192,17 +195,46 @@ async def async_added_to_hass(self) -> None:
             entry_data.async_subscribe_device_updated(self._handle_device_update)
         )
 
+    async def async_will_remove_from_hass(self) -> None:
+        """Handle entity about to be removed from Home Assistant."""
+        if self._available_future and not self._available_future.done():
+            self._available_future.cancel()
+            self._available_future = None
+
+    async def _async_wait_available(self) -> None:
+        """Wait until the device is available."""
+        # If the device has deep sleep, we need to wait for it to wake up
+        # and connect to the network to be able to install the update.
+        if self._entry_data.available:
+            return
+        self._available_future = self.hass.loop.create_future()
+        try:
+            await self._available_future
+        finally:
+            self._available_future = None
+
     async def async_install(
         self, version: str | None, backup: bool, **kwargs: Any
     ) -> None:
         """Install an update."""
-        async with self.hass.data.setdefault(KEY_UPDATE_LOCK, asyncio.Lock()):
-            coordinator = self.coordinator
-            api = coordinator.api
-            device = coordinator.data.get(self._device_info.name)
-            assert device is not None
-            configuration = device["configuration"]
-            try:
+        if self._install_lock.locked():
+            raise HomeAssistantError(
+                translation_domain=DOMAIN,
+                translation_key="ota_in_progress",
+                translation_placeholders={
+                    "configuration": self._device_info.name,
+                },
+            )
+
+        # Ensure only one OTA per device at a time
+        async with self._install_lock:
+            # Ensure only one compile at a time for ALL devices
+            async with self.hass.data.setdefault(KEY_UPDATE_LOCK, asyncio.Lock()):
+                coordinator = self.coordinator
+                api = coordinator.api
+                device = coordinator.data.get(self._device_info.name)
+                assert device is not None
+                configuration = device["configuration"]
                 if not await api.compile(configuration):
                     raise HomeAssistantError(
                         translation_domain=DOMAIN,
@@ -211,14 +243,25 @@ async def async_install(
                             "configuration": configuration,
                         },
                     )
-                if not await api.upload(configuration, "OTA"):
-                    raise HomeAssistantError(
-                        translation_domain=DOMAIN,
-                        translation_key="error_uploading",
-                        translation_placeholders={
-                            "configuration": configuration,
-                        },
-                    )
+
+            # If the device uses deep sleep, there's a small chance it goes
+            # to sleep right after the dashboard connects but before the OTA
+            # starts. In that case, the update won't go through, so we try
+            # again to catch it on its next wakeup.
+            attempts = 2 if self._device_info.has_deep_sleep else 1
+            try:
+                for attempt in range(1, attempts + 1):
+                    await self._async_wait_available()
+                    if await api.upload(configuration, "OTA"):
+                        break
+                    if attempt == attempts:
+                        raise HomeAssistantError(
+                            translation_domain=DOMAIN,
+                            translation_key="error_uploading",
+                            translation_placeholders={
+                                "configuration": configuration,
+                            },
+                        )
             finally:
                 await self.coordinator.async_request_refresh()
 
diff --git a/tests/components/esphome/test_update.py b/tests/components/esphome/test_update.py
index c9b88d9fb57fc1..63294a6ad69df0 100644
--- a/tests/components/esphome/test_update.py
+++ b/tests/components/esphome/test_update.py
@@ -1,5 +1,6 @@
 """Test ESPHome update entities."""
 
+import asyncio
 from typing import Any
 from unittest.mock import patch
 
@@ -546,3 +547,296 @@ async def test_generic_device_update_entity_has_update(
     )
 
     mock_client.update_command.assert_called_with(key=1, command=UpdateCommand.CHECK)
+
+
+async def test_attempt_to_update_twice(
+    hass: HomeAssistant,
+    mock_client: APIClient,
+    mock_esphome_device: MockESPHomeDeviceType,
+    mock_dashboard: dict[str, Any],
+) -> None:
+    """Test attempting to update twice."""
+    mock_dashboard["configured"] = [
+        {
+            "name": "test",
+            "current_version": "2023.2.0-dev",
+            "configuration": "test.yaml",
+        }
+    ]
+    await async_get_dashboard(hass).async_refresh()
+    await mock_esphome_device(
+        mock_client=mock_client,
+        entity_info=[],
+        user_service=[],
+        states=[],
+    )
+    await hass.async_block_till_done()
+    state = hass.states.get("update.test_firmware")
+    assert state is not None
+
+    async def delayed_compile(*args: Any, **kwargs: Any) -> None:
+        """Delay the update."""
+        await asyncio.sleep(0)
+        return True
+
+    # Compile success, upload fails
+    with (
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.compile",
+            delayed_compile,
+        ),
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.upload",
+            return_value=False,
+        ),
+    ):
+        update_task = hass.async_create_task(
+            hass.services.async_call(
+                UPDATE_DOMAIN,
+                SERVICE_INSTALL,
+                {ATTR_ENTITY_ID: "update.test_firmware"},
+                blocking=True,
+            )
+        )
+
+        with pytest.raises(HomeAssistantError, match="update is already in progress"):
+            await hass.services.async_call(
+                UPDATE_DOMAIN,
+                SERVICE_INSTALL,
+                {ATTR_ENTITY_ID: "update.test_firmware"},
+                blocking=True,
+            )
+
+        with pytest.raises(HomeAssistantError, match="OTA"):
+            await update_task
+
+
+async def test_update_deep_sleep_already_online(
+    hass: HomeAssistant,
+    mock_client: APIClient,
+    mock_esphome_device: MockESPHomeDeviceType,
+    mock_dashboard: dict[str, Any],
+) -> None:
+    """Test attempting to update twice."""
+    mock_dashboard["configured"] = [
+        {
+            "name": "test",
+            "current_version": "2023.2.0-dev",
+            "configuration": "test.yaml",
+        }
+    ]
+    await async_get_dashboard(hass).async_refresh()
+    await mock_esphome_device(
+        mock_client=mock_client,
+        entity_info=[],
+        user_service=[],
+        states=[],
+        device_info={"has_deep_sleep": True},
+    )
+    await hass.async_block_till_done()
+    state = hass.states.get("update.test_firmware")
+    assert state is not None
+
+    # Compile success, upload success
+    with (
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.compile",
+            return_value=True,
+        ),
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.upload",
+            return_value=True,
+        ),
+    ):
+        await hass.services.async_call(
+            UPDATE_DOMAIN,
+            SERVICE_INSTALL,
+            {ATTR_ENTITY_ID: "update.test_firmware"},
+            blocking=True,
+        )
+
+
+async def test_update_deep_sleep_offline(
+    hass: HomeAssistant,
+    mock_client: APIClient,
+    mock_esphome_device: MockESPHomeDeviceType,
+    mock_dashboard: dict[str, Any],
+) -> None:
+    """Test device comes online while updating."""
+    mock_dashboard["configured"] = [
+        {
+            "name": "test",
+            "current_version": "2023.2.0-dev",
+            "configuration": "test.yaml",
+        }
+    ]
+    await async_get_dashboard(hass).async_refresh()
+    device = await mock_esphome_device(
+        mock_client=mock_client,
+        entity_info=[],
+        user_service=[],
+        states=[],
+        device_info={"has_deep_sleep": True},
+    )
+    await hass.async_block_till_done()
+    state = hass.states.get("update.test_firmware")
+    assert state is not None
+    await device.mock_disconnect(True)
+
+    # Compile success, upload success
+    with (
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.compile",
+            return_value=True,
+        ),
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.upload",
+            return_value=True,
+        ),
+    ):
+        update_task = hass.async_create_task(
+            hass.services.async_call(
+                UPDATE_DOMAIN,
+                SERVICE_INSTALL,
+                {ATTR_ENTITY_ID: "update.test_firmware"},
+                blocking=True,
+            )
+        )
+        await asyncio.sleep(0)
+        assert not update_task.done()
+        await device.mock_connect()
+        await hass.async_block_till_done()
+
+
+async def test_update_deep_sleep_offline_sleep_during_ota(
+    hass: HomeAssistant,
+    mock_client: APIClient,
+    mock_esphome_device: MockESPHomeDeviceType,
+    mock_dashboard: dict[str, Any],
+) -> None:
+    """Test device goes to sleep right as we start the OTA."""
+    mock_dashboard["configured"] = [
+        {
+            "name": "test",
+            "current_version": "2023.2.0-dev",
+            "configuration": "test.yaml",
+        }
+    ]
+    await async_get_dashboard(hass).async_refresh()
+    device = await mock_esphome_device(
+        mock_client=mock_client,
+        entity_info=[],
+        user_service=[],
+        states=[],
+        device_info={"has_deep_sleep": True},
+    )
+    await hass.async_block_till_done()
+    state = hass.states.get("update.test_firmware")
+    assert state is not None
+    await device.mock_disconnect(True)
+
+    upload_attempt = 0
+    upload_attempt_2_future = hass.loop.create_future()
+    disconnect_future = hass.loop.create_future()
+
+    async def upload_takes_a_while(*args: Any, **kwargs: Any) -> None:
+        """Delay the update."""
+        nonlocal upload_attempt
+        upload_attempt += 1
+        if upload_attempt == 1:
+            # We are simulating the device going back to sleep
+            # before the upload can be started
+            # Wait for the device to go unavailable
+            # before returning false
+            await disconnect_future
+            return False
+        upload_attempt_2_future.set_result(None)
+        return True
+
+    # Compile success, upload fails first time, success second time
+    with (
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.compile",
+            return_value=True,
+        ),
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.upload",
+            upload_takes_a_while,
+        ),
+    ):
+        update_task = hass.async_create_task(
+            hass.services.async_call(
+                UPDATE_DOMAIN,
+                SERVICE_INSTALL,
+                {ATTR_ENTITY_ID: "update.test_firmware"},
+                blocking=True,
+            )
+        )
+        await asyncio.sleep(0)
+        assert not update_task.done()
+        await device.mock_connect()
+        # Mock device being at the end of its sleep cycle
+        # and going to sleep right as the upload starts
+        # This can happen because there is non zero time
+        # between when we tell the dashboard to upload and
+        # when the upload actually starts
+        await device.mock_disconnect(True)
+        disconnect_future.set_result(None)
+        assert not upload_attempt_2_future.done()
+        # Now the device wakes up and the upload is attempted
+        await device.mock_connect()
+        await upload_attempt_2_future
+        await hass.async_block_till_done()
+
+
+async def test_update_deep_sleep_offline_cancelled_unload(
+    hass: HomeAssistant,
+    mock_client: APIClient,
+    mock_esphome_device: MockESPHomeDeviceType,
+    mock_dashboard: dict[str, Any],
+) -> None:
+    """Test deep sleep update attempt is cancelled on unload."""
+    mock_dashboard["configured"] = [
+        {
+            "name": "test",
+            "current_version": "2023.2.0-dev",
+            "configuration": "test.yaml",
+        }
+    ]
+    await async_get_dashboard(hass).async_refresh()
+    device = await mock_esphome_device(
+        mock_client=mock_client,
+        entity_info=[],
+        user_service=[],
+        states=[],
+        device_info={"has_deep_sleep": True},
+    )
+    await hass.async_block_till_done()
+    state = hass.states.get("update.test_firmware")
+    assert state is not None
+    await device.mock_disconnect(True)
+
+    # Compile success, upload success, but we cancel the update
+    with (
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.compile",
+            return_value=True,
+        ),
+        patch(
+            "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.upload",
+            return_value=True,
+        ),
+    ):
+        update_task = hass.async_create_task(
+            hass.services.async_call(
+                UPDATE_DOMAIN,
+                SERVICE_INSTALL,
+                {ATTR_ENTITY_ID: "update.test_firmware"},
+                blocking=True,
+            )
+        )
+        await asyncio.sleep(0)
+        assert not update_task.done()
+        await hass.config_entries.async_unload(device.entry.entry_id)
+        await hass.async_block_till_done()
+        assert update_task.cancelled()


Reviews:
user: login: kbx81
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-03T19:07:20Z
user: login: bdraco
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-03T19:19:56Z
user: login: balloob
body: Genius.
state: APPROVED
author_association: MEMBER
submitted_at: 2025-05-04T00:39:14Z

Review comments:
diff_hunk: @@ -195,7 +195,10 @@
       "message": "Error compiling {configuration}; Try again in ESPHome dashboard for more information."
     },
     "error_uploading": {
-      "message": "Error during OTA of {configuration}; Try again in ESPHome dashboard for more information."
+      "message": "Error during OTA (Over-The-Air) of {configuration}; Try again in ESPHome dashboard for more information."
+    },
+    "ota_in_progress": {
+      "message": "An OTA (Over-The-Air) update is already progress for {configuration}."
path: homeassistant/components/esphome/strings.json
user: login: kbx81
body: Typo/word missing here I think
```suggestion
      "message": "An OTA (Over-The-Air) update is already in progress for {configuration}."
```
created_at: 2025-05-03T19:07:20Z
author_association: CONTRIBUTOR
diff_hunk: @@ -195,7 +195,10 @@
       "message": "Error compiling {configuration}; Try again in ESPHome dashboard for more information."
     },
     "error_uploading": {
-      "message": "Error during OTA of {configuration}; Try again in ESPHome dashboard for more information."
+      "message": "Error during OTA (Over-The-Air) of {configuration}; Try again in ESPHome dashboard for more information."
+    },
+    "ota_in_progress": {
+      "message": "An OTA (Over-The-Air) update is already progress for {configuration}."
path: homeassistant/components/esphome/strings.json
user: login: bdraco
body: thanks!
created_at: 2025-05-03T19:19:55Z
author_association: MEMBER

PR comments:
user: login: home-assistant[bot]
created_at: 2025-05-03T18:43:10Z
body: 
Hey there @ottowinter, @jesserockz, @kbx81, mind taking a look at this pull request as it has been labeled with an integration (`esphome`) you are listed as a [code owner](https://github.com/home-assistant/core/blob/dev/CODEOWNERS#L446) for? Thanks!

<details>
<summary>Code owner commands</summary>


Code owners of `esphome` can trigger bot actions by commenting:

- `@home-assistant close` Closes the pull request.
- `@home-assistant rename Awesome new title` Renames the pull request.
- `@home-assistant reopen` Reopen the pull request.
- `@home-assistant unassign esphome` Removes the current integration label and assignees on the pull request, add the integration domain after the command.
- `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
- `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

</details>


author_association: CONTRIBUTOR
user: login: bdraco
created_at: 2025-05-04T00:49:56Z
body: Looks of downgrade/upgrading everything working great.  All the edge cases seem to be taken care of now.

Will start working on tests
author_association: MEMBER