From 193188b965c666f4a2620989dc9e332c22990d89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maciej=20Soko=C5=82owski?= <matemaciek@gmail.com>
Date: Sun, 22 Oct 2017 17:22:51 +0200
Subject: [PATCH] RGB Tradfri simple support (#9703)

---
 homeassistant/components/light/tradfri.py | 35 +++++++++++------------
 homeassistant/components/tradfri.py       |  2 +-
 requirements_all.txt                      |  2 +-
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/homeassistant/components/light/tradfri.py b/homeassistant/components/light/tradfri.py
index ff9201d49b9..0a54522d7b7 100644
--- a/homeassistant/components/light/tradfri.py
+++ b/homeassistant/components/light/tradfri.py
@@ -161,14 +161,16 @@ class TradfriLight(Light):
     @property
     def min_mireds(self):
         """Return the coldest color_temp that this light supports."""
-        from pytradfri.color import MAX_KELVIN_WS
-        return color_util.color_temperature_kelvin_to_mired(MAX_KELVIN_WS)
+        return color_util.color_temperature_kelvin_to_mired(
+            self._light_control.max_kelvin
+        )
 
     @property
     def max_mireds(self):
         """Return the warmest color_temp that this light supports."""
-        from pytradfri.color import MIN_KELVIN_WS
-        return color_util.color_temperature_kelvin_to_mired(MIN_KELVIN_WS)
+        return color_util.color_temperature_kelvin_to_mired(
+            self._light_control.min_kelvin
+        )
 
     @property
     def device_state_attributes(self):
@@ -217,12 +219,8 @@ class TradfriLight(Light):
     @property
     def color_temp(self):
         """Return the CT color value in mireds."""
-        if (self._light_data.kelvin_color is None or
-                self.supported_features & SUPPORT_COLOR_TEMP == 0 or
-                not self._temp_supported):
-            return None
         return color_util.color_temperature_kelvin_to_mired(
-            self._light_data.kelvin_color
+            self._light_data.kelvin_color_inferred
         )
 
     @property
@@ -297,10 +295,13 @@ class TradfriLight(Light):
         self._rgb_color = None
         self._features = SUPPORTED_FEATURES
 
-        if self._light_data.hex_color is not None:
-            if self._light.device_info.manufacturer == IKEA:
+        if self._light.device_info.manufacturer == IKEA:
+            if self._light_control.can_set_kelvin:
                 self._features |= SUPPORT_COLOR_TEMP
-            else:
+            if self._light_control.can_set_color:
+                self._features |= SUPPORT_RGB_COLOR
+        else:
+            if self._light_data.hex_color is not None:
                 self._features |= SUPPORT_RGB_COLOR
 
         self._temp_supported = self._light.device_info.manufacturer \
@@ -309,11 +310,7 @@ class TradfriLight(Light):
     def _observe_update(self, tradfri_device):
         """Receive new state data for this light."""
         self._refresh(tradfri_device)
-
-        # Handle Hue lights paired with the gateway
-        # hex_color is 0 when bulb is unreachable
-        if self._light_data.hex_color not in (None, '0'):
-            self._rgb_color = color_util.rgb_hex_to_rgb_list(
-                self._light_data.hex_color)
-
+        self._rgb_color = color_util.rgb_hex_to_rgb_list(
+            self._light_data.hex_color_inferred
+        )
         self.hass.async_add_job(self.async_update_ha_state())
diff --git a/homeassistant/components/tradfri.py b/homeassistant/components/tradfri.py
index a24305c7fd4..2e4bf260237 100644
--- a/homeassistant/components/tradfri.py
+++ b/homeassistant/components/tradfri.py
@@ -16,7 +16,7 @@ from homeassistant.helpers import discovery
 from homeassistant.const import CONF_HOST, CONF_API_KEY
 from homeassistant.components.discovery import SERVICE_IKEA_TRADFRI
 
-REQUIREMENTS = ['pytradfri==3.0',
+REQUIREMENTS = ['pytradfri==3.0.2',
                 'DTLSSocket==0.1.3',
                 'https://github.com/chrysn/aiocoap/archive/'
                 '3286f48f0b949901c8b5c04c0719dc54ab63d431.zip'
diff --git a/requirements_all.txt b/requirements_all.txt
index 52cf6771693..77c0b33cbaf 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -852,7 +852,7 @@ pythonegardia==1.0.22
 pytrackr==0.0.5
 
 # homeassistant.components.tradfri
-pytradfri==3.0
+pytradfri==3.0.2
 
 # homeassistant.components.device_tracker.unifi
 pyunifi==2.13