Normalize the display name of yeelight devices (#54883)
Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
parent
71b8409c0d
commit
3cb7227040
5 changed files with 35 additions and 10 deletions
|
@ -316,12 +316,29 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
@callback
|
||||
def async_format_model(model: str) -> str:
|
||||
"""Generate a more human readable model."""
|
||||
return model.replace("_", " ").title()
|
||||
|
||||
|
||||
@callback
|
||||
def async_format_id(id_: str) -> str:
|
||||
"""Generate a more human readable id."""
|
||||
return hex(int(id_, 16)) if id_ else "None"
|
||||
|
||||
|
||||
@callback
|
||||
def async_format_model_id(model: str, id_: str) -> str:
|
||||
"""Generate a more human readable name."""
|
||||
return f"{async_format_model(model)} {async_format_id(id_)}"
|
||||
|
||||
|
||||
@callback
|
||||
def _async_unique_name(capabilities: dict) -> str:
|
||||
"""Generate name from capabilities."""
|
||||
model = str(capabilities["model"]).replace("_", " ").title()
|
||||
short_id = hex(int(capabilities["id"], 16))
|
||||
return f"Yeelight {model} {short_id}"
|
||||
model_id = async_format_model_id(capabilities["model"], capabilities["id"])
|
||||
return f"Yeelight {model_id}"
|
||||
|
||||
|
||||
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||
|
|
|
@ -23,6 +23,9 @@ from . import (
|
|||
NIGHTLIGHT_SWITCH_TYPE_LIGHT,
|
||||
YeelightScanner,
|
||||
_async_unique_name,
|
||||
async_format_id,
|
||||
async_format_model,
|
||||
async_format_model_id,
|
||||
)
|
||||
|
||||
MODEL_UNKNOWN = "unknown"
|
||||
|
@ -92,12 +95,16 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""Confirm discovery."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title=f"{self._discovered_model} {self.unique_id}",
|
||||
title=async_format_model_id(self._discovered_model, self.unique_id),
|
||||
data={CONF_ID: self.unique_id, CONF_HOST: self._discovered_ip},
|
||||
)
|
||||
|
||||
self._set_confirm_only()
|
||||
placeholders = {"model": self._discovered_model, "host": self._discovered_ip}
|
||||
placeholders = {
|
||||
"id": async_format_id(self.unique_id),
|
||||
"model": async_format_model(self._discovered_model),
|
||||
"host": self._discovered_ip,
|
||||
}
|
||||
self.context["title_placeholders"] = placeholders
|
||||
return self.async_show_form(
|
||||
step_id="discovery_confirm", description_placeholders=placeholders
|
||||
|
@ -118,7 +125,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
else:
|
||||
self._abort_if_unique_id_configured()
|
||||
return self.async_create_entry(
|
||||
title=f"{model} {self.unique_id}",
|
||||
title=async_format_model_id(model, self.unique_id),
|
||||
data={
|
||||
CONF_HOST: user_input[CONF_HOST],
|
||||
CONF_ID: self.unique_id,
|
||||
|
@ -162,7 +169,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
continue # ignore configured devices
|
||||
model = capabilities["model"]
|
||||
host = urlparse(capabilities["location"]).hostname
|
||||
name = f"{host} {model} {unique_id}"
|
||||
model_id = async_format_model_id(model, unique_id)
|
||||
name = f"{model_id} ({host})"
|
||||
self._discovered_devices[unique_id] = capabilities
|
||||
devices_name[unique_id] = name
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"config": {
|
||||
"flow_title": "{model} {host}",
|
||||
"flow_title": "{model} {id} ({host})",
|
||||
"step": {
|
||||
"user": {
|
||||
"description": "If you leave the host empty, discovery will be used to find devices.",
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
"error": {
|
||||
"cannot_connect": "Failed to connect"
|
||||
},
|
||||
"flow_title": "{model} {host}",
|
||||
"flow_title": "{model} {id} ({host})",
|
||||
"step": {
|
||||
"discovery_confirm": {
|
||||
"description": "Do you want to setup {model} ({host})?"
|
||||
|
|
|
@ -214,7 +214,7 @@ async def test_manual(hass: HomeAssistant):
|
|||
)
|
||||
await hass.async_block_till_done()
|
||||
assert result4["type"] == "create_entry"
|
||||
assert result4["title"] == "color 0x000000000015243f"
|
||||
assert result4["title"] == "Color 0x15243f"
|
||||
assert result4["data"] == {CONF_HOST: IP_ADDRESS, CONF_ID: "0x000000000015243f"}
|
||||
|
||||
# Duplicate
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue