Provide yeelight unique_id using ssdp discovery (#35448)

* Provide yeelight unique_id using ssdp discovery

* Fixes

* Comment fix

* Cleanup initialization logic and add unique id to binary sensor

* Update homeassistant/components/yeelight/__init__.py

Co-authored-by: Teemu R. <tpr@iki.fi>

* Update homeassistant/components/yeelight/__init__.py

Co-authored-by: Teemu R. <tpr@iki.fi>

* Update comment

* Update comment

* Fix wrong model docstring

Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
zewelor 2020-05-14 19:44:32 +02:00 committed by GitHub
parent 9fd6db4b5f
commit 8cf354c042
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 11 deletions

View file

@ -2,6 +2,7 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Optional
import voluptuous as vol import voluptuous as vol
from yeelight import Bulb, BulbException from yeelight import Bulb, BulbException
@ -201,8 +202,7 @@ class YeelightDevice:
self._config = config self._config = config
self._ipaddr = ipaddr self._ipaddr = ipaddr
self._name = config.get(CONF_NAME) self._name = config.get(CONF_NAME)
self._model = config.get(CONF_MODEL) self._bulb_device = Bulb(self.ipaddr, model=config.get(CONF_MODEL))
self._bulb_device = Bulb(self.ipaddr, model=self._model)
self._device_type = None self._device_type = None
self._available = False self._available = False
self._initialized = False self._initialized = False
@ -234,8 +234,8 @@ class YeelightDevice:
@property @property
def model(self): def model(self):
"""Return configured device model.""" """Return configured/autodetected device model."""
return self._model return self._bulb_device.model
@property @property
def is_nightlight_supported(self) -> bool: def is_nightlight_supported(self) -> bool:
@ -287,6 +287,11 @@ class YeelightDevice:
return self._device_type 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): def turn_on(self, duration=DEFAULT_TRANSITION, light_type=None, power_mode=None):
"""Turn on device.""" """Turn on device."""
try: try:
@ -324,7 +329,20 @@ class YeelightDevice:
return self._available 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): def _initialize_device(self):
self._get_capabilities()
self._initialized = True self._initialized = True
dispatcher_send(self._hass, DEVICE_INITIALIZED, self.ipaddr) dispatcher_send(self._hass, DEVICE_INITIALIZED, self.ipaddr)
@ -335,8 +353,4 @@ class YeelightDevice:
def setup(self): def setup(self):
"""Fetch initial device properties.""" """Fetch initial device properties."""
initial_update = self._update_properties() self._update_properties()
# We can build correct class anyway.
if not initial_update and self.model:
self._initialize_device()

View file

@ -1,5 +1,6 @@
"""Sensor platform support for yeelight.""" """Sensor platform support for yeelight."""
import logging import logging
from typing import Optional
from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.helpers.dispatcher import async_dispatcher_connect 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 @property
def should_poll(self): def should_poll(self):
"""No polling needed.""" """No polling needed."""

View file

@ -1,5 +1,6 @@
"""Light platform support for yeelight.""" """Light platform support for yeelight."""
import logging import logging
from typing import Optional
import voluptuous as vol import voluptuous as vol
import yeelight import yeelight
@ -470,6 +471,12 @@ class YeelightGenericLight(LightEntity):
"""No polling needed.""" """No polling needed."""
return False return False
@property
def unique_id(self) -> Optional[str]:
"""Return a unique ID."""
return self.device.unique_id
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return if bulb is available.""" """Return if bulb is available."""
@ -902,6 +909,14 @@ class YeelightWithNightLight(
class YeelightNightLightMode(YeelightGenericLight): class YeelightNightLightMode(YeelightGenericLight):
"""Representation of a Yeelight when in nightlight mode.""" """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 @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the device if any.""" """Return the name of the device if any."""
@ -985,6 +1000,14 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch):
self._light_type = LightType.Ambient 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 @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the device if any.""" """Return the name of the device if any."""

View file

@ -2,7 +2,7 @@
"domain": "yeelight", "domain": "yeelight",
"name": "Yeelight", "name": "Yeelight",
"documentation": "https://www.home-assistant.io/integrations/yeelight", "documentation": "https://www.home-assistant.io/integrations/yeelight",
"requirements": ["yeelight==0.5.1"], "requirements": ["yeelight==0.5.2"],
"after_dependencies": ["discovery"], "after_dependencies": ["discovery"],
"codeowners": ["@rytilahti", "@zewelor"] "codeowners": ["@rytilahti", "@zewelor"]
} }

View file

@ -2224,7 +2224,7 @@ ya_ma==0.3.8
yalesmartalarmclient==0.1.6 yalesmartalarmclient==0.1.6
# homeassistant.components.yeelight # homeassistant.components.yeelight
yeelight==0.5.1 yeelight==0.5.2
# homeassistant.components.yeelightsunflower # homeassistant.components.yeelightsunflower
yeelightsunflower==0.0.10 yeelightsunflower==0.0.10