From d64ef2ba73a3322e62e592252b38af1040c6f187 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 15 Feb 2022 15:44:35 -0600 Subject: [PATCH] Deduplicate flux_led title and CONF_NAME (#66598) --- homeassistant/components/flux_led/button.py | 2 +- homeassistant/components/flux_led/config_flow.py | 7 ++----- homeassistant/components/flux_led/discovery.py | 9 ++++++--- homeassistant/components/flux_led/entity.py | 2 +- homeassistant/components/flux_led/light.py | 2 +- homeassistant/components/flux_led/number.py | 2 +- homeassistant/components/flux_led/select.py | 2 +- homeassistant/components/flux_led/sensor.py | 2 +- homeassistant/components/flux_led/switch.py | 4 ++-- tests/components/flux_led/test_config_flow.py | 14 +++----------- tests/components/flux_led/test_init.py | 3 +-- 11 files changed, 20 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/flux_led/button.py b/homeassistant/components/flux_led/button.py index fcd4ecc3adc..bfbe63cf02e 100644 --- a/homeassistant/components/flux_led/button.py +++ b/homeassistant/components/flux_led/button.py @@ -63,7 +63,7 @@ class FluxButton(FluxBaseEntity, ButtonEntity): """Initialize the button.""" self.entity_description = description super().__init__(device, entry) - self._attr_name = f"{entry.data[CONF_NAME]} {description.name}" + self._attr_name = f"{entry.data.get(CONF_NAME, entry.title)} {description.name}" base_unique_id = entry.unique_id or entry.entry_id self._attr_unique_id = f"{base_unique_id}_{description.key}" diff --git a/homeassistant/components/flux_led/config_flow.py b/homeassistant/components/flux_led/config_flow.py index c6f14e929d6..9b1cda2dea1 100644 --- a/homeassistant/components/flux_led/config_flow.py +++ b/homeassistant/components/flux_led/config_flow.py @@ -17,7 +17,7 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.components import dhcp -from homeassistant.const import CONF_HOST, CONF_NAME +from homeassistant.const import CONF_HOST from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import device_registry as dr @@ -145,10 +145,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Create a config entry from a device.""" self._async_abort_entries_match({CONF_HOST: device[ATTR_IPADDR]}) name = async_name_from_discovery(device) - data: dict[str, Any] = { - CONF_HOST: device[ATTR_IPADDR], - CONF_NAME: name, - } + data: dict[str, Any] = {CONF_HOST: device[ATTR_IPADDR]} async_populate_data_from_discovery(data, data, device) return self.async_create_entry( title=name, diff --git a/homeassistant/components/flux_led/discovery.py b/homeassistant/components/flux_led/discovery.py index 0f65c7c1797..c30418fa8e4 100644 --- a/homeassistant/components/flux_led/discovery.py +++ b/homeassistant/components/flux_led/discovery.py @@ -125,10 +125,13 @@ def async_update_entry_from_discovery( if model_num and entry.data.get(CONF_MODEL_NUM) != model_num: data_updates[CONF_MODEL_NUM] = model_num async_populate_data_from_discovery(entry.data, data_updates, device) - if not entry.data.get(CONF_NAME) or is_ip_address(entry.data[CONF_NAME]): - updates["title"] = data_updates[CONF_NAME] = async_name_from_discovery(device) - if data_updates: + if is_ip_address(entry.title): + updates["title"] = async_name_from_discovery(device) + title_matches_name = entry.title == entry.data.get(CONF_NAME) + if data_updates or title_matches_name: updates["data"] = {**entry.data, **data_updates} + if title_matches_name: + del updates["data"][CONF_NAME] if updates: return hass.config_entries.async_update_entry(entry, **updates) return False diff --git a/homeassistant/components/flux_led/entity.py b/homeassistant/components/flux_led/entity.py index 5946ab817de..da92931d1e6 100644 --- a/homeassistant/components/flux_led/entity.py +++ b/homeassistant/components/flux_led/entity.py @@ -40,7 +40,7 @@ def _async_device_info( ATTR_IDENTIFIERS: {(DOMAIN, entry.entry_id)}, ATTR_MANUFACTURER: "Zengge", ATTR_MODEL: device.model, - ATTR_NAME: entry.data[CONF_NAME], + ATTR_NAME: entry.data.get(CONF_NAME, entry.title), ATTR_SW_VERSION: sw_version_str, } if hw_model := entry.data.get(CONF_MODEL): diff --git a/homeassistant/components/flux_led/light.py b/homeassistant/components/flux_led/light.py index 4534c45e228..0d179cd2b77 100644 --- a/homeassistant/components/flux_led/light.py +++ b/homeassistant/components/flux_led/light.py @@ -178,7 +178,7 @@ async def async_setup_entry( FluxLight( coordinator, entry.unique_id or entry.entry_id, - entry.data[CONF_NAME], + entry.data.get(CONF_NAME, entry.title), list(custom_effect_colors), options.get(CONF_CUSTOM_EFFECT_SPEED_PCT, DEFAULT_EFFECT_SPEED), options.get(CONF_CUSTOM_EFFECT_TRANSITION, TRANSITION_GRADUAL), diff --git a/homeassistant/components/flux_led/number.py b/homeassistant/components/flux_led/number.py index d7fad9cf0e6..b4e6e87a829 100644 --- a/homeassistant/components/flux_led/number.py +++ b/homeassistant/components/flux_led/number.py @@ -50,7 +50,7 @@ async def async_setup_entry( | FluxMusicPixelsPerSegmentNumber | FluxMusicSegmentsNumber ] = [] - name = entry.data[CONF_NAME] + name = entry.data.get(CONF_NAME, entry.title) base_unique_id = entry.unique_id or entry.entry_id if device.pixels_per_segment is not None: diff --git a/homeassistant/components/flux_led/select.py b/homeassistant/components/flux_led/select.py index 3b78baa782b..7edf0ef50f8 100644 --- a/homeassistant/components/flux_led/select.py +++ b/homeassistant/components/flux_led/select.py @@ -53,7 +53,7 @@ async def async_setup_entry( | FluxRemoteConfigSelect | FluxWhiteChannelSelect ] = [] - name = entry.data[CONF_NAME] + name = entry.data.get(CONF_NAME, entry.title) base_unique_id = entry.unique_id or entry.entry_id if device.device_type == DeviceType.Switch: diff --git a/homeassistant/components/flux_led/sensor.py b/homeassistant/components/flux_led/sensor.py index 18d1aac5506..a4266e55fa8 100644 --- a/homeassistant/components/flux_led/sensor.py +++ b/homeassistant/components/flux_led/sensor.py @@ -26,7 +26,7 @@ async def async_setup_entry( FluxPairedRemotes( coordinator, entry.unique_id or entry.entry_id, - f"{entry.data[CONF_NAME]} Paired Remotes", + f"{entry.data.get(CONF_NAME, entry.title)} Paired Remotes", "paired_remotes", ) ] diff --git a/homeassistant/components/flux_led/switch.py b/homeassistant/components/flux_led/switch.py index ee004fc2250..e8c34f12b11 100644 --- a/homeassistant/components/flux_led/switch.py +++ b/homeassistant/components/flux_led/switch.py @@ -35,7 +35,7 @@ async def async_setup_entry( coordinator: FluxLedUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] entities: list[FluxSwitch | FluxRemoteAccessSwitch | FluxMusicSwitch] = [] base_unique_id = entry.unique_id or entry.entry_id - name = entry.data[CONF_NAME] + name = entry.data.get(CONF_NAME, entry.title) if coordinator.device.device_type == DeviceType.Switch: entities.append(FluxSwitch(coordinator, base_unique_id, name, None)) @@ -73,7 +73,7 @@ class FluxRemoteAccessSwitch(FluxBaseEntity, SwitchEntity): ) -> None: """Initialize the light.""" super().__init__(device, entry) - self._attr_name = f"{entry.data[CONF_NAME]} Remote Access" + self._attr_name = f"{entry.data.get(CONF_NAME, entry.title)} Remote Access" base_unique_id = entry.unique_id or entry.entry_id self._attr_unique_id = f"{base_unique_id}_remote_access" diff --git a/tests/components/flux_led/test_config_flow.py b/tests/components/flux_led/test_config_flow.py index dcab5cc01ad..0ff8180a761 100644 --- a/tests/components/flux_led/test_config_flow.py +++ b/tests/components/flux_led/test_config_flow.py @@ -23,7 +23,7 @@ from homeassistant.components.flux_led.const import ( TRANSITION_JUMP, TRANSITION_STROBE, ) -from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_NAME +from homeassistant.const import CONF_DEVICE, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import RESULT_TYPE_ABORT, RESULT_TYPE_FORM @@ -94,7 +94,6 @@ async def test_discovery(hass: HomeAssistant): assert result3["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -170,7 +169,6 @@ async def test_discovery_legacy(hass: HomeAssistant): assert result3["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -253,7 +251,6 @@ async def test_discovery_with_existing_device_present(hass: HomeAssistant): assert result3["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -330,7 +327,6 @@ async def test_manual_working_discovery(hass: HomeAssistant): assert result4["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -377,7 +373,6 @@ async def test_manual_no_discovery_data(hass: HomeAssistant): CONF_HOST: IP_ADDRESS, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, - CONF_NAME: IP_ADDRESS, } @@ -445,7 +440,6 @@ async def test_discovered_by_discovery(hass): assert result2["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -483,7 +477,6 @@ async def test_discovered_by_dhcp_udp_responds(hass): assert result2["data"] == { CONF_MINOR_VERSION: 4, CONF_HOST: IP_ADDRESS, - CONF_NAME: DEFAULT_ENTRY_TITLE, CONF_MODEL: MODEL, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_INFO: MODEL, @@ -522,7 +515,6 @@ async def test_discovered_by_dhcp_no_udp_response(hass): CONF_HOST: IP_ADDRESS, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, - CONF_NAME: DEFAULT_ENTRY_TITLE, } assert mock_async_setup.called assert mock_async_setup_entry.called @@ -553,7 +545,6 @@ async def test_discovered_by_dhcp_partial_udp_response_fallback_tcp(hass): CONF_HOST: IP_ADDRESS, CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, - CONF_NAME: DEFAULT_ENTRY_TITLE, } assert mock_async_setup.called assert mock_async_setup_entry.called @@ -630,7 +621,8 @@ async def test_options(hass: HomeAssistant): """Test options flow.""" config_entry = MockConfigEntry( domain=DOMAIN, - data={CONF_HOST: IP_ADDRESS, CONF_NAME: DEFAULT_ENTRY_TITLE}, + data={CONF_HOST: IP_ADDRESS}, + title=IP_ADDRESS, options={ CONF_CUSTOM_EFFECT_COLORS: "[255,0,0], [0,0,255]", CONF_CUSTOM_EFFECT_SPEED_PCT: 30, diff --git a/tests/components/flux_led/test_init.py b/tests/components/flux_led/test_init.py index de655c2e6ad..489f6c932c2 100644 --- a/tests/components/flux_led/test_init.py +++ b/tests/components/flux_led/test_init.py @@ -117,7 +117,7 @@ async def test_config_entry_fills_unique_id_with_directed_discovery( ) -> None: """Test that the unique id is added if its missing via directed (not broadcast) discovery.""" config_entry = MockConfigEntry( - domain=DOMAIN, data={CONF_HOST: IP_ADDRESS}, unique_id=None + domain=DOMAIN, data={CONF_HOST: IP_ADDRESS}, unique_id=None, title=IP_ADDRESS ) config_entry.add_to_hass(hass) last_address = None @@ -144,7 +144,6 @@ async def test_config_entry_fills_unique_id_with_directed_discovery( assert config_entry.state == ConfigEntryState.LOADED assert config_entry.unique_id == MAC_ADDRESS - assert config_entry.data[CONF_NAME] == title assert config_entry.title == title