diff --git a/homeassistant/components/yeelight/__init__.py b/homeassistant/components/yeelight/__init__.py index 1e0fe841cac..c36c7be00fa 100644 --- a/homeassistant/components/yeelight/__init__.py +++ b/homeassistant/components/yeelight/__init__.py @@ -2,6 +2,7 @@ from datetime import timedelta import logging +from typing import Optional import voluptuous as vol from yeelight import Bulb, BulbException @@ -201,8 +202,7 @@ class YeelightDevice: self._config = config self._ipaddr = ipaddr self._name = config.get(CONF_NAME) - self._model = config.get(CONF_MODEL) - self._bulb_device = Bulb(self.ipaddr, model=self._model) + self._bulb_device = Bulb(self.ipaddr, model=config.get(CONF_MODEL)) self._device_type = None self._available = False self._initialized = False @@ -234,8 +234,8 @@ class YeelightDevice: @property def model(self): - """Return configured device model.""" - return self._model + """Return configured/autodetected device model.""" + return self._bulb_device.model @property def is_nightlight_supported(self) -> bool: @@ -287,6 +287,11 @@ class YeelightDevice: return self._device_type + @property + def unique_id(self) -> Optional[str]: + """Return a unique ID.""" + return self.bulb.capabilities.get("id") + def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None, power_mode=None): """Turn on device.""" try: @@ -324,7 +329,20 @@ class YeelightDevice: return self._available + def _get_capabilities(self): + """Request device capabilities.""" + try: + self.bulb.get_capabilities() + except BulbException as ex: + _LOGGER.error( + "Unable to get device capabilities %s, %s: %s", + self.ipaddr, + self.name, + ex, + ) + def _initialize_device(self): + self._get_capabilities() self._initialized = True dispatcher_send(self._hass, DEVICE_INITIALIZED, self.ipaddr) @@ -335,8 +353,4 @@ class YeelightDevice: def setup(self): """Fetch initial device properties.""" - initial_update = self._update_properties() - - # We can build correct class anyway. - if not initial_update and self.model: - self._initialize_device() + self._update_properties() diff --git a/homeassistant/components/yeelight/binary_sensor.py b/homeassistant/components/yeelight/binary_sensor.py index 78a20ab0104..cc2145d93b4 100644 --- a/homeassistant/components/yeelight/binary_sensor.py +++ b/homeassistant/components/yeelight/binary_sensor.py @@ -1,5 +1,6 @@ """Sensor platform support for yeelight.""" import logging +from typing import Optional from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -38,6 +39,14 @@ class YeelightNightlightModeSensor(BinarySensorEntity): ) ) + @property + def unique_id(self) -> Optional[str]: + """Return a unique ID.""" + unique = self._device.unique_id + + if unique: + return unique + "-nightlight_sensor" + @property def should_poll(self): """No polling needed.""" diff --git a/homeassistant/components/yeelight/light.py b/homeassistant/components/yeelight/light.py index 29f943906d6..e0ece2afdb9 100644 --- a/homeassistant/components/yeelight/light.py +++ b/homeassistant/components/yeelight/light.py @@ -1,5 +1,6 @@ """Light platform support for yeelight.""" import logging +from typing import Optional import voluptuous as vol import yeelight @@ -470,6 +471,12 @@ class YeelightGenericLight(LightEntity): """No polling needed.""" return False + @property + def unique_id(self) -> Optional[str]: + """Return a unique ID.""" + + return self.device.unique_id + @property def available(self) -> bool: """Return if bulb is available.""" @@ -902,6 +909,14 @@ class YeelightWithNightLight( class YeelightNightLightMode(YeelightGenericLight): """Representation of a Yeelight when in nightlight mode.""" + @property + def unique_id(self) -> Optional[str]: + """Return a unique ID.""" + unique = super().unique_id + + if unique: + return unique + "-nightlight" + @property def name(self) -> str: """Return the name of the device if any.""" @@ -985,6 +1000,14 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch): self._light_type = LightType.Ambient + @property + def unique_id(self) -> Optional[str]: + """Return a unique ID.""" + unique = super().unique_id + + if unique: + return unique + "-ambilight" + @property def name(self) -> str: """Return the name of the device if any.""" diff --git a/homeassistant/components/yeelight/manifest.json b/homeassistant/components/yeelight/manifest.json index ad3022d5d5a..32ccf1c117e 100644 --- a/homeassistant/components/yeelight/manifest.json +++ b/homeassistant/components/yeelight/manifest.json @@ -2,7 +2,7 @@ "domain": "yeelight", "name": "Yeelight", "documentation": "https://www.home-assistant.io/integrations/yeelight", - "requirements": ["yeelight==0.5.1"], + "requirements": ["yeelight==0.5.2"], "after_dependencies": ["discovery"], "codeowners": ["@rytilahti", "@zewelor"] } diff --git a/requirements_all.txt b/requirements_all.txt index 518e91194ff..5f595e0c5a5 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2224,7 +2224,7 @@ ya_ma==0.3.8 yalesmartalarmclient==0.1.6 # homeassistant.components.yeelight -yeelight==0.5.1 +yeelight==0.5.2 # homeassistant.components.yeelightsunflower yeelightsunflower==0.0.10