From 1278fe1f81321bb37f163a171004bb0bf91611fb Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 22 Feb 2023 13:54:02 +0100 Subject: [PATCH] Use load_json_object in nanoleaf (#88592) * Use load_json_object in nanoleaf * pretty * prettier --- .../components/nanoleaf/config_flow.py | 36 +++++++++---------- tests/components/nanoleaf/test_config_flow.py | 6 ++-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/nanoleaf/config_flow.py b/homeassistant/components/nanoleaf/config_flow.py index be16db310b5..87239f5fd80 100644 --- a/homeassistant/components/nanoleaf/config_flow.py +++ b/homeassistant/components/nanoleaf/config_flow.py @@ -15,7 +15,7 @@ from homeassistant.const import CONF_HOST, CONF_TOKEN from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.json import save_json -from homeassistant.util.json import load_json +from homeassistant.util.json import JsonObjectType, JsonValueType, load_json_object from .const import DOMAIN @@ -36,16 +36,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): reauth_entry: config_entries.ConfigEntry | None = None + nanoleaf: Nanoleaf + + # For discovery integration import + discovery_conf: JsonObjectType + device_id: str + VERSION = 1 - def __init__(self) -> None: - """Initialize a Nanoleaf flow.""" - self.nanoleaf: Nanoleaf - - # For discovery integration import - self.discovery_conf: dict - self.device_id: str - async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: @@ -134,19 +132,19 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # Import from discovery integration self.device_id = device_id - self.discovery_conf = cast( - dict, - await self.hass.async_add_executor_job( - load_json, self.hass.config.path(CONFIG_FILE) - ), - ) - auth_token: str | None = self.discovery_conf.get(self.device_id, {}).get( - "token", # >= 2021.4 - self.discovery_conf.get(host, {}).get("token"), # < 2021.4 + self.discovery_conf = await self.hass.async_add_executor_job( + load_json_object, self.hass.config.path(CONFIG_FILE) ) + + auth_token: JsonValueType = None + if device_conf := self.discovery_conf.get(self.device_id): # >= 2021.4 + auth_token = cast(JsonObjectType, device_conf).get("token") + if not auth_token and (host_conf := self.discovery_conf.get(host)): # < 2021.4 + auth_token = cast(JsonObjectType, host_conf).get("token") + if auth_token is not None: self.nanoleaf = Nanoleaf( - async_get_clientsession(self.hass), host, auth_token + async_get_clientsession(self.hass), host, cast(str, auth_token) ) _LOGGER.warning( "Importing Nanoleaf %s from the discovery integration", name diff --git a/tests/components/nanoleaf/test_config_flow.py b/tests/components/nanoleaf/test_config_flow.py index 6880ae76719..200e2c21547 100644 --- a/tests/components/nanoleaf/test_config_flow.py +++ b/tests/components/nanoleaf/test_config_flow.py @@ -230,7 +230,7 @@ async def test_discovery_link_unavailable( with patch( "homeassistant.components.nanoleaf.config_flow.Nanoleaf.get_info", ), patch( - "homeassistant.components.nanoleaf.config_flow.load_json", + "homeassistant.components.nanoleaf.config_flow.load_json_object", return_value={}, ): result = await hass.config_entries.flow.async_init( @@ -353,7 +353,7 @@ async def test_import_discovery_integration( Test updating the .nanoleaf_conf file if it was not the only device in the file. """ with patch( - "homeassistant.components.nanoleaf.config_flow.load_json", + "homeassistant.components.nanoleaf.config_flow.load_json_object", return_value=dict(nanoleaf_conf_file), ), patch( "homeassistant.components.nanoleaf.config_flow.Nanoleaf", @@ -402,7 +402,7 @@ async def test_import_discovery_integration( async def test_ssdp_discovery(hass: HomeAssistant) -> None: """Test SSDP discovery.""" with patch( - "homeassistant.components.nanoleaf.config_flow.load_json", + "homeassistant.components.nanoleaf.config_flow.load_json_object", return_value={}, ), patch( "homeassistant.components.nanoleaf.config_flow.Nanoleaf",