diff --git a/homeassistant/components/lamarzocco/entity.py b/homeassistant/components/lamarzocco/entity.py index f0942f51ace..5542906d887 100644 --- a/homeassistant/components/lamarzocco/entity.py +++ b/homeassistant/components/lamarzocco/entity.py @@ -6,8 +6,12 @@ from dataclasses import dataclass from pylamarzocco.const import FirmwareType from pylamarzocco.lm_machine import LaMarzoccoMachine -from homeassistant.const import CONF_ADDRESS -from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo +from homeassistant.const import CONF_ADDRESS, CONF_MAC +from homeassistant.helpers.device_registry import ( + CONNECTION_BLUETOOTH, + CONNECTION_NETWORK_MAC, + DeviceInfo, +) from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -48,17 +52,17 @@ class LaMarzoccoBaseEntity( serial_number=device.serial_number, sw_version=device.firmware[FirmwareType.MACHINE].current_version, ) + connections: set[tuple[str, str]] = set() if coordinator.config_entry.data.get(CONF_ADDRESS): - self._attr_device_info.update( - DeviceInfo( - connections={ - ( - CONNECTION_NETWORK_MAC, - coordinator.config_entry.data[CONF_ADDRESS], - ) - } - ) + connections.add( + (CONNECTION_NETWORK_MAC, coordinator.config_entry.data[CONF_ADDRESS]) ) + if coordinator.config_entry.data.get(CONF_MAC): + connections.add( + (CONNECTION_BLUETOOTH, coordinator.config_entry.data[CONF_MAC]) + ) + if connections: + self._attr_device_info.update(DeviceInfo(connections=connections)) class LaMarzoccoEntity(LaMarzoccoBaseEntity): diff --git a/tests/components/lamarzocco/snapshots/test_init.ambr b/tests/components/lamarzocco/snapshots/test_init.ambr new file mode 100644 index 00000000000..519a9301bfd --- /dev/null +++ b/tests/components/lamarzocco/snapshots/test_init.ambr @@ -0,0 +1,41 @@ +# serializer version: 1 +# name: test_device + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + tuple( + 'bluetooth', + 'aa:bb:cc:dd:ee:ff', + ), + tuple( + 'mac', + '00:00:00:00:00:00', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'lamarzocco', + 'GS012345', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'La Marzocco', + 'model': , + 'model_id': , + 'name': 'GS012345', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': 'GS012345', + 'suggested_area': None, + 'sw_version': '1.40', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/lamarzocco/snapshots/test_switch.ambr b/tests/components/lamarzocco/snapshots/test_switch.ambr index 084b54b3f3a..79a305c998f 100644 --- a/tests/components/lamarzocco/snapshots/test_switch.ambr +++ b/tests/components/lamarzocco/snapshots/test_switch.ambr @@ -91,42 +91,6 @@ 'state': 'on', }) # --- -# name: test_device - DeviceRegistryEntrySnapshot({ - 'area_id': None, - 'config_entries': , - 'configuration_url': None, - 'connections': set({ - tuple( - 'mac', - '00:00:00:00:00:00', - ), - }), - 'disabled_by': None, - 'entry_type': None, - 'hw_version': None, - 'id': , - 'identifiers': set({ - tuple( - 'lamarzocco', - 'GS012345', - ), - }), - 'is_new': False, - 'labels': set({ - }), - 'manufacturer': 'La Marzocco', - 'model': , - 'model_id': , - 'name': 'GS012345', - 'name_by_user': None, - 'primary_config_entry': , - 'serial_number': 'GS012345', - 'suggested_area': None, - 'sw_version': '1.40', - 'via_device_id': None, - }) -# --- # name: test_switches[-set_power-kwargs0] StateSnapshot({ 'attributes': ReadOnlyDict({ diff --git a/tests/components/lamarzocco/test_config_flow.py b/tests/components/lamarzocco/test_config_flow.py index b206b7b68a3..e25aab39012 100644 --- a/tests/components/lamarzocco/test_config_flow.py +++ b/tests/components/lamarzocco/test_config_flow.py @@ -381,6 +381,26 @@ async def test_bluetooth_discovery( } +async def test_bluetooth_discovery_already_configured( + hass: HomeAssistant, + mock_lamarzocco: MagicMock, + mock_cloud_client: MagicMock, + mock_setup_entry: Generator[AsyncMock], + mock_config_entry: MockConfigEntry, +) -> None: + """Test bluetooth discovery.""" + mock_config_entry.add_to_hass(hass) + + service_info = get_bluetooth_service_info( + mock_lamarzocco.model, mock_lamarzocco.serial_number + ) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_BLUETOOTH}, data=service_info + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + async def test_bluetooth_discovery_errors( hass: HomeAssistant, mock_lamarzocco: MagicMock, diff --git a/tests/components/lamarzocco/test_init.py b/tests/components/lamarzocco/test_init.py index 75c3019afb4..cb6b028bda0 100644 --- a/tests/components/lamarzocco/test_init.py +++ b/tests/components/lamarzocco/test_init.py @@ -5,6 +5,7 @@ from unittest.mock import AsyncMock, MagicMock, patch from pylamarzocco.const import FirmwareType from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful import pytest +from syrupy import SnapshotAssertion from websockets.protocol import State from homeassistant.components.lamarzocco.config_flow import CONF_MACHINE @@ -19,7 +20,11 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_STOP, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import issue_registry as ir +from homeassistant.helpers import ( + device_registry as dr, + entity_registry as er, + issue_registry as ir, +) from . import USER_INPUT, async_init_integration, get_bluetooth_service_info @@ -220,3 +225,32 @@ async def test_gateway_version_issue( issue_registry = ir.async_get(hass) issue = issue_registry.async_get_issue(DOMAIN, "unsupported_gateway_firmware") assert (issue is not None) == issue_exists + + +async def test_device( + hass: HomeAssistant, + mock_lamarzocco: MagicMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the device.""" + + await async_init_integration(hass, mock_config_entry) + + hass.config_entries.async_update_entry( + mock_config_entry, + data={**mock_config_entry.data, CONF_MAC: "aa:bb:cc:dd:ee:ff"}, + ) + + state = hass.states.get(f"switch.{mock_lamarzocco.serial_number}") + assert state + + entry = entity_registry.async_get(state.entity_id) + assert entry + assert entry.device_id + + device = device_registry.async_get(entry.device_id) + assert device + assert device == snapshot diff --git a/tests/components/lamarzocco/test_switch.py b/tests/components/lamarzocco/test_switch.py index 5c6d1cb1e42..9082e6f4c09 100644 --- a/tests/components/lamarzocco/test_switch.py +++ b/tests/components/lamarzocco/test_switch.py @@ -15,7 +15,7 @@ from homeassistant.components.switch import ( from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import entity_registry as er from . import WAKE_UP_SLEEP_ENTRY_IDS, async_init_integration @@ -88,30 +88,6 @@ async def test_switches( control_fn.assert_called_with(enabled=True, **kwargs) -async def test_device( - hass: HomeAssistant, - mock_lamarzocco: MagicMock, - mock_config_entry: MockConfigEntry, - device_registry: dr.DeviceRegistry, - entity_registry: er.EntityRegistry, - snapshot: SnapshotAssertion, -) -> None: - """Test the device for one switch.""" - - await async_init_integration(hass, mock_config_entry) - - state = hass.states.get(f"switch.{mock_lamarzocco.serial_number}") - assert state - - entry = entity_registry.async_get(state.entity_id) - assert entry - assert entry.device_id - - device = device_registry.async_get(entry.device_id) - assert device - assert device == snapshot - - async def test_auto_on_off_switches( hass: HomeAssistant, mock_lamarzocco: MagicMock,