Use load_json_object in fitbit (#88585)
* Use load_json_object in fitbit * Remove unnecessary cast
This commit is contained in:
parent
95dd62186e
commit
50f908ce2d
1 changed files with 7 additions and 10 deletions
|
@ -27,7 +27,7 @@ from homeassistant.helpers.icon import icon_for_battery_level
|
||||||
from homeassistant.helpers.json import save_json
|
from homeassistant.helpers.json import save_json
|
||||||
from homeassistant.helpers.network import NoURLAvailableError, get_url
|
from homeassistant.helpers.network import NoURLAvailableError, get_url
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
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 homeassistant.util.unit_system import METRIC_SYSTEM
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
@ -85,7 +85,7 @@ def request_app_setup(
|
||||||
"""Handle configuration updates."""
|
"""Handle configuration updates."""
|
||||||
config_path = hass.config.path(FITBIT_CONFIG_FILE)
|
config_path = hass.config.path(FITBIT_CONFIG_FILE)
|
||||||
if os.path.isfile(config_path):
|
if os.path.isfile(config_path):
|
||||||
config_file = load_json(config_path)
|
config_file = load_json_object(config_path)
|
||||||
if config_file == DEFAULT_CONFIG:
|
if config_file == DEFAULT_CONFIG:
|
||||||
error_msg = (
|
error_msg = (
|
||||||
f"You didn't correctly modify {FITBIT_CONFIG_FILE}, please try"
|
f"You didn't correctly modify {FITBIT_CONFIG_FILE}, please try"
|
||||||
|
@ -161,7 +161,7 @@ def setup_platform(
|
||||||
"""Set up the Fitbit sensor."""
|
"""Set up the Fitbit sensor."""
|
||||||
config_path = hass.config.path(FITBIT_CONFIG_FILE)
|
config_path = hass.config.path(FITBIT_CONFIG_FILE)
|
||||||
if os.path.isfile(config_path):
|
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:
|
if config_file == DEFAULT_CONFIG:
|
||||||
request_app_setup(
|
request_app_setup(
|
||||||
hass, config, add_entities, config_path, discovery_info=None
|
hass, config, add_entities, config_path, discovery_info=None
|
||||||
|
@ -175,13 +175,10 @@ def setup_platform(
|
||||||
if "fitbit" in _CONFIGURING:
|
if "fitbit" in _CONFIGURING:
|
||||||
configurator.request_done(hass, _CONFIGURING.pop("fitbit"))
|
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 (
|
if (
|
||||||
access_token is not None
|
(access_token := config_file.get(ATTR_ACCESS_TOKEN)) is not None
|
||||||
and refresh_token is not None
|
and (refresh_token := config_file.get(ATTR_REFRESH_TOKEN)) is not None
|
||||||
and expires_at is not None
|
and (expires_at := config_file.get(ATTR_LAST_SAVED_AT)) is not None
|
||||||
):
|
):
|
||||||
authd_client = Fitbit(
|
authd_client = Fitbit(
|
||||||
config_file.get(CONF_CLIENT_ID),
|
config_file.get(CONF_CLIENT_ID),
|
||||||
|
@ -192,7 +189,7 @@ def setup_platform(
|
||||||
refresh_cb=lambda x: None,
|
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()
|
authd_client.client.refresh_token()
|
||||||
|
|
||||||
user_profile = authd_client.user_profile_get()["user"]
|
user_profile = authd_client.user_profile_get()["user"]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue