From 5cc594682f216250adb635f478886c2058bef1be Mon Sep 17 00:00:00 2001 From: Arto Jantunen Date: Mon, 15 Nov 2021 18:28:19 +0200 Subject: [PATCH] Add unique id's to Vallox entities (#58459) * Add unique id's to Vallox entities * Cache uuid properties Requested in code review. Caching None isn't a problem as the underlying implementation of get_uuid in the vallox_websocket_api library can never return None. * Simplify get_uuid type check Based on review comments. * Set _attr_unique_id in init * Import the library get_uuid under a different name There are a few options here: 1. Rename the get_uuid method with a synonym 2. Import get_uuid under a different name 3. Convert get_uuid into a property 4. Rename get_uuid in the Vallox library None of these options is that appealing. I'll start with option two, anyways. --- homeassistant/components/vallox/__init__.py | 9 +++++++++ homeassistant/components/vallox/fan.py | 2 ++ homeassistant/components/vallox/sensor.py | 3 +++ 3 files changed, 14 insertions(+) diff --git a/homeassistant/components/vallox/__init__.py b/homeassistant/components/vallox/__init__.py index 63b594a5bf2..73dc633834e 100644 --- a/homeassistant/components/vallox/__init__.py +++ b/homeassistant/components/vallox/__init__.py @@ -5,9 +5,11 @@ from dataclasses import dataclass, field import ipaddress import logging from typing import Any, NamedTuple +from uuid import UUID from vallox_websocket_api import PROFILE as VALLOX_PROFILE, Vallox from vallox_websocket_api.exceptions import ValloxApiException +from vallox_websocket_api.vallox import get_uuid as calculate_uuid import voluptuous as vol from homeassistant.const import CONF_HOST, CONF_NAME, EVENT_HOMEASSISTANT_STARTED @@ -114,6 +116,13 @@ class ValloxState: return value + def get_uuid(self) -> UUID | None: + """Return cached UUID value.""" + uuid = calculate_uuid(self.metric_cache) + if not isinstance(uuid, UUID): + raise ValueError + return uuid + class ValloxDataUpdateCoordinator(DataUpdateCoordinator): """The DataUpdateCoordinator for Vallox.""" diff --git a/homeassistant/components/vallox/fan.py b/homeassistant/components/vallox/fan.py index 4d621615aef..6de30302838 100644 --- a/homeassistant/components/vallox/fan.py +++ b/homeassistant/components/vallox/fan.py @@ -99,6 +99,8 @@ class ValloxFan(CoordinatorEntity, FanEntity): self._attr_name = name + self._attr_unique_id = str(self.coordinator.data.get_uuid()) + @property def supported_features(self) -> int: """Flag supported features.""" diff --git a/homeassistant/components/vallox/sensor.py b/homeassistant/components/vallox/sensor.py index 0b96316b766..17bcf0e4499 100644 --- a/homeassistant/components/vallox/sensor.py +++ b/homeassistant/components/vallox/sensor.py @@ -52,6 +52,9 @@ class ValloxSensor(CoordinatorEntity, SensorEntity): self._attr_name = f"{name} {description.name}" + uuid = self.coordinator.data.get_uuid() + self._attr_unique_id = f"{uuid}-{description.key}" + @property def native_value(self) -> StateType: """Return the value reported by the sensor."""