From 71e3047f5c544b42ffe70ec34c77b4236daea3c2 Mon Sep 17 00:00:00 2001
From: Aaron Bach <bachya1208@gmail.com>
Date: Tue, 2 Oct 2018 02:34:12 -0600
Subject: [PATCH] OpenUV: Fixed issue with missing protection window data
 (#17051)

---
 homeassistant/components/binary_sensor/openuv.py |  6 +++++-
 homeassistant/components/openuv/__init__.py      | 11 +++++++++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/homeassistant/components/binary_sensor/openuv.py b/homeassistant/components/binary_sensor/openuv.py
index c7c27d73ee4..bd6e4d1d5dc 100644
--- a/homeassistant/components/binary_sensor/openuv.py
+++ b/homeassistant/components/binary_sensor/openuv.py
@@ -93,7 +93,11 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorDevice):
 
     async def async_update(self):
         """Update the state."""
-        data = self.openuv.data[DATA_PROTECTION_WINDOW]['result']
+        data = self.openuv.data[DATA_PROTECTION_WINDOW]
+
+        if not data:
+            return
+
         if self._sensor_type == TYPE_PROTECTION_WINDOW:
             self._state = parse_datetime(
                 data['from_time']) <= utcnow() <= parse_datetime(
diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py
index 35ab16b4d1f..8485e1e3201 100644
--- a/homeassistant/components/openuv/__init__.py
+++ b/homeassistant/components/openuv/__init__.py
@@ -212,8 +212,15 @@ class OpenUV:
     async def async_update(self):
         """Update sensor/binary sensor data."""
         if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions:
-            data = await self.client.uv_protection_window()
-            self.data[DATA_PROTECTION_WINDOW] = data
+            resp = await self.client.uv_protection_window()
+            data = resp['result']
+
+            if data.get('from_time') and data.get('to_time'):
+                self.data[DATA_PROTECTION_WINDOW] = data
+            else:
+                _LOGGER.error(
+                    'No valid protection window data for this location')
+                self.data[DATA_PROTECTION_WINDOW] = {}
 
         if any(c in self.sensor_conditions for c in SENSORS):
             data = await self.client.uv_index()