From a79a9809f492e8ff594f9ef3b2d9fdc046a0f081 Mon Sep 17 00:00:00 2001
From: Otto Winter <otto@otto-winter.com>
Date: Mon, 14 Oct 2019 16:02:39 +0200
Subject: [PATCH] ESPHome Fix intermediary state published (#27638)

Fixes https://github.com/esphome/issues/issues/426
---
 homeassistant/components/esphome/__init__.py | 17 ++++++++++++++---
 homeassistant/components/esphome/camera.py   |  4 ++--
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/homeassistant/components/esphome/__init__.py b/homeassistant/components/esphome/__init__.py
index bc06aba94ea..dd4ac699089 100644
--- a/homeassistant/components/esphome/__init__.py
+++ b/homeassistant/components/esphome/__init__.py
@@ -479,7 +479,9 @@ class EsphomeEntity(Entity):
         }
         self._remove_callbacks.append(
             async_dispatcher_connect(
-                self.hass, DISPATCHER_UPDATE_ENTITY.format(**kwargs), self._on_update
+                self.hass,
+                DISPATCHER_UPDATE_ENTITY.format(**kwargs),
+                self._on_state_update,
             )
         )
 
@@ -493,14 +495,23 @@ class EsphomeEntity(Entity):
             async_dispatcher_connect(
                 self.hass,
                 DISPATCHER_ON_DEVICE_UPDATE.format(**kwargs),
-                self.async_schedule_update_ha_state,
+                self._on_device_update,
             )
         )
 
-    async def _on_update(self) -> None:
+    async def _on_state_update(self) -> None:
         """Update the entity state when state or static info changed."""
         self.async_schedule_update_ha_state()
 
+    async def _on_device_update(self) -> None:
+        """Update the entity state when device info has changed."""
+        if self._entry_data.available:
+            # Don't update the HA state yet when the device comes online.
+            # Only update the HA state when the full state arrives
+            # through the next entity state packet.
+            return
+        self.async_schedule_update_ha_state()
+
     async def async_will_remove_from_hass(self) -> None:
         """Unregister callbacks."""
         for remove_callback in self._remove_callbacks:
diff --git a/homeassistant/components/esphome/camera.py b/homeassistant/components/esphome/camera.py
index cc2e0cede23..c3615c4726d 100644
--- a/homeassistant/components/esphome/camera.py
+++ b/homeassistant/components/esphome/camera.py
@@ -47,9 +47,9 @@ class EsphomeCamera(Camera, EsphomeEntity):
     def _state(self) -> Optional[CameraState]:
         return super()._state
 
-    async def _on_update(self) -> None:
+    async def _on_state_update(self) -> None:
         """Notify listeners of new image when update arrives."""
-        await super()._on_update()
+        await super()._on_state_update()
         async with self._image_cond:
             self._image_cond.notify_all()
 
-- 
GitLab