Add unique ID to fitbit (#79587)

* Set unique ID for fitbit sensors, including the user ID

* Remove fitbit_ from unique ids (see: https://developers.home-assistant.io/docs/entity_registry_index/#unique-id)

* change fitbit user_profile type to dict[str, Any]

* Fitbit: define a default unique ID, and add battery info if present

* No need for trailing _battery in unique ID, since it already contains "devices/battery_"
This commit is contained in:
Nathan Broadbent 2022-10-05 01:28:00 +13:00 committed by GitHub
parent 9d2ba7c008
commit 1907b87666
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -195,8 +195,9 @@ def setup_platform(
if int(time.time()) - expires_at > 3600:
authd_client.client.refresh_token()
user_profile = authd_client.user_profile_get()["user"]
if (unit_system := config[CONF_UNIT_SYSTEM]) == "default":
authd_client.system = authd_client.user_profile_get()["user"]["locale"]
authd_client.system = user_profile["locale"]
if authd_client.system != "en_GB":
if hass.config.units.is_metric:
authd_client.system = "metric"
@ -211,6 +212,7 @@ def setup_platform(
entities = [
FitbitSensor(
authd_client,
user_profile,
config_path,
description,
hass.config.units.is_metric,
@ -224,6 +226,7 @@ def setup_platform(
[
FitbitSensor(
authd_client,
user_profile,
config_path,
FITBIT_RESOURCE_BATTERY,
hass.config.units.is_metric,
@ -345,6 +348,7 @@ class FitbitSensor(SensorEntity):
def __init__(
self,
client: Fitbit,
user_profile: dict[str, Any],
config_path: str,
description: FitbitSensorEntityDescription,
is_metric: bool,
@ -358,8 +362,12 @@ class FitbitSensor(SensorEntity):
self.is_metric = is_metric
self.clock_format = clock_format
self.extra = extra
self._attr_unique_id = f"{user_profile['encodedId']}_{description.key}"
if self.extra is not None:
self._attr_name = f"{self.extra.get('deviceVersion')} Battery"
self._attr_unique_id = f"{self._attr_unique_id}_{self.extra.get('id')}"
if (unit_type := description.unit_type) == "":
split_resource = description.key.rsplit("/", maxsplit=1)[-1]
try: