Simplify fitbit unit system and conversions (#100825)

* Simplify fitbit unit conversions

* Use enum values in unit system schema

* Use fitbit unit system enums
This commit is contained in:
Allen Porter 2023-09-25 17:08:59 -07:00 committed by GitHub
parent 785618909a
commit 18f29993c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 143 additions and 100 deletions

View file

@ -6,7 +6,9 @@ from typing import Any
from fitbit import Fitbit
from homeassistant.core import HomeAssistant
from homeassistant.util.unit_system import METRIC_SYSTEM
from .const import FitbitUnitSystem
from .model import FitbitDevice, FitbitProfile
_LOGGER = logging.getLogger(__name__)
@ -19,11 +21,13 @@ class FitbitApi:
self,
hass: HomeAssistant,
client: Fitbit,
unit_system: FitbitUnitSystem | None = None,
) -> None:
"""Initialize Fitbit auth."""
self._hass = hass
self._profile: FitbitProfile | None = None
self._client = client
self._unit_system = unit_system
@property
def client(self) -> Fitbit:
@ -32,14 +36,38 @@ class FitbitApi:
def get_user_profile(self) -> FitbitProfile:
"""Return the user profile from the API."""
response: dict[str, Any] = self._client.user_profile_get()
_LOGGER.debug("user_profile_get=%s", response)
profile = response["user"]
return FitbitProfile(
encoded_id=profile["encodedId"],
full_name=profile["fullName"],
locale=profile.get("locale"),
)
if self._profile is None:
response: dict[str, Any] = self._client.user_profile_get()
_LOGGER.debug("user_profile_get=%s", response)
profile = response["user"]
self._profile = FitbitProfile(
encoded_id=profile["encodedId"],
full_name=profile["fullName"],
locale=profile.get("locale"),
)
return self._profile
def get_unit_system(self) -> FitbitUnitSystem:
"""Get the unit system to use when fetching timeseries.
This is used in a couple ways. The first is to determine the request
header to use when talking to the fitbit API which changes the
units returned by the API. The second is to tell Home Assistant the
units set in sensor values for the values returned by the API.
"""
if (
self._unit_system is not None
and self._unit_system != FitbitUnitSystem.LEGACY_DEFAULT
):
return self._unit_system
# Use units consistent with the account user profile or fallback to the
# home assistant unit settings.
profile = self.get_user_profile()
if profile.locale == FitbitUnitSystem.EN_GB:
return FitbitUnitSystem.EN_GB
if self._hass.config.units is METRIC_SYSTEM:
return FitbitUnitSystem.METRIC
return FitbitUnitSystem.EN_US
def get_devices(self) -> list[FitbitDevice]:
"""Return available devices."""
@ -58,6 +86,10 @@ class FitbitApi:
def get_latest_time_series(self, resource_type: str) -> dict[str, Any]:
"""Return the most recent value from the time series for the specified resource type."""
# Set request header based on the configured unit system
self._client.system = self.get_unit_system()
response: dict[str, Any] = self._client.time_series(resource_type, period="7d")
_LOGGER.debug("time_series(%s)=%s", resource_type, response)
key = resource_type.replace("/", "-")