From 9c8d8fef16dbffeaa8913c74f4c96e11161e7ad0 Mon Sep 17 00:00:00 2001 From: tdfountain <174762217+tdfountain@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:06:45 -0800 Subject: [PATCH] Suggest area for NUT based on device location (#129770) --- homeassistant/components/nut/__init__.py | 5 ++++- tests/components/nut/test_init.py | 27 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/nut/__init__.py b/homeassistant/components/nut/__init__.py index 6bbe19e8f3c..b4e53c1380c 100644 --- a/homeassistant/components/nut/__init__.py +++ b/homeassistant/components/nut/__init__.py @@ -132,6 +132,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: NutConfigEntry) -> bool: model=data.device_info.model, sw_version=data.device_info.firmware, serial_number=data.device_info.serial, + suggested_area=data.device_info.device_location, ) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -211,6 +212,7 @@ class NUTDeviceInfo: model: str | None = None firmware: str | None = None serial: str | None = None + device_location: str | None = None class PyNUTData: @@ -271,7 +273,8 @@ class PyNUTData: model = _model_from_status(self._status) firmware = _firmware_from_status(self._status) serial = _serial_from_status(self._status) - return NUTDeviceInfo(manufacturer, model, firmware, serial) + device_location: str | None = self._status.get("device.location") + return NUTDeviceInfo(manufacturer, model, firmware, serial, device_location) async def _async_get_status(self) -> dict[str, str]: """Get the ups status from NUT.""" diff --git a/tests/components/nut/test_init.py b/tests/components/nut/test_init.py index cd56c209a36..d5d85daa336 100644 --- a/tests/components/nut/test_init.py +++ b/tests/components/nut/test_init.py @@ -120,3 +120,30 @@ async def test_serial_number(hass: HomeAssistant) -> None: assert device_entry is not None assert device_entry.serial_number == mock_serial_number + + +async def test_device_location(hass: HomeAssistant) -> None: + """Test for suggested location on device.""" + mock_serial_number = "A00000000000" + mock_device_location = "XYZ Location" + await async_init_integration( + hass, + username="someuser", + password="somepassword", + list_vars={ + "ups.serial": mock_serial_number, + "device.location": mock_device_location, + }, + list_ups={"ups1": "UPS 1"}, + list_commands_return_value=[], + ) + + device_registry = dr.async_get(hass) + assert device_registry is not None + + device_entry = device_registry.async_get_device( + identifiers={(DOMAIN, mock_serial_number)} + ) + + assert device_entry is not None + assert device_entry.suggested_area == mock_device_location