mirror of
https://github.com/cyberjunky/home-assistant-garmin_connect.git
synced 2026-01-08 20:38:00 -05:00
Merge pull request #120 from ronlut/main
Add user- and integration name to sensor names and ids
This commit is contained in:
@@ -9,6 +9,7 @@ import datetime
|
|||||||
from tzlocal import get_localzone
|
from tzlocal import get_localzone
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
|
ENTITY_ID_FORMAT,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
@@ -19,10 +20,11 @@ from homeassistant.const import (
|
|||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_ID
|
|
||||||
|
from homeassistant.const import CONF_ID, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo, generate_entity_id
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
@@ -48,14 +50,14 @@ async def async_setup_entry(
|
|||||||
coordinator: DataUpdateCoordinator = hass.data[GARMIN_DOMAIN][entry.entry_id][
|
coordinator: DataUpdateCoordinator = hass.data[GARMIN_DOMAIN][entry.entry_id][
|
||||||
DATA_COORDINATOR
|
DATA_COORDINATOR
|
||||||
]
|
]
|
||||||
unique_id = entry.data[CONF_ID]
|
device_id = entry.data[CONF_ID]
|
||||||
|
user_identifier = entry.data[CONF_USERNAME].split("@")[0]
|
||||||
|
|
||||||
entities = []
|
entities = []
|
||||||
for (
|
for (
|
||||||
sensor_type,
|
sensor_type,
|
||||||
(name, unit, icon, device_class, state_class, enabled_by_default),
|
(name, unit, icon, device_class, state_class, enabled_by_default),
|
||||||
) in GARMIN_ENTITY_LIST.items():
|
) in GARMIN_ENTITY_LIST.items():
|
||||||
|
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
|
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
|
||||||
sensor_type,
|
sensor_type,
|
||||||
@@ -69,7 +71,8 @@ async def async_setup_entry(
|
|||||||
entities.append(
|
entities.append(
|
||||||
GarminConnectSensor(
|
GarminConnectSensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
unique_id,
|
device_id,
|
||||||
|
user_identifier,
|
||||||
sensor_type,
|
sensor_type,
|
||||||
name,
|
name,
|
||||||
unit,
|
unit,
|
||||||
@@ -84,7 +87,8 @@ async def async_setup_entry(
|
|||||||
entities.append(
|
entities.append(
|
||||||
GarminConnectGearSensor(
|
GarminConnectGearSensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
unique_id,
|
device_id,
|
||||||
|
user_identifier,
|
||||||
gear_item[GEAR.UUID],
|
gear_item[GEAR.UUID],
|
||||||
gear_item["gearTypeName"],
|
gear_item["gearTypeName"],
|
||||||
gear_item["displayName"],
|
gear_item["displayName"],
|
||||||
@@ -137,9 +141,10 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator,
|
coordinator,
|
||||||
unique_id,
|
device_id,
|
||||||
|
user_identifier,
|
||||||
sensor_type,
|
sensor_type,
|
||||||
name,
|
sensor_name,
|
||||||
unit,
|
unit,
|
||||||
icon,
|
icon,
|
||||||
device_class,
|
device_class,
|
||||||
@@ -149,17 +154,22 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Initialize a Garmin Connect sensor."""
|
"""Initialize a Garmin Connect sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._unique_id = unique_id
|
self._device_id = device_id
|
||||||
self._type = sensor_type
|
self._type = sensor_type
|
||||||
self._device_class = device_class
|
self._device_class = device_class
|
||||||
self._state_class = state_class
|
self._state_class = state_class
|
||||||
self._enabled_default = enabled_default
|
self._enabled_default = enabled_default
|
||||||
|
|
||||||
self._attr_name = name
|
self._attr_name = f"{user_identifier} {sensor_name}"
|
||||||
|
self.entity_id = generate_entity_id(
|
||||||
|
ENTITY_ID_FORMAT,
|
||||||
|
f"{GARMIN_DOMAIN} {self._attr_name}",
|
||||||
|
hass=coordinator.hass,
|
||||||
|
)
|
||||||
self._attr_device_class = self._device_class
|
self._attr_device_class = self._device_class
|
||||||
self._attr_icon = icon
|
self._attr_icon = icon
|
||||||
self._attr_native_unit_of_measurement = unit
|
self._attr_native_unit_of_measurement = unit
|
||||||
self._attr_unique_id = f"{self._unique_id}_{self._type}"
|
self._attr_unique_id = f"{GARMIN_DOMAIN}_{self._device_id}_{self._type}"
|
||||||
self._attr_state_class = state_class
|
self._attr_state_class = state_class
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -223,7 +233,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
|
|||||||
def device_info(self) -> DeviceInfo:
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return device information."""
|
"""Return device information."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(GARMIN_DOMAIN, self._unique_id)},
|
"identifiers": {(GARMIN_DOMAIN, self._device_id)},
|
||||||
"name": "Garmin Connect",
|
"name": "Garmin Connect",
|
||||||
"manufacturer": "Garmin Connect",
|
"manufacturer": "Garmin Connect",
|
||||||
}
|
}
|
||||||
@@ -249,27 +259,28 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator,
|
coordinator,
|
||||||
unique_id,
|
device_id,
|
||||||
|
user_identifier,
|
||||||
uuid,
|
uuid,
|
||||||
sensor_type,
|
sensor_type,
|
||||||
name,
|
sensor_name,
|
||||||
device_class: None,
|
device_class: None,
|
||||||
enabled_default: bool = True,
|
enabled_default: bool = True,
|
||||||
):
|
):
|
||||||
"""Initialize a Garmin Connect sensor."""
|
"""Initialize a Garmin Connect sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._unique_id = unique_id
|
self._device_id = device_id
|
||||||
self._type = sensor_type
|
self._type = sensor_type
|
||||||
self._uuid = uuid
|
self._uuid = uuid
|
||||||
self._device_class = device_class
|
self._device_class = device_class
|
||||||
self._enabled_default = enabled_default
|
self._enabled_default = enabled_default
|
||||||
|
|
||||||
self._attr_name = name
|
self._attr_name = f"{user_identifier} {sensor_name}"
|
||||||
self._attr_device_class = self._device_class
|
self._attr_device_class = self._device_class
|
||||||
self._attr_icon = GEAR_ICONS[sensor_type]
|
self._attr_icon = GEAR_ICONS[sensor_type]
|
||||||
self._attr_native_unit_of_measurement = UnitOfLength.KILOMETERS
|
self._attr_native_unit_of_measurement = UnitOfLength.KILOMETERS
|
||||||
self._attr_unique_id = f"{self._unique_id}_{self._uuid}"
|
self._attr_unique_id = f"{GARMIN_DOMAIN}_{self._device_id}_{self._uuid}"
|
||||||
self._attr_state_class = SensorStateClass.TOTAL
|
self._attr_state_class = SensorStateClass.TOTAL
|
||||||
self._attr_device_class = "garmin_gear"
|
self._attr_device_class = "garmin_gear"
|
||||||
|
|
||||||
@@ -337,7 +348,7 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
|
|||||||
def device_info(self) -> DeviceInfo:
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return device information."""
|
"""Return device information."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(GARMIN_DOMAIN, self._unique_id)},
|
"identifiers": {(GARMIN_DOMAIN, self._device_id)},
|
||||||
"name": "Garmin Connect",
|
"name": "Garmin Connect",
|
||||||
"manufacturer": "Garmin Connect",
|
"manufacturer": "Garmin Connect",
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user