Fix yeelight connection issue (#40251)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
08342a1e05
commit
f23fcfcd9b
10 files changed
+230
-126
No files matched your search
@@ -7,7 +7,7 @@ from typing import Optional
|
||||
import voluptuous as vol
|
||||
from yeelight import Bulb, BulbException, discover_bulbs
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry, ConfigEntryNotReady
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICES,
|
||||
CONF_HOST,
|
||||
@@ -180,8 +180,8 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up Yeelight from a config entry."""
|
||||
|
||||
async def _initialize(host: str) -> None:
|
||||
device = await _async_setup_device(hass, host, entry.options)
|
||||
async def _initialize(host: str, capabilities: Optional[dict] = None) -> None:
|
||||
device = await _async_setup_device(hass, host, entry, capabilities)
|
||||
hass.data[DOMAIN][DATA_CONFIG_ENTRIES][entry.entry_id][DATA_DEVICE] = device
|
||||
for component in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
@@ -252,20 +252,33 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
async def _async_setup_device(
|
||||
hass: HomeAssistant,
|
||||
host: str,
|
||||
config: dict,
|
||||
entry: ConfigEntry,
|
||||
capabilities: Optional[dict],
|
||||
) -> None:
|
||||
# Get model from config and capabilities
|
||||
model = entry.options.get(CONF_MODEL)
|
||||
if not model and capabilities is not None:
|
||||
model = capabilities.get("model")
|
||||
|
||||
# Set up device
|
||||
bulb = Bulb(host, model=config.get(CONF_MODEL) or None)
|
||||
capabilities = await hass.async_add_executor_job(bulb.get_capabilities)
|
||||
if capabilities is None: # timeout
|
||||
_LOGGER.error("Failed to get capabilities from %s", host)
|
||||
raise ConfigEntryNotReady
|
||||
device = YeelightDevice(hass, host, config, bulb)
|
||||
bulb = Bulb(host, model=model or None)
|
||||
if capabilities is None:
|
||||
capabilities = await hass.async_add_executor_job(bulb.get_capabilities)
|
||||
|
||||
device = YeelightDevice(hass, host, entry.options, bulb, capabilities)
|
||||
await hass.async_add_executor_job(device.update)
|
||||
await device.async_setup()
|
||||
return device
|
||||
|
||||
|
||||
@callback
|
||||
def _async_unique_name(capabilities: dict) -> str:
|
||||
"""Generate name from capabilities."""
|
||||
model = capabilities["model"]
|
||||
unique_id = capabilities["id"]
|
||||
return f"yeelight_{model}_{unique_id}"
|
||||
|
||||
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Handle options update."""
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
@@ -332,7 +345,7 @@ class YeelightScanner:
|
||||
"""Register callback function."""
|
||||
host = self._seen.get(unique_id)
|
||||
if host is not None:
|
||||
self._hass.async_add_job(callback_func(host))
|
||||
self._hass.async_create_task(callback_func(host))
|
||||
else:
|
||||
self._callbacks[unique_id] = callback_func
|
||||
if len(self._callbacks) == 1:
|
||||
@@ -351,18 +364,25 @@ class YeelightScanner:
|
||||
class YeelightDevice:
|
||||
"""Represents single Yeelight device."""
|
||||
|
||||
def __init__(self, hass, host, config, bulb):
|
||||
def __init__(self, hass, host, config, bulb, capabilities):
|
||||
"""Initialize device."""
|
||||
self._hass = hass
|
||||
self._config = config
|
||||
self._host = host
|
||||
unique_id = bulb.capabilities.get("id")
|
||||
self._name = config.get(CONF_NAME) or f"yeelight_{bulb.model}_{unique_id}"
|
||||
self._bulb_device = bulb
|
||||
self._capabilities = capabilities or {}
|
||||
self._device_type = None
|
||||
self._available = False
|
||||
self._remove_time_tracker = None
|
||||
|
||||
self._name = host # Default name is host
|
||||
if capabilities:
|
||||
# Generate name from model and id when capabilities is available
|
||||
self._name = _async_unique_name(capabilities)
|
||||
if config.get(CONF_NAME):
|
||||
# Override default name when name is set in config
|
||||
self._name = config[CONF_NAME]
|
||||
|
||||
@property
|
||||
def bulb(self):
|
||||
"""Return bulb device."""
|
||||
@@ -396,7 +416,7 @@ class YeelightDevice:
|
||||
@property
|
||||
def fw_version(self):
|
||||
"""Return the firmware version."""
|
||||
return self._bulb_device.capabilities.get("fw_ver")
|
||||
return self._capabilities.get("fw_ver")
|
||||
|
||||
@property
|
||||
def is_nightlight_supported(self) -> bool:
|
||||
@@ -449,11 +469,6 @@ 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:
|
||||
@@ -532,15 +547,24 @@ class YeelightDevice:
|
||||
class YeelightEntity(Entity):
|
||||
"""Represents single Yeelight entity."""
|
||||
|
||||
def __init__(self, device: YeelightDevice):
|
||||
def __init__(self, device: YeelightDevice, entry: ConfigEntry):
|
||||
"""Initialize the entity."""
|
||||
self._device = device
|
||||
self._unique_id = entry.entry_id
|
||||
if entry.unique_id is not None:
|
||||
# Use entry unique id (device id) whenever possible
|
||||
self._unique_id = entry.unique_id
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique ID."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> dict:
|
||||
"""Return the device info."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self._device.unique_id)},
|
||||
"identifiers": {(DOMAIN, self._unique_id)},
|
||||
"name": self._device.name,
|
||||
"manufacturer": "Yeelight",
|
||||
"model": self._device.model,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"""Sensor platform support for yeelight."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@@ -19,7 +18,7 @@ async def async_setup_entry(
|
||||
device = hass.data[DOMAIN][DATA_CONFIG_ENTRIES][config_entry.entry_id][DATA_DEVICE]
|
||||
if device.is_nightlight_supported:
|
||||
_LOGGER.debug("Adding nightlight mode sensor for %s", device.name)
|
||||
async_add_entities([YeelightNightlightModeSensor(device)])
|
||||
async_add_entities([YeelightNightlightModeSensor(device, config_entry)])
|
||||
|
||||
|
||||
class YeelightNightlightModeSensor(YeelightEntity, BinarySensorEntity):
|
||||
@@ -35,16 +34,6 @@ class YeelightNightlightModeSensor(YeelightEntity, BinarySensorEntity):
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> Optional[str]:
|
||||
"""Return a unique ID."""
|
||||
unique = self._device.unique_id
|
||||
|
||||
if unique:
|
||||
return unique + "-nightlight_sensor"
|
||||
|
||||
return None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
|
||||
@@ -18,6 +18,7 @@ from . import (
|
||||
CONF_SAVE_ON_CHANGE,
|
||||
CONF_TRANSITION,
|
||||
NIGHTLIGHT_SWITCH_TYPE_LIGHT,
|
||||
_async_unique_name,
|
||||
)
|
||||
from . import DOMAIN # pylint:disable=unused-import
|
||||
|
||||
@@ -38,7 +39,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the config flow."""
|
||||
self._capabilities = None
|
||||
self._discovered_devices = {}
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
@@ -49,7 +49,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
try:
|
||||
await self._async_try_connect(user_input[CONF_HOST])
|
||||
return self.async_create_entry(
|
||||
title=self._async_default_name(),
|
||||
title=user_input[CONF_HOST],
|
||||
data=user_input,
|
||||
)
|
||||
except CannotConnect:
|
||||
@@ -59,9 +59,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
else:
|
||||
return await self.async_step_pick_device()
|
||||
|
||||
user_input = user_input or {}
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({vol.Optional(CONF_HOST): str}),
|
||||
data_schema=vol.Schema(
|
||||
{vol.Optional(CONF_HOST, default=user_input.get(CONF_HOST, "")): str}
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
@@ -69,9 +72,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle the step to pick discovered device."""
|
||||
if user_input is not None:
|
||||
unique_id = user_input[CONF_DEVICE]
|
||||
self._capabilities = self._discovered_devices[unique_id]
|
||||
capabilities = self._discovered_devices[unique_id]
|
||||
await self.async_set_unique_id(unique_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
return self.async_create_entry(
|
||||
title=self._async_default_name(),
|
||||
title=_async_unique_name(capabilities),
|
||||
data={CONF_ID: unique_id},
|
||||
)
|
||||
|
||||
@@ -122,25 +127,32 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def _async_try_connect(self, host):
|
||||
"""Set up with options."""
|
||||
for entry in self._async_current_entries():
|
||||
if entry.data.get(CONF_HOST) == host:
|
||||
raise AlreadyConfigured
|
||||
|
||||
bulb = yeelight.Bulb(host)
|
||||
try:
|
||||
capabilities = await self.hass.async_add_executor_job(bulb.get_capabilities)
|
||||
if capabilities is None: # timeout
|
||||
_LOGGER.error("Failed to get capabilities from %s: timeout", host)
|
||||
raise CannotConnect
|
||||
_LOGGER.debug("Failed to get capabilities from %s: timeout", host)
|
||||
else:
|
||||
_LOGGER.debug("Get capabilities: %s", capabilities)
|
||||
await self.async_set_unique_id(capabilities["id"])
|
||||
self._abort_if_unique_id_configured()
|
||||
return
|
||||
except OSError as err:
|
||||
_LOGGER.error("Failed to get capabilities from %s: %s", host, err)
|
||||
raise CannotConnect from err
|
||||
_LOGGER.debug("Get capabilities: %s", capabilities)
|
||||
self._capabilities = capabilities
|
||||
await self.async_set_unique_id(capabilities["id"])
|
||||
self._abort_if_unique_id_configured()
|
||||
_LOGGER.debug("Failed to get capabilities from %s: %s", host, err)
|
||||
# Ignore the error since get_capabilities uses UDP discovery packet
|
||||
# which does not work in all network environments
|
||||
|
||||
@callback
|
||||
def _async_default_name(self):
|
||||
model = self._capabilities["model"]
|
||||
unique_id = self._capabilities["id"]
|
||||
return f"yeelight_{model}_{unique_id}"
|
||||
# Fallback to get properties
|
||||
try:
|
||||
await self.hass.async_add_executor_job(bulb.get_properties)
|
||||
except yeelight.BulbException as err:
|
||||
_LOGGER.error("Failed to get properties from %s: %s", host, err)
|
||||
raise CannotConnect from err
|
||||
_LOGGER.debug("Get properties: %s", bulb.last_properties)
|
||||
|
||||
|
||||
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
@@ -153,11 +165,8 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
async def async_step_init(self, user_input=None):
|
||||
"""Handle the initial step."""
|
||||
if user_input is not None:
|
||||
# keep the name from imported entries
|
||||
options = {
|
||||
CONF_NAME: self._config_entry.options.get(CONF_NAME),
|
||||
**user_input,
|
||||
}
|
||||
options = {**self._config_entry.options}
|
||||
options.update(user_input)
|
||||
return self.async_create_entry(title="", data=options)
|
||||
|
||||
options = self._config_entry.options
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Light platform support for yeelight."""
|
||||
from functools import partial
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import voluptuous as vol
|
||||
import yeelight
|
||||
@@ -241,7 +240,7 @@ async def async_setup_entry(
|
||||
device_type = device.type
|
||||
|
||||
def _lights_setup_helper(klass):
|
||||
lights.append(klass(device, custom_effects=custom_effects))
|
||||
lights.append(klass(device, config_entry, custom_effects=custom_effects))
|
||||
|
||||
if device_type == BulbType.White:
|
||||
_lights_setup_helper(YeelightGenericLight)
|
||||
@@ -382,9 +381,9 @@ def _async_setup_services(hass: HomeAssistant):
|
||||
class YeelightGenericLight(YeelightEntity, LightEntity):
|
||||
"""Representation of a Yeelight generic light."""
|
||||
|
||||
def __init__(self, device, custom_effects=None):
|
||||
def __init__(self, device, entry, custom_effects=None):
|
||||
"""Initialize the Yeelight light."""
|
||||
super().__init__(device)
|
||||
super().__init__(device, entry)
|
||||
|
||||
self.config = device.config
|
||||
|
||||
@@ -418,12 +417,6 @@ class YeelightGenericLight(YeelightEntity, LightEntity):
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> Optional[str]:
|
||||
"""Return a unique ID."""
|
||||
|
||||
return self.device.unique_id
|
||||
|
||||
@property
|
||||
def supported_features(self) -> int:
|
||||
"""Flag supported features."""
|
||||
@@ -852,14 +845,10 @@ class YeelightNightLightMode(YeelightGenericLight):
|
||||
"""Representation of a Yeelight when in nightlight mode."""
|
||||
|
||||
@property
|
||||
def unique_id(self) -> Optional[str]:
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
unique = super().unique_id
|
||||
|
||||
if unique:
|
||||
return unique + "-nightlight"
|
||||
|
||||
return None
|
||||
return f"{unique}-nightlight"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@@ -945,12 +934,10 @@ class YeelightAmbientLight(YeelightColorLightWithoutNightlightSwitch):
|
||||
self._light_type = LightType.Ambient
|
||||
|
||||
@property
|
||||
def unique_id(self) -> Optional[str]:
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique ID."""
|
||||
unique = super().unique_id
|
||||
|
||||
if unique:
|
||||
return unique + "-ambilight"
|
||||
return f"{unique}-ambilight"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
|
||||
@@ -36,4 +36,4 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user