From a1f2b6d402ff249a3a27017c20a6439722504f23 Mon Sep 17 00:00:00 2001
From: Otto Winter <otto@otto-winter.com>
Date: Tue, 12 Nov 2019 18:26:46 +0100
Subject: [PATCH] ESPHome fix missing state in certain circumstances (#28729)

* Fix ESPHome having missing state in certain situations

Fixes https://github.com/esphome/issues/issues/828

* Update requirements_all

* Also fix climate preset mode
---
 homeassistant/components/esphome/binary_sensor.py | 2 ++
 homeassistant/components/esphome/climate.py       | 2 +-
 homeassistant/components/esphome/manifest.json    | 2 +-
 homeassistant/components/esphome/sensor.py        | 4 ++++
 requirements_all.txt                              | 2 +-
 requirements_test_all.txt                         | 2 +-
 6 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/homeassistant/components/esphome/binary_sensor.py b/homeassistant/components/esphome/binary_sensor.py
index 4e684638bb7..64506f69283 100644
--- a/homeassistant/components/esphome/binary_sensor.py
+++ b/homeassistant/components/esphome/binary_sensor.py
@@ -44,6 +44,8 @@ class EsphomeBinarySensor(EsphomeEntity, BinarySensorDevice):
             return self._entry_data.available
         if self._state is None:
             return None
+        if self._state.missing_state:
+            return None
         return self._state.state
 
     @property
diff --git a/homeassistant/components/esphome/climate.py b/homeassistant/components/esphome/climate.py
index 1dfe2184952..5fed8da76ef 100644
--- a/homeassistant/components/esphome/climate.py
+++ b/homeassistant/components/esphome/climate.py
@@ -138,7 +138,7 @@ class EsphomeClimateDevice(EsphomeEntity, ClimateDevice):
     @esphome_state_property
     def preset_mode(self):
         """Return current preset mode."""
-        return PRESET_AWAY if self._state.away else None
+        return PRESET_AWAY if self._state.away else PRESET_HOME
 
     @esphome_state_property
     def current_temperature(self) -> Optional[float]:
diff --git a/homeassistant/components/esphome/manifest.json b/homeassistant/components/esphome/manifest.json
index 40691c653f5..724946e6984 100644
--- a/homeassistant/components/esphome/manifest.json
+++ b/homeassistant/components/esphome/manifest.json
@@ -4,7 +4,7 @@
   "config_flow": true,
   "documentation": "https://www.home-assistant.io/integrations/esphome",
   "requirements": [
-    "aioesphomeapi==2.4.2"
+    "aioesphomeapi==2.5.0"
   ],
   "dependencies": [],
   "zeroconf": ["_esphomelib._tcp.local."],
diff --git a/homeassistant/components/esphome/sensor.py b/homeassistant/components/esphome/sensor.py
index b6adbf93c41..e50991af6c1 100644
--- a/homeassistant/components/esphome/sensor.py
+++ b/homeassistant/components/esphome/sensor.py
@@ -67,6 +67,8 @@ class EsphomeSensor(EsphomeEntity):
         """Return the state of the entity."""
         if math.isnan(self._state.state):
             return None
+        if self._state.missing_state:
+            return None
         return "{:.{prec}f}".format(
             self._state.state, prec=self._static_info.accuracy_decimals
         )
@@ -96,4 +98,6 @@ class EsphomeTextSensor(EsphomeEntity):
     @esphome_state_property
     def state(self) -> Optional[str]:
         """Return the state of the entity."""
+        if self._state.missing_state:
+            return None
         return self._state.state
diff --git a/requirements_all.txt b/requirements_all.txt
index c907fd879a2..163918f5180 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -142,7 +142,7 @@ aiobotocore==0.10.2
 aiodns==2.0.0
 
 # homeassistant.components.esphome
-aioesphomeapi==2.4.2
+aioesphomeapi==2.5.0
 
 # homeassistant.components.freebox
 aiofreepybox==0.0.8
diff --git a/requirements_test_all.txt b/requirements_test_all.txt
index 2126fed9f6f..90668e3eaab 100644
--- a/requirements_test_all.txt
+++ b/requirements_test_all.txt
@@ -50,7 +50,7 @@ aioautomatic==0.6.5
 aiobotocore==0.10.2
 
 # homeassistant.components.esphome
-aioesphomeapi==2.4.2
+aioesphomeapi==2.5.0
 
 # homeassistant.components.emulated_hue
 # homeassistant.components.http
-- 
GitLab