diff --git a/homeassistant/components/lifx/config_flow.py b/homeassistant/components/lifx/config_flow.py index fa83ff5c427..56a88c89806 100644 --- a/homeassistant/components/lifx/config_flow.py +++ b/homeassistant/components/lifx/config_flow.py @@ -119,9 +119,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): assert self._discovered_device is not None discovered = self._discovered_device _LOGGER.debug( - "Confirming discovery: %s with serial %s", + "Confirming discovery of %s (%s) [%s]", discovered.label, - self.unique_id, + discovered.group, + discovered.mac_addr, ) if user_input is not None or self._async_discovered_pending_migration(): return self._async_create_entry_from_device(discovered) @@ -130,8 +131,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._set_confirm_only() placeholders = { "label": discovered.label, - "host": discovered.ip_addr, - "serial": self.unique_id, + "group": discovered.group, } self.context["title_placeholders"] = placeholders return self.async_show_form( @@ -224,11 +224,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # get_hostfirmware required for MAC address offset # get_version required for lifx_features() # get_label required to log the name of the device + # get_group required to populate suggested areas messages = await asyncio.gather( *[ async_execute_lifx(device.get_hostfirmware), async_execute_lifx(device.get_version), async_execute_lifx(device.get_label), + async_execute_lifx(device.get_group), ] ) except asyncio.TimeoutError: @@ -237,7 +239,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): connection.async_stop() if ( messages is None - or len(messages) != 3 + or len(messages) != 4 or lifx_features(device)["relays"] is True or device.host_firmware_version is None ): diff --git a/homeassistant/components/lifx/coordinator.py b/homeassistant/components/lifx/coordinator.py index 9343c3b7dad..1f01f9859c7 100644 --- a/homeassistant/components/lifx/coordinator.py +++ b/homeassistant/components/lifx/coordinator.py @@ -173,6 +173,8 @@ class LIFXUpdateCoordinator(DataUpdateCoordinator): self.device.get_hostfirmware() if self.device.product is None: self.device.get_version() + if self.device.group is None: + self.device.get_group() response = await async_execute_lifx(self.device.get_color) diff --git a/homeassistant/components/lifx/entity.py b/homeassistant/components/lifx/entity.py index a500e353fbf..63996d60027 100644 --- a/homeassistant/components/lifx/entity.py +++ b/homeassistant/components/lifx/entity.py @@ -25,6 +25,7 @@ class LIFXEntity(CoordinatorEntity[LIFXUpdateCoordinator]): name=coordinator.label, model=products.product_map.get(self.bulb.product, "LIFX Bulb"), sw_version=self.bulb.host_firmware_version, + suggested_area=self.bulb.group, ) @@ -42,4 +43,5 @@ class LIFXSensorEntity(CoordinatorEntity[LIFXSensorUpdateCoordinator]): name=coordinator.parent.label, model=products.product_map.get(self.bulb.product, "LIFX Bulb"), sw_version=self.bulb.host_firmware_version, + suggested_area=self.bulb.group, ) diff --git a/homeassistant/components/lifx/strings.json b/homeassistant/components/lifx/strings.json index b83ae9c1609..45de5ebe3cf 100644 --- a/homeassistant/components/lifx/strings.json +++ b/homeassistant/components/lifx/strings.json @@ -1,6 +1,6 @@ { "config": { - "flow_title": "{label} ({host}) {serial}", + "flow_title": "{label} ({group})", "step": { "user": { "description": "If you leave the host empty, discovery will be used to find devices.", @@ -14,7 +14,7 @@ } }, "discovery_confirm": { - "description": "Do you want to setup {label} ({host}) {serial}?" + "description": "Do you want to setup {label} ({group})?" } }, "error": { diff --git a/homeassistant/components/lifx/translations/en.json b/homeassistant/components/lifx/translations/en.json index 119259457a7..96f97f2e91e 100644 --- a/homeassistant/components/lifx/translations/en.json +++ b/homeassistant/components/lifx/translations/en.json @@ -8,10 +8,10 @@ "error": { "cannot_connect": "Failed to connect" }, - "flow_title": "{label} ({host}) {serial}", + "flow_title": "{label} ({group})", "step": { "discovery_confirm": { - "description": "Do you want to setup {label} ({host}) {serial}?" + "description": "Do you want to setup {label} ({group})?" }, "pick_device": { "data": { diff --git a/tests/components/lifx/__init__.py b/tests/components/lifx/__init__.py index bb861c4a683..0278b8ec3a7 100644 --- a/tests/components/lifx/__init__.py +++ b/tests/components/lifx/__init__.py @@ -14,6 +14,7 @@ MODULE = "homeassistant.components.lifx" MODULE_CONFIG_FLOW = "homeassistant.components.lifx.config_flow" IP_ADDRESS = "127.0.0.1" LABEL = "My Bulb" +GROUP = "My Group" SERIAL = "aa:bb:cc:dd:ee:cc" MAC_ADDRESS = "aa:bb:cc:dd:ee:cd" DEFAULT_ENTRY_TITLE = LABEL @@ -81,6 +82,7 @@ def _mocked_bulb() -> Light: bulb = Light(asyncio.get_running_loop(), SERIAL, IP_ADDRESS) bulb.host_firmware_version = "3.00" bulb.label = LABEL + bulb.group = GROUP bulb.color = [1, 2, 3, 4] bulb.power_level = 0 bulb.fire_and_forget = AsyncMock() @@ -88,6 +90,7 @@ def _mocked_bulb() -> Light: bulb.try_sending = AsyncMock() bulb.set_infrared = MockLifxCommand(bulb) bulb.get_label = MockLifxCommand(bulb) + bulb.get_group = MockLifxCommand(bulb) bulb.get_color = MockLifxCommand(bulb) bulb.set_power = MockLifxCommand(bulb) bulb.set_color = MockLifxCommand(bulb) diff --git a/tests/components/lifx/test_config_flow.py b/tests/components/lifx/test_config_flow.py index c9f069d8660..d82209e61b6 100644 --- a/tests/components/lifx/test_config_flow.py +++ b/tests/components/lifx/test_config_flow.py @@ -11,6 +11,8 @@ from homeassistant.components.lifx.const import CONF_SERIAL from homeassistant.const import CONF_DEVICE, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.setup import async_setup_component from . import ( DEFAULT_ENTRY_TITLE, @@ -19,9 +21,11 @@ from . import ( MAC_ADDRESS, MODULE, SERIAL, + _mocked_bulb, _mocked_failing_bulb, _mocked_relay, _patch_config_flow_try_connect, + _patch_device, _patch_discovery, ) @@ -523,3 +527,41 @@ async def test_refuse_relays(hass: HomeAssistant): await hass.async_block_till_done() assert result2["type"] == "form" assert result2["errors"] == {"base": "cannot_connect"} + + +async def test_suggested_area(hass: HomeAssistant) -> None: + """Test suggested area is populated from lifx group label.""" + + class MockLifxCommandGetGroup: + """Mock the get_group method that gets the group name from the bulb.""" + + def __init__(self, bulb, **kwargs): + """Init command.""" + self.bulb = bulb + self.lifx_group = kwargs.get("lifx_group") + + def __call__(self, *args, **kwargs): + """Call command.""" + self.bulb.group = self.lifx_group + + config_entry = MockConfigEntry( + domain=DOMAIN, data={CONF_HOST: "1.2.3.4"}, unique_id=SERIAL + ) + config_entry.add_to_hass(hass) + bulb = _mocked_bulb() + bulb.group = None + bulb.get_group = MockLifxCommandGetGroup(bulb, lifx_group="My LIFX Group") + + with _patch_discovery(device=bulb), _patch_config_flow_try_connect( + device=bulb + ), _patch_device(device=bulb): + await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + entity_id = "light.my_bulb" + entity = entity_registry.async_get(entity_id) + + device_registry = dr.async_get(hass) + device = device_registry.async_get(entity.device_id) + assert device.suggested_area == "My LIFX Group"