From 50f908ce2d0d4bdce892c4f451f918cc1a824fce Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 1 Mar 2023 07:44:29 +0100 Subject: [PATCH] Use load_json_object in fitbit (#88585) * Use load_json_object in fitbit * Remove unnecessary cast --- homeassistant/components/fitbit/sensor.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/fitbit/sensor.py b/homeassistant/components/fitbit/sensor.py index d703699a433..c53c01c84a7 100644 --- a/homeassistant/components/fitbit/sensor.py +++ b/homeassistant/components/fitbit/sensor.py @@ -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"]