1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

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.
This commit is contained in:
Arto Jantunen 2021-11-15 18:28:19 +02:00 committed by GitHub
parent a4208c0926
commit 5cc594682f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -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."""

View File

@ -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."""

View File

@ -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."""