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:
parent
9fd6db4b5f
commit
8cf354c042
5 changed files with 57 additions and 11 deletions
|
@ -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()
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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"]
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue