diff --git a/homeassistant/components/renault/renault_hub.py b/homeassistant/components/renault/renault_hub.py index 07770ad3769d..b7a9b40e2c93 100644 --- a/homeassistant/components/renault/renault_hub.py +++ b/homeassistant/components/renault/renault_hub.py @@ -11,7 +11,15 @@ from renault_api.renault_account import RenaultAccount from renault_api.renault_client import RenaultClient from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + ATTR_IDENTIFIERS, + ATTR_MANUFACTURER, + ATTR_MODEL, + ATTR_NAME, + ATTR_SW_VERSION, +) from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import CONF_KAMEREON_ACCOUNT_ID, DEFAULT_SCAN_INTERVAL @@ -49,11 +57,16 @@ class RenaultHub: self._account = await self._client.get_api_account(account_id) vehicles = await self._account.get_vehicles() + device_registry = dr.async_get(self._hass) if vehicles.vehicleLinks: await asyncio.gather( *( self.async_initialise_vehicle( - vehicle_link, self._account, scan_interval + vehicle_link, + self._account, + scan_interval, + config_entry, + device_registry, ) for vehicle_link in vehicles.vehicleLinks ) @@ -64,6 +77,8 @@ class RenaultHub: vehicle_link: KamereonVehiclesLink, renault_account: RenaultAccount, scan_interval: timedelta, + config_entry: ConfigEntry, + device_registry: dr.DeviceRegistry, ) -> None: """Set up proxy.""" assert vehicle_link.vin is not None @@ -76,6 +91,14 @@ class RenaultHub: scan_interval=scan_interval, ) await vehicle.async_initialise() + device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers=vehicle.device_info[ATTR_IDENTIFIERS], + manufacturer=vehicle.device_info[ATTR_MANUFACTURER], + name=vehicle.device_info[ATTR_NAME], + model=vehicle.device_info[ATTR_MODEL], + sw_version=vehicle.device_info[ATTR_SW_VERSION], + ) self._vehicles[vehicle_link.vin] = vehicle async def get_account_ids(self) -> list[str]: diff --git a/tests/components/renault/__init__.py b/tests/components/renault/__init__.py index da72da05d5de..9191851c777f 100644 --- a/tests/components/renault/__init__.py +++ b/tests/components/renault/__init__.py @@ -9,8 +9,16 @@ from renault_api.renault_account import RenaultAccount from homeassistant.components.renault.const import DOMAIN from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import ( + ATTR_IDENTIFIERS, + ATTR_MANUFACTURER, + ATTR_MODEL, + ATTR_NAME, + ATTR_SW_VERSION, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client +from homeassistant.helpers.device_registry import DeviceRegistry from .const import MOCK_CONFIG, MOCK_VEHICLES @@ -218,3 +226,17 @@ async def setup_renault_integration_vehicle_with_side_effect( await hass.async_block_till_done() return config_entry + + +def check_device_registry( + device_registry: DeviceRegistry, expected_device: dict[str, Any] +) -> None: + """Ensure that the expected_device is correctly registered.""" + assert len(device_registry.devices) == 1 + registry_entry = device_registry.async_get_device(expected_device[ATTR_IDENTIFIERS]) + assert registry_entry is not None + assert registry_entry.identifiers == expected_device[ATTR_IDENTIFIERS] + assert registry_entry.manufacturer == expected_device[ATTR_MANUFACTURER] + assert registry_entry.name == expected_device[ATTR_NAME] + assert registry_entry.model == expected_device[ATTR_MODEL] + assert registry_entry.sw_version == expected_device[ATTR_SW_VERSION] diff --git a/tests/components/renault/test_sensor.py b/tests/components/renault/test_sensor.py index 42a75012b38d..41fceccb56c2 100644 --- a/tests/components/renault/test_sensor.py +++ b/tests/components/renault/test_sensor.py @@ -9,6 +9,7 @@ from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.setup import async_setup_component from . import ( + check_device_registry, setup_renault_integration_vehicle, setup_renault_integration_vehicle_with_no_data, setup_renault_integration_vehicle_with_side_effect, @@ -30,15 +31,7 @@ async def test_sensors(hass, vehicle_type): await hass.async_block_till_done() mock_vehicle = MOCK_VEHICLES[vehicle_type] - assert len(device_registry.devices) == 1 - expected_device = mock_vehicle["expected_device"] - registry_entry = device_registry.async_get_device(expected_device["identifiers"]) - assert registry_entry is not None - assert registry_entry.identifiers == expected_device["identifiers"] - assert registry_entry.manufacturer == expected_device["manufacturer"] - assert registry_entry.name == expected_device["name"] - assert registry_entry.model == expected_device["model"] - assert registry_entry.sw_version == expected_device["sw_version"] + check_device_registry(device_registry, mock_vehicle["expected_device"]) expected_entities = mock_vehicle[SENSOR_DOMAIN] assert len(entity_registry.entities) == len(expected_entities) @@ -65,15 +58,7 @@ async def test_sensor_empty(hass, vehicle_type): await hass.async_block_till_done() mock_vehicle = MOCK_VEHICLES[vehicle_type] - assert len(device_registry.devices) == 1 - expected_device = mock_vehicle["expected_device"] - registry_entry = device_registry.async_get_device(expected_device["identifiers"]) - assert registry_entry is not None - assert registry_entry.identifiers == expected_device["identifiers"] - assert registry_entry.manufacturer == expected_device["manufacturer"] - assert registry_entry.name == expected_device["name"] - assert registry_entry.model == expected_device["model"] - assert registry_entry.sw_version == expected_device["sw_version"] + check_device_registry(device_registry, mock_vehicle["expected_device"]) expected_entities = mock_vehicle[SENSOR_DOMAIN] assert len(entity_registry.entities) == len(expected_entities) @@ -107,15 +92,7 @@ async def test_sensor_errors(hass, vehicle_type): await hass.async_block_till_done() mock_vehicle = MOCK_VEHICLES[vehicle_type] - assert len(device_registry.devices) == 1 - expected_device = mock_vehicle["expected_device"] - registry_entry = device_registry.async_get_device(expected_device["identifiers"]) - assert registry_entry is not None - assert registry_entry.identifiers == expected_device["identifiers"] - assert registry_entry.manufacturer == expected_device["manufacturer"] - assert registry_entry.name == expected_device["name"] - assert registry_entry.model == expected_device["model"] - assert registry_entry.sw_version == expected_device["sw_version"] + check_device_registry(device_registry, mock_vehicle["expected_device"]) expected_entities = mock_vehicle[SENSOR_DOMAIN] assert len(entity_registry.entities) == len(expected_entities) @@ -136,6 +113,7 @@ async def test_sensor_access_denied(hass): entity_registry = mock_registry(hass) device_registry = mock_device_registry(hass) + vehicle_type = "zoe_40" access_denied_exception = exceptions.AccessDeniedException( "err.func.403", "Access is denied for this resource", @@ -143,11 +121,13 @@ async def test_sensor_access_denied(hass): with patch("homeassistant.components.renault.PLATFORMS", [SENSOR_DOMAIN]): await setup_renault_integration_vehicle_with_side_effect( - hass, "zoe_40", access_denied_exception + hass, vehicle_type, access_denied_exception ) await hass.async_block_till_done() - assert len(device_registry.devices) == 0 + mock_vehicle = MOCK_VEHICLES[vehicle_type] + check_device_registry(device_registry, mock_vehicle["expected_device"]) + assert len(entity_registry.entities) == 0 @@ -157,6 +137,7 @@ async def test_sensor_not_supported(hass): entity_registry = mock_registry(hass) device_registry = mock_device_registry(hass) + vehicle_type = "zoe_40" not_supported_exception = exceptions.NotSupportedException( "err.tech.501", "This feature is not technically supported by this gateway", @@ -164,9 +145,11 @@ async def test_sensor_not_supported(hass): with patch("homeassistant.components.renault.PLATFORMS", [SENSOR_DOMAIN]): await setup_renault_integration_vehicle_with_side_effect( - hass, "zoe_40", not_supported_exception + hass, vehicle_type, not_supported_exception ) await hass.async_block_till_done() - assert len(device_registry.devices) == 0 + mock_vehicle = MOCK_VEHICLES[vehicle_type] + check_device_registry(device_registry, mock_vehicle["expected_device"]) + assert len(entity_registry.entities) == 0