Deduplicate flux_led title and CONF_NAME (#66598)

This commit is contained in:
J. Nick Koston 2022-02-15 15:44:35 -06:00 committed by GitHub
parent 734fdf7bff
commit d64ef2ba73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 20 additions and 29 deletions

View File

@ -63,7 +63,7 @@ class FluxButton(FluxBaseEntity, ButtonEntity):
"""Initialize the button.""" """Initialize the button."""
self.entity_description = description self.entity_description = description
super().__init__(device, entry) 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 base_unique_id = entry.unique_id or entry.entry_id
self._attr_unique_id = f"{base_unique_id}_{description.key}" self._attr_unique_id = f"{base_unique_id}_{description.key}"

View File

@ -17,7 +17,7 @@ import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components import dhcp 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.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import device_registry as dr 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.""" """Create a config entry from a device."""
self._async_abort_entries_match({CONF_HOST: device[ATTR_IPADDR]}) self._async_abort_entries_match({CONF_HOST: device[ATTR_IPADDR]})
name = async_name_from_discovery(device) name = async_name_from_discovery(device)
data: dict[str, Any] = { data: dict[str, Any] = {CONF_HOST: device[ATTR_IPADDR]}
CONF_HOST: device[ATTR_IPADDR],
CONF_NAME: name,
}
async_populate_data_from_discovery(data, data, device) async_populate_data_from_discovery(data, data, device)
return self.async_create_entry( return self.async_create_entry(
title=name, title=name,

View File

@ -125,10 +125,13 @@ def async_update_entry_from_discovery(
if model_num and entry.data.get(CONF_MODEL_NUM) != model_num: if model_num and entry.data.get(CONF_MODEL_NUM) != model_num:
data_updates[CONF_MODEL_NUM] = model_num data_updates[CONF_MODEL_NUM] = model_num
async_populate_data_from_discovery(entry.data, data_updates, device) 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]): if is_ip_address(entry.title):
updates["title"] = data_updates[CONF_NAME] = async_name_from_discovery(device) updates["title"] = async_name_from_discovery(device)
if data_updates: title_matches_name = entry.title == entry.data.get(CONF_NAME)
if data_updates or title_matches_name:
updates["data"] = {**entry.data, **data_updates} updates["data"] = {**entry.data, **data_updates}
if title_matches_name:
del updates["data"][CONF_NAME]
if updates: if updates:
return hass.config_entries.async_update_entry(entry, **updates) return hass.config_entries.async_update_entry(entry, **updates)
return False return False

View File

@ -40,7 +40,7 @@ def _async_device_info(
ATTR_IDENTIFIERS: {(DOMAIN, entry.entry_id)}, ATTR_IDENTIFIERS: {(DOMAIN, entry.entry_id)},
ATTR_MANUFACTURER: "Zengge", ATTR_MANUFACTURER: "Zengge",
ATTR_MODEL: device.model, 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, ATTR_SW_VERSION: sw_version_str,
} }
if hw_model := entry.data.get(CONF_MODEL): if hw_model := entry.data.get(CONF_MODEL):

View File

@ -178,7 +178,7 @@ async def async_setup_entry(
FluxLight( FluxLight(
coordinator, coordinator,
entry.unique_id or entry.entry_id, entry.unique_id or entry.entry_id,
entry.data[CONF_NAME], entry.data.get(CONF_NAME, entry.title),
list(custom_effect_colors), list(custom_effect_colors),
options.get(CONF_CUSTOM_EFFECT_SPEED_PCT, DEFAULT_EFFECT_SPEED), options.get(CONF_CUSTOM_EFFECT_SPEED_PCT, DEFAULT_EFFECT_SPEED),
options.get(CONF_CUSTOM_EFFECT_TRANSITION, TRANSITION_GRADUAL), options.get(CONF_CUSTOM_EFFECT_TRANSITION, TRANSITION_GRADUAL),

View File

@ -50,7 +50,7 @@ async def async_setup_entry(
| FluxMusicPixelsPerSegmentNumber | FluxMusicPixelsPerSegmentNumber
| FluxMusicSegmentsNumber | FluxMusicSegmentsNumber
] = [] ] = []
name = entry.data[CONF_NAME] name = entry.data.get(CONF_NAME, entry.title)
base_unique_id = entry.unique_id or entry.entry_id base_unique_id = entry.unique_id or entry.entry_id
if device.pixels_per_segment is not None: if device.pixels_per_segment is not None:

View File

@ -53,7 +53,7 @@ async def async_setup_entry(
| FluxRemoteConfigSelect | FluxRemoteConfigSelect
| FluxWhiteChannelSelect | FluxWhiteChannelSelect
] = [] ] = []
name = entry.data[CONF_NAME] name = entry.data.get(CONF_NAME, entry.title)
base_unique_id = entry.unique_id or entry.entry_id base_unique_id = entry.unique_id or entry.entry_id
if device.device_type == DeviceType.Switch: if device.device_type == DeviceType.Switch:

View File

@ -26,7 +26,7 @@ async def async_setup_entry(
FluxPairedRemotes( FluxPairedRemotes(
coordinator, coordinator,
entry.unique_id or entry.entry_id, 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", "paired_remotes",
) )
] ]

View File

@ -35,7 +35,7 @@ async def async_setup_entry(
coordinator: FluxLedUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: FluxLedUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
entities: list[FluxSwitch | FluxRemoteAccessSwitch | FluxMusicSwitch] = [] entities: list[FluxSwitch | FluxRemoteAccessSwitch | FluxMusicSwitch] = []
base_unique_id = entry.unique_id or entry.entry_id 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: if coordinator.device.device_type == DeviceType.Switch:
entities.append(FluxSwitch(coordinator, base_unique_id, name, None)) entities.append(FluxSwitch(coordinator, base_unique_id, name, None))
@ -73,7 +73,7 @@ class FluxRemoteAccessSwitch(FluxBaseEntity, SwitchEntity):
) -> None: ) -> None:
"""Initialize the light.""" """Initialize the light."""
super().__init__(device, entry) 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 base_unique_id = entry.unique_id or entry.entry_id
self._attr_unique_id = f"{base_unique_id}_remote_access" self._attr_unique_id = f"{base_unique_id}_remote_access"

View File

@ -23,7 +23,7 @@ from homeassistant.components.flux_led.const import (
TRANSITION_JUMP, TRANSITION_JUMP,
TRANSITION_STROBE, 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.core import HomeAssistant
from homeassistant.data_entry_flow import RESULT_TYPE_ABORT, RESULT_TYPE_FORM 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"] == { assert result3["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -170,7 +169,6 @@ async def test_discovery_legacy(hass: HomeAssistant):
assert result3["data"] == { assert result3["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -253,7 +251,6 @@ async def test_discovery_with_existing_device_present(hass: HomeAssistant):
assert result3["data"] == { assert result3["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -330,7 +327,6 @@ async def test_manual_working_discovery(hass: HomeAssistant):
assert result4["data"] == { assert result4["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -377,7 +373,6 @@ async def test_manual_no_discovery_data(hass: HomeAssistant):
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION,
CONF_NAME: IP_ADDRESS,
} }
@ -445,7 +440,6 @@ async def test_discovered_by_discovery(hass):
assert result2["data"] == { assert result2["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -483,7 +477,6 @@ async def test_discovered_by_dhcp_udp_responds(hass):
assert result2["data"] == { assert result2["data"] == {
CONF_MINOR_VERSION: 4, CONF_MINOR_VERSION: 4,
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_NAME: DEFAULT_ENTRY_TITLE,
CONF_MODEL: MODEL, CONF_MODEL: MODEL,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_INFO: MODEL, CONF_MODEL_INFO: MODEL,
@ -522,7 +515,6 @@ async def test_discovered_by_dhcp_no_udp_response(hass):
CONF_HOST: IP_ADDRESS, CONF_HOST: IP_ADDRESS,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION,
CONF_NAME: DEFAULT_ENTRY_TITLE,
} }
assert mock_async_setup.called assert mock_async_setup.called
assert mock_async_setup_entry.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_HOST: IP_ADDRESS,
CONF_MODEL_NUM: MODEL_NUM, CONF_MODEL_NUM: MODEL_NUM,
CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION, CONF_MODEL_DESCRIPTION: MODEL_DESCRIPTION,
CONF_NAME: DEFAULT_ENTRY_TITLE,
} }
assert mock_async_setup.called assert mock_async_setup.called
assert mock_async_setup_entry.called assert mock_async_setup_entry.called
@ -630,7 +621,8 @@ async def test_options(hass: HomeAssistant):
"""Test options flow.""" """Test options flow."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
data={CONF_HOST: IP_ADDRESS, CONF_NAME: DEFAULT_ENTRY_TITLE}, data={CONF_HOST: IP_ADDRESS},
title=IP_ADDRESS,
options={ options={
CONF_CUSTOM_EFFECT_COLORS: "[255,0,0], [0,0,255]", CONF_CUSTOM_EFFECT_COLORS: "[255,0,0], [0,0,255]",
CONF_CUSTOM_EFFECT_SPEED_PCT: 30, CONF_CUSTOM_EFFECT_SPEED_PCT: 30,

View File

@ -117,7 +117,7 @@ async def test_config_entry_fills_unique_id_with_directed_discovery(
) -> None: ) -> None:
"""Test that the unique id is added if its missing via directed (not broadcast) discovery.""" """Test that the unique id is added if its missing via directed (not broadcast) discovery."""
config_entry = MockConfigEntry( 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) config_entry.add_to_hass(hass)
last_address = None 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.state == ConfigEntryState.LOADED
assert config_entry.unique_id == MAC_ADDRESS assert config_entry.unique_id == MAC_ADDRESS
assert config_entry.data[CONF_NAME] == title
assert config_entry.title == title assert config_entry.title == title