Skip to content

Commit

Permalink
Merge pull request #120 from ronlut/main
Browse files Browse the repository at this point in the history
Add user- and integration name to sensor names and ids
  • Loading branch information
cyberjunky authored Jan 16, 2024
2 parents 5f31a74 + 6abe6ac commit a12306d
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions custom_components/garmin_connect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tzlocal import get_localzone

from homeassistant.components.sensor import (
ENTITY_ID_FORMAT,
SensorEntity,
SensorDeviceClass,
SensorStateClass,
Expand All @@ -19,10 +20,11 @@
ATTR_ENTITY_ID,
CONF_ID,
)
from homeassistant.const import CONF_ID

from homeassistant.const import CONF_ID, CONF_USERNAME
from homeassistant.core import HomeAssistant
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 (
CoordinatorEntity,
DataUpdateCoordinator,
Expand All @@ -48,14 +50,14 @@ async def async_setup_entry(
coordinator: DataUpdateCoordinator = hass.data[GARMIN_DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
unique_id = entry.data[CONF_ID]
device_id = entry.data[CONF_ID]
user_identifier = entry.data[CONF_USERNAME].split("@")[0]

entities = []
for (
sensor_type,
(name, unit, icon, device_class, state_class, enabled_by_default),
) in GARMIN_ENTITY_LIST.items():

_LOGGER.debug(
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
sensor_type,
Expand All @@ -69,7 +71,8 @@ async def async_setup_entry(
entities.append(
GarminConnectSensor(
coordinator,
unique_id,
device_id,
user_identifier,
sensor_type,
name,
unit,
Expand All @@ -84,7 +87,8 @@ async def async_setup_entry(
entities.append(
GarminConnectGearSensor(
coordinator,
unique_id,
device_id,
user_identifier,
gear_item[GEAR.UUID],
gear_item["gearTypeName"],
gear_item["displayName"],
Expand Down Expand Up @@ -137,9 +141,10 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
def __init__(
self,
coordinator,
unique_id,
device_id,
user_identifier,
sensor_type,
name,
sensor_name,
unit,
icon,
device_class,
Expand All @@ -149,17 +154,22 @@ def __init__(
"""Initialize a Garmin Connect sensor."""
super().__init__(coordinator)

self._unique_id = unique_id
self._device_id = device_id
self._type = sensor_type
self._device_class = device_class
self._state_class = state_class
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_icon = icon
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

@property
Expand Down Expand Up @@ -223,7 +233,7 @@ def extra_state_attributes(self):
def device_info(self) -> DeviceInfo:
"""Return device information."""
return {
"identifiers": {(GARMIN_DOMAIN, self._unique_id)},
"identifiers": {(GARMIN_DOMAIN, self._device_id)},
"name": "Garmin Connect",
"manufacturer": "Garmin Connect",
}
Expand All @@ -249,27 +259,28 @@ class GarminConnectGearSensor(CoordinatorEntity, SensorEntity):
def __init__(
self,
coordinator,
unique_id,
device_id,
user_identifier,
uuid,
sensor_type,
name,
sensor_name,
device_class: None,
enabled_default: bool = True,
):
"""Initialize a Garmin Connect sensor."""
super().__init__(coordinator)

self._unique_id = unique_id
self._device_id = device_id
self._type = sensor_type
self._uuid = uuid
self._device_class = device_class
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_icon = GEAR_ICONS[sensor_type]
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_device_class = "garmin_gear"

Expand Down Expand Up @@ -337,7 +348,7 @@ def _activity_names_for_gear_defaults(self, gear_defaults, activity_types):
def device_info(self) -> DeviceInfo:
"""Return device information."""
return {
"identifiers": {(GARMIN_DOMAIN, self._unique_id)},
"identifiers": {(GARMIN_DOMAIN, self._device_id)},
"name": "Garmin Connect",
"manufacturer": "Garmin Connect",
}
Expand Down

0 comments on commit a12306d

Please sign in to comment.