From f3e16ca30490b31e6a29d51c52acabe8f8ab6518 Mon Sep 17 00:00:00 2001
From: John Mihalic <mezz@johnmihalic.com>
Date: Thu, 3 Aug 2017 09:58:40 -0400
Subject: [PATCH] Catch divide by zero errors when a sleep type is 0 (#8809)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add an optional extended description…
---
 homeassistant/components/sensor/eight_sleep.py | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/homeassistant/components/sensor/eight_sleep.py b/homeassistant/components/sensor/eight_sleep.py
index f7d42a9f5bd..e6f4addf003 100644
--- a/homeassistant/components/sensor/eight_sleep.py
+++ b/homeassistant/components/sensor/eight_sleep.py
@@ -197,10 +197,16 @@ class EightUserSensor(EightSleepUserEntity):
         sleep_time = sum(self._attr['breakdown'].values()) - \
             self._attr['breakdown']['awake']
         state_attr[ATTR_SLEEP_DUR] = sleep_time
-        state_attr[ATTR_LIGHT_PERC] = round((
-            self._attr['breakdown']['light'] / sleep_time) * 100, 2)
-        state_attr[ATTR_DEEP_PERC] = round((
-            self._attr['breakdown']['deep'] / sleep_time) * 100, 2)
+        try:
+            state_attr[ATTR_LIGHT_PERC] = round((
+                self._attr['breakdown']['light'] / sleep_time) * 100, 2)
+        except ZeroDivisionError:
+            state_attr[ATTR_LIGHT_PERC] = 0
+        try:
+            state_attr[ATTR_DEEP_PERC] = round((
+                self._attr['breakdown']['deep'] / sleep_time) * 100, 2)
+        except ZeroDivisionError:
+            state_attr[ATTR_DEEP_PERC] = 0
 
         if self._units == 'si':
             room_temp = round(self._attr['room_temp'], 2)
-- 
GitLab