This commit is contained in:
ray0711
2023-04-16 10:02:46 +02:00
6 changed files with 171 additions and 87 deletions

View File

@@ -5,8 +5,15 @@ import logging
import voluptuous as vol
from numbers import Number
import datetime
import pytz
from tzlocal import get_localzone
from homeassistant.components.sensor import SensorEntity, SensorStateClass
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
@@ -14,6 +21,7 @@ from homeassistant.const import (
DEVICE_CLASS_TIMESTAMP,
LENGTH_KILOMETERS,
)
from homeassistant.const import ATTR_ATTRIBUTION, CONF_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import DeviceInfo
@@ -47,16 +55,17 @@ async def async_setup_entry(
entities = []
for (
sensor_type,
(name, unit, icon, device_class, enabled_by_default),
(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",
"Registering entity: %s, %s, %s, %s, %s, %s, %s",
sensor_type,
name,
unit,
icon,
device_class,
state_class,
enabled_by_default,
)
entities.append(
@@ -68,6 +77,7 @@ async def async_setup_entry(
unit,
icon,
device_class,
state_class,
enabled_by_default,
)
)
@@ -114,6 +124,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
unit,
icon,
device_class,
state_class,
enabled_default: bool = True,
):
"""Initialize a Garmin Connect sensor."""
@@ -122,6 +133,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
self._unique_id = unique_id
self._type = sensor_type
self._device_class = device_class
self._state_class = state_class
self._enabled_default = enabled_default
self._attr_name = name
@@ -129,7 +141,7 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
self._attr_icon = icon
self._attr_native_unit_of_measurement = unit
self._attr_unique_id = f"{self._unique_id}_{self._type}"
self._attr_state_class = SensorStateClass.TOTAL
self._attr_state_class = state_class
@property
def native_value(self):
@@ -147,12 +159,22 @@ class GarminConnectSensor(CoordinatorEntity, SensorEntity):
self.coordinator.data[self._type]
)
if active_alarms:
return active_alarms[0]
date_time_obj = datetime.datetime.strptime(active_alarms[0], "%Y-%m-%dT%H:%M:%S")
tz = get_localzone()
timezone = pytz.timezone(tz.zone)
timezone_date_time_obj = timezone.localize(date_time_obj)
return timezone_date_time_obj
else:
return None
elif self._type == "stressQualifier":
return value
if self._device_class == DEVICE_CLASS_TIMESTAMP:
return value
if self._device_class == SensorDeviceClass.TIMESTAMP:
date_time_obj = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
tz = get_localzone()
timezone = pytz.timezone(tz.zone)
timezone_date_time_obj = timezone.localize(date_time_obj)
return timezone_date_time_obj
return round(value, 2) if isinstance(value, Number) else value