diff --git a/homeassistant/components/yeelight/__init__.py b/homeassistant/components/yeelight/__init__.py index d315c3b5860..aeffe4c6ea5 100644 --- a/homeassistant/components/yeelight/__init__.py +++ b/homeassistant/components/yeelight/__init__.py @@ -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): diff --git a/homeassistant/components/yeelight/config_flow.py b/homeassistant/components/yeelight/config_flow.py index 651d41ff268..268a0e9cea2 100644 --- a/homeassistant/components/yeelight/config_flow.py +++ b/homeassistant/components/yeelight/config_flow.py @@ -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 diff --git a/homeassistant/components/yeelight/strings.json b/homeassistant/components/yeelight/strings.json index 807fae1ca64..a0ce26550c8 100644 --- a/homeassistant/components/yeelight/strings.json +++ b/homeassistant/components/yeelight/strings.json @@ -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.", diff --git a/homeassistant/components/yeelight/translations/en.json b/homeassistant/components/yeelight/translations/en.json index 06431e7bc2b..3ed5bbe5515 100644 --- a/homeassistant/components/yeelight/translations/en.json +++ b/homeassistant/components/yeelight/translations/en.json @@ -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})?" diff --git a/tests/components/yeelight/test_config_flow.py b/tests/components/yeelight/test_config_flow.py index 17902a08bfa..9fb86c8921a 100644 --- a/tests/components/yeelight/test_config_flow.py +++ b/tests/components/yeelight/test_config_flow.py @@ -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