From 356e09091d7e885ee44320ae791ddfe2bce0c242 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:50:34 +0200 Subject: [PATCH] Add model_id to rainforest_raven device info (#128652) * Remove single-use rainforest properties * Add model_id --- .../rainforest_raven/coordinator.py | 50 ++++--------------- .../rainforest_raven/snapshots/test_init.ambr | 39 +++++++++++++++ .../rainforest_raven/test_coordinator.py | 26 ---------- .../components/rainforest_raven/test_init.py | 38 +++++++++++++- 4 files changed, 86 insertions(+), 67 deletions(-) create mode 100644 tests/components/rainforest_raven/snapshots/test_init.ambr diff --git a/homeassistant/components/rainforest_raven/coordinator.py b/homeassistant/components/rainforest_raven/coordinator.py index cab3b1199ac..31df922a168 100644 --- a/homeassistant/components/rainforest_raven/coordinator.py +++ b/homeassistant/components/rainforest_raven/coordinator.py @@ -81,20 +81,6 @@ class RAVEnDataCoordinator(DataUpdateCoordinator): update_interval=timedelta(seconds=30), ) - @property - def device_fw_version(self) -> str | None: - """Return the firmware version of the device.""" - if self._device_info: - return self._device_info.fw_version - return None - - @property - def device_hw_version(self) -> str | None: - """Return the hardware version of the device.""" - if self._device_info: - return self._device_info.hw_version - return None - @property def device_mac_address(self) -> str | None: """Return the MAC address of the device.""" @@ -102,36 +88,20 @@ class RAVEnDataCoordinator(DataUpdateCoordinator): return self._device_info.device_mac_id.hex() return None - @property - def device_manufacturer(self) -> str | None: - """Return the manufacturer of the device.""" - if self._device_info: - return self._device_info.manufacturer - return None - - @property - def device_model(self) -> str | None: - """Return the model of the device.""" - if self._device_info: - return self._device_info.model_id - return None - - @property - def device_name(self) -> str: - """Return the product name of the device.""" - return "RAVEn Device" - @property def device_info(self) -> DeviceInfo | None: """Return device info.""" - if self._device_info and self.device_mac_address: + if (device_info := self._device_info) and ( + mac_address := self.device_mac_address + ): return DeviceInfo( - identifiers={(DOMAIN, self.device_mac_address)}, - manufacturer=self.device_manufacturer, - model=self.device_model, - name=self.device_name, - sw_version=self.device_fw_version, - hw_version=self.device_hw_version, + identifiers={(DOMAIN, mac_address)}, + manufacturer=device_info.manufacturer, + model=device_info.model_id, + model_id=device_info.model_id, + name="RAVEn Device", + sw_version=device_info.fw_version, + hw_version=device_info.hw_version, ) return None diff --git a/tests/components/rainforest_raven/snapshots/test_init.ambr b/tests/components/rainforest_raven/snapshots/test_init.ambr new file mode 100644 index 00000000000..768bbc729d4 --- /dev/null +++ b/tests/components/rainforest_raven/snapshots/test_init.ambr @@ -0,0 +1,39 @@ +# serializer version: 1 +# name: test_device_registry[None-0] + list([ + ]) +# --- +# name: test_device_registry[device_info0-1] + list([ + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '2.7.3', + 'id': , + 'identifiers': set({ + tuple( + 'rainforest_raven', + 'abcdef0123456789', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Rainforest Automation, Inc.', + 'model': 'Z105-2-EMU2-LEDD_JM', + 'model_id': 'Z105-2-EMU2-LEDD_JM', + 'name': 'RAVEn Device', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': None, + 'sw_version': '2.0.0 (7400)', + 'via_device_id': None, + }), + ]) +# --- diff --git a/tests/components/rainforest_raven/test_coordinator.py b/tests/components/rainforest_raven/test_coordinator.py index 5c61c3d8ad4..dc29e95bbb5 100644 --- a/tests/components/rainforest_raven/test_coordinator.py +++ b/tests/components/rainforest_raven/test_coordinator.py @@ -15,32 +15,6 @@ from homeassistant.exceptions import ConfigEntryNotReady from . import create_mock_entry -@pytest.mark.usefixtures("mock_device") -async def test_coordinator_device_info(hass: HomeAssistant) -> None: - """Test reporting device information from the coordinator.""" - entry = create_mock_entry() - entry._async_set_state(hass, ConfigEntryState.SETUP_IN_PROGRESS, None) - coordinator = RAVEnDataCoordinator(hass, entry) - - assert coordinator.device_fw_version is None - assert coordinator.device_hw_version is None - assert coordinator.device_info is None - assert coordinator.device_mac_address is None - assert coordinator.device_manufacturer is None - assert coordinator.device_model is None - assert coordinator.device_name == "RAVEn Device" - - await coordinator.async_config_entry_first_refresh() - - assert coordinator.device_fw_version == "2.0.0 (7400)" - assert coordinator.device_hw_version == "2.7.3" - assert coordinator.device_info - assert coordinator.device_mac_address - assert coordinator.device_manufacturer == "Rainforest Automation, Inc." - assert coordinator.device_model == "Z105-2-EMU2-LEDD_JM" - assert coordinator.device_name == "RAVEn Device" - - async def test_coordinator_cache_device( hass: HomeAssistant, mock_device: AsyncMock ) -> None: diff --git a/tests/components/rainforest_raven/test_init.py b/tests/components/rainforest_raven/test_init.py index 974c45150a6..a2237096fb6 100644 --- a/tests/components/rainforest_raven/test_init.py +++ b/tests/components/rainforest_raven/test_init.py @@ -1,8 +1,18 @@ """Tests for the Rainforest RAVEn component initialisation.""" +from unittest.mock import AsyncMock + +from aioraven.data import DeviceInfo as RAVenDeviceInfo +import pytest +from syrupy.assertion import SnapshotAssertion + from homeassistant.components.rainforest_raven.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from . import create_mock_entry +from .const import DEVICE_INFO from tests.common import MockConfigEntry @@ -18,4 +28,30 @@ async def test_load_unload_entry( await hass.async_block_till_done() assert mock_entry.state is ConfigEntryState.NOT_LOADED - assert not hass.data.get(DOMAIN) + + +@pytest.mark.parametrize( + ("device_info", "device_count"), + [(DEVICE_INFO, 1), (None, 0)], +) +async def test_device_registry( + hass: HomeAssistant, + mock_device: AsyncMock, + device_registry: dr.DeviceRegistry, + snapshot: SnapshotAssertion, + device_info: RAVenDeviceInfo | None, + device_count: int, +) -> None: + """Test device registry, including if get_device_info returns None.""" + mock_device.get_device_info.return_value = device_info + entry = create_mock_entry() + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + + assert entry.state is ConfigEntryState.LOADED + + assert len(hass.states.async_all()) == 5 + + entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id) + assert len(entries) == device_count + assert entries == snapshot