Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions homeassistant/components/fitbit/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from homeassistant.helpers.json import save_json
from homeassistant.helpers.network import NoURLAvailableError, get_url
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.json import load_json
from homeassistant.util.json import load_json_object
from homeassistant.util.unit_system import METRIC_SYSTEM

from .const import (
Expand Down Expand Up @@ -85,7 +85,7 @@ def fitbit_configuration_callback(fields: list[dict[str, str]]) -> None:
"""Handle configuration updates."""
config_path = hass.config.path(FITBIT_CONFIG_FILE)
if os.path.isfile(config_path):
config_file = load_json(config_path)
config_file = load_json_object(config_path)
if config_file == DEFAULT_CONFIG:
error_msg = (
f"You didn't correctly modify {FITBIT_CONFIG_FILE}, please try"
Expand Down Expand Up @@ -161,7 +161,7 @@ def setup_platform(
"""Set up the Fitbit sensor."""
config_path = hass.config.path(FITBIT_CONFIG_FILE)
if os.path.isfile(config_path):
config_file: ConfigType = cast(ConfigType, load_json(config_path))
config_file = load_json_object(config_path)
if config_file == DEFAULT_CONFIG:
request_app_setup(
hass, config, add_entities, config_path, discovery_info=None
Expand All @@ -175,13 +175,10 @@ def setup_platform(
if "fitbit" in _CONFIGURING:
configurator.request_done(hass, _CONFIGURING.pop("fitbit"))

access_token: str | None = config_file.get(ATTR_ACCESS_TOKEN)
refresh_token: str | None = config_file.get(ATTR_REFRESH_TOKEN)
expires_at: int | None = config_file.get(ATTR_LAST_SAVED_AT)
if (
access_token is not None
and refresh_token is not None
and expires_at is not None
(access_token := config_file.get(ATTR_ACCESS_TOKEN)) is not None
and (refresh_token := config_file.get(ATTR_REFRESH_TOKEN)) is not None
and (expires_at := config_file.get(ATTR_LAST_SAVED_AT)) is not None
):
authd_client = Fitbit(
config_file.get(CONF_CLIENT_ID),
Expand All @@ -192,7 +189,7 @@ def setup_platform(
refresh_cb=lambda x: None,
)

if int(time.time()) - expires_at > 3600:
if int(time.time()) - cast(int, expires_at) > 3600:
authd_client.client.refresh_token()

user_profile = authd_client.user_profile_get()["user"]
Expand Down