From a9df341abf92952832848c6099ad3f7647ebb986 Mon Sep 17 00:00:00 2001 From: tdfountain <174762217+tdfountain@users.noreply.github.com> Date: Sat, 22 Mar 2025 14:11:48 -0700 Subject: [PATCH] Optimize entity creation by storing device name as data in NUT (#141147) --- homeassistant/components/nut/__init__.py | 5 ++++- homeassistant/components/nut/entity.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/nut/__init__.py b/homeassistant/components/nut/__init__.py index 169dbbbff5d..94a2599501a 100644 --- a/homeassistant/components/nut/__init__.py +++ b/homeassistant/components/nut/__init__.py @@ -240,6 +240,7 @@ class PyNUTData: self._client = AIONUTClient(self._host, port, username, password, 5, persistent) self.ups_list: dict[str, str] | None = None + self.device_name: str | None = None self._status: dict[str, str] | None = None self._device_info: NUTDeviceInfo | None = None @@ -250,7 +251,7 @@ class PyNUTData: @property def name(self) -> str: - """Return the name of the ups.""" + """Return the name of the NUT device.""" return self._alias or f"Nut-{self._host}" @property @@ -294,6 +295,8 @@ class PyNUTData: self._status = await self._async_get_status() if self._device_info is None: self._device_info = self._get_device_info() + if self.device_name is None: + self.device_name = self.name.title() return self._status async def async_run_command(self, command_name: str) -> None: diff --git a/homeassistant/components/nut/entity.py b/homeassistant/components/nut/entity.py index 8179526acf3..5445b51c5cb 100644 --- a/homeassistant/components/nut/entity.py +++ b/homeassistant/components/nut/entity.py @@ -42,10 +42,10 @@ class NUTBaseEntity(CoordinatorEntity[DataUpdateCoordinator]): """Initialize the entity.""" super().__init__(coordinator) - device_name = data.name.title() + self.pynut_data = data self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, unique_id)}, - name=device_name, + name=self.pynut_data.device_name, ) self._attr_device_info.update(_get_nut_device_info(data))