From 4f4391bd09874ed740c28c9f5a4420285635b8fc Mon Sep 17 00:00:00 2001 From: Thomas55555 <59625598+Thomas55555@users.noreply.github.com> Date: Tue, 12 Mar 2024 08:25:35 +0100 Subject: [PATCH] Add missing device info to Husqvarna Automower (#113090) * Add missing DeviceInfo to Husqvarna Automower * add a test * Adress review * Update homeassistant/components/husqvarna_automower/entity.py * fix url --------- Co-authored-by: Joost Lekkerkerker --- .../components/husqvarna_automower/const.py | 1 - .../husqvarna_automower/coordinator.py | 2 ++ .../components/husqvarna_automower/entity.py | 9 +++++- .../snapshots/test_init.ambr | 31 +++++++++++++++++++ .../husqvarna_automower/test_init.py | 21 +++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/components/husqvarna_automower/snapshots/test_init.ambr diff --git a/homeassistant/components/husqvarna_automower/const.py b/homeassistant/components/husqvarna_automower/const.py index ab30bae45f2..5e38b354957 100644 --- a/homeassistant/components/husqvarna_automower/const.py +++ b/homeassistant/components/husqvarna_automower/const.py @@ -2,6 +2,5 @@ DOMAIN = "husqvarna_automower" NAME = "Husqvarna Automower" -HUSQVARNA_URL = "https://developer.husqvarnagroup.cloud/login" OAUTH2_AUTHORIZE = "https://api.authentication.husqvarnagroup.dev/v1/oauth2/authorize" OAUTH2_TOKEN = "https://api.authentication.husqvarnagroup.dev/v1/oauth2/token" diff --git a/homeassistant/components/husqvarna_automower/coordinator.py b/homeassistant/components/husqvarna_automower/coordinator.py index 2188725ed76..eb2c06262cf 100644 --- a/homeassistant/components/husqvarna_automower/coordinator.py +++ b/homeassistant/components/husqvarna_automower/coordinator.py @@ -21,6 +21,8 @@ MAX_WS_RECONNECT_TIME = 600 class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[dict[str, MowerAttributes]]): """Class to manage fetching Husqvarna data.""" + config_entry: ConfigEntry + def __init__( self, hass: HomeAssistant, api: AutomowerSession, entry: ConfigEntry ) -> None: diff --git a/homeassistant/components/husqvarna_automower/entity.py b/homeassistant/components/husqvarna_automower/entity.py index 2edce942f0c..c33229a5f64 100644 --- a/homeassistant/components/husqvarna_automower/entity.py +++ b/homeassistant/components/husqvarna_automower/entity.py @@ -3,6 +3,7 @@ import logging from aioautomower.model import MowerAttributes +from aioautomower.utils import structure_token from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -12,6 +13,8 @@ from .const import DOMAIN _LOGGER = logging.getLogger(__name__) +HUSQVARNA_URL = "https://developer.husqvarnagroup.cloud" + class AutomowerBaseEntity(CoordinatorEntity[AutomowerDataUpdateCoordinator]): """Defining the Automower base Entity.""" @@ -26,11 +29,15 @@ class AutomowerBaseEntity(CoordinatorEntity[AutomowerDataUpdateCoordinator]): """Initialize AutomowerEntity.""" super().__init__(coordinator) self.mower_id = mower_id + entry = coordinator.config_entry + structured_token = structure_token(entry.data["token"]["access_token"]) self._attr_device_info = DeviceInfo( + configuration_url=f"{HUSQVARNA_URL}/applications/{structured_token.client_id}", identifiers={(DOMAIN, mower_id)}, - name=self.mower_attributes.system.name, manufacturer="Husqvarna", model=self.mower_attributes.system.model, + name=self.mower_attributes.system.name, + serial_number=self.mower_attributes.system.serial_number, suggested_area="Garden", ) diff --git a/tests/components/husqvarna_automower/snapshots/test_init.ambr b/tests/components/husqvarna_automower/snapshots/test_init.ambr new file mode 100644 index 00000000000..63ca15461e1 --- /dev/null +++ b/tests/components/husqvarna_automower/snapshots/test_init.ambr @@ -0,0 +1,31 @@ +# serializer version: 1 +# name: test_device_info + DeviceRegistryEntrySnapshot({ + 'area_id': 'garden', + 'config_entries': , + 'configuration_url': 'https://developer.husqvarnagroup.cloud/applications/433e5fdf-5129-452c-xxxx-fadce3213042', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'husqvarna_automower', + 'c7233734-b219-4287-a173-08e3643f89f0', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Husqvarna', + 'model': '450XH-TEST', + 'name': 'Test Mower 1', + 'name_by_user': None, + 'serial_number': 123, + 'suggested_area': 'Garden', + 'sw_version': None, + 'via_device_id': None, + }) +# --- diff --git a/tests/components/husqvarna_automower/test_init.py b/tests/components/husqvarna_automower/test_init.py index 5398178e339..3c97a3b2668 100644 --- a/tests/components/husqvarna_automower/test_init.py +++ b/tests/components/husqvarna_automower/test_init.py @@ -8,12 +8,15 @@ from unittest.mock import AsyncMock from aioautomower.exceptions import ApiException, HusqvarnaWSServerHandshakeError from freezegun.api import FrozenDateTimeFactory import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.husqvarna_automower.const import DOMAIN, OAUTH2_TOKEN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr from . import setup_integration +from .const import TEST_MOWER_ID from tests.common import MockConfigEntry, async_fire_time_changed from tests.test_util.aiohttp import AiohttpClientMocker @@ -109,3 +112,21 @@ async def test_websocket_not_available( assert mock_automower_client.auth.websocket_connect.call_count == 2 assert mock_automower_client.start_listening.call_count == 2 assert mock_config_entry.state == ConfigEntryState.LOADED + + +async def test_device_info( + hass: HomeAssistant, + mock_automower_client: AsyncMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test select platform.""" + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + reg_device = device_registry.async_get_device( + identifiers={(DOMAIN, TEST_MOWER_ID)}, + ) + assert reg_device == snapshot