Use load_json_object in fitbit (#88585)

* Use load_json_object in fitbit

* Remove unnecessary cast
pull/88949/head
epenet 2023-03-01 07:44:29 +01:00 committed by GitHub
parent 95dd62186e
commit 50f908ce2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 10 deletions

View File

@ -27,7 +27,7 @@ from homeassistant.helpers.icon import icon_for_battery_level
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 (
@ -85,7 +85,7 @@ def request_app_setup(
"""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"
@ -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
@ -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),
@ -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"]