From 2bf17cba8e3e653394f87d94b615e7555a15d1c2 Mon Sep 17 00:00:00 2001
From: shred86 <32663154+shred86@users.noreply.github.com>
Date: Sat, 7 Apr 2018 01:15:35 -0800
Subject: [PATCH] Brightness conversion for Abode dimmers (#13711)

With AbodePy 0.12.3, dimmers will now work but a conversion of the brightness is required. Additionally, when a brightness value of 100 is sent to Abode, 99 is returned causing AbodePy to throw an error so this component will send 99 instead of 100.

Keeps the brightness value sent and returned from the device response consistent. However, during initialization and when a device refresh is received, Abode can return 100 thus we'll convert that case back to 99.
---
 homeassistant/components/light/abode.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 homeassistant/components/light/abode.py

diff --git a/homeassistant/components/light/abode.py b/homeassistant/components/light/abode.py
old mode 100644
new mode 100755
index bfea19fc3fa..8b7e09d86bc
--- a/homeassistant/components/light/abode.py
+++ b/homeassistant/components/light/abode.py
@@ -5,7 +5,7 @@ For more details about this platform, please refer to the documentation at
 https://home-assistant.io/components/light.abode/
 """
 import logging
-
+from math import ceil
 from homeassistant.components.abode import AbodeDevice, DOMAIN as ABODE_DOMAIN
 from homeassistant.components.light import (
     ATTR_BRIGHTNESS, ATTR_HS_COLOR,
@@ -51,7 +51,9 @@ class AbodeLight(AbodeDevice, Light):
                 *kwargs[ATTR_HS_COLOR]))
 
         if ATTR_BRIGHTNESS in kwargs and self._device.is_dimmable:
-            self._device.set_level(kwargs[ATTR_BRIGHTNESS])
+            # Convert HASS brightness (0-255) to Abode brightness (0-99)
+            # If 100 is sent to Abode, response is 99 causing an error
+            self._device.set_level(ceil(kwargs[ATTR_BRIGHTNESS] * 99 / 255.0))
         else:
             self._device.switch_on()
 
@@ -68,7 +70,12 @@ class AbodeLight(AbodeDevice, Light):
     def brightness(self):
         """Return the brightness of the light."""
         if self._device.is_dimmable and self._device.has_brightness:
-            return self._device.brightness
+            brightness = int(self._device.brightness)
+            # Abode returns 100 during device initialization and device refresh
+            if brightness == 100:
+                return 255
+            # Convert Abode brightness (0-99) to HASS brightness (0-255)
+            return ceil(brightness * 255 / 99.0)
 
     @property
     def hs_color(self):
-- 
GitLab