From 5f3cc5f29a80ec23bddfba99fe1b609c2d19d6ad Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:09:59 +0000 Subject: [PATCH 1/3] Use load_json_object in nanoleaf --- .../components/nanoleaf/config_flow.py | 36 ++++++++++--------- tests/components/nanoleaf/test_config_flow.py | 6 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/nanoleaf/config_flow.py b/homeassistant/components/nanoleaf/config_flow.py index be16db310b563..e44bc78f09844 100644 --- a/homeassistant/components/nanoleaf/config_flow.py +++ b/homeassistant/components/nanoleaf/config_flow.py @@ -15,7 +15,7 @@ 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, load_json_object from .const import DOMAIN @@ -36,15 +36,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): reauth_entry: config_entries.ConfigEntry | None = None - VERSION = 1 + nanoleaf: Nanoleaf - def __init__(self) -> None: - """Initialize a Nanoleaf flow.""" - self.nanoleaf: Nanoleaf + # For discovery integration import + discovery_conf: JsonObjectType + device_id: str - # For discovery integration import - self.discovery_conf: dict - self.device_id: str + VERSION = 1 async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -134,16 +132,20 @@ async def _async_discovery_handler( # 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: str | None = None + if device_conf := self.discovery_conf.get(self.device_id): # >= 2021.4 + auth_token = cast( + str | None, cast(JsonObjectType, device_conf).get("token") + ) + if auth_token is None and ( + host_conf := self.discovery_conf.get(host) + ): # < 2021.4 + auth_token = cast(str | None, cast(JsonObjectType, host_conf).get("token")) + if auth_token is not None: self.nanoleaf = Nanoleaf( async_get_clientsession(self.hass), host, auth_token diff --git a/tests/components/nanoleaf/test_config_flow.py b/tests/components/nanoleaf/test_config_flow.py index 6880ae7671954..200e2c2154790 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", From 0458ec80de59d500d63a2021bb73496bf1e46c34 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:11:41 +0000 Subject: [PATCH 2/3] pretty --- homeassistant/components/nanoleaf/config_flow.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/nanoleaf/config_flow.py b/homeassistant/components/nanoleaf/config_flow.py index e44bc78f09844..be39e4ad72970 100644 --- a/homeassistant/components/nanoleaf/config_flow.py +++ b/homeassistant/components/nanoleaf/config_flow.py @@ -141,9 +141,7 @@ async def _async_discovery_handler( auth_token = cast( str | None, cast(JsonObjectType, device_conf).get("token") ) - if auth_token is None and ( - host_conf := self.discovery_conf.get(host) - ): # < 2021.4 + if not auth_token and (host_conf := self.discovery_conf.get(host)): # < 2021.4 auth_token = cast(str | None, cast(JsonObjectType, host_conf).get("token")) if auth_token is not None: From 88497eb4a5320ad621788c629d7c0044011d2b10 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:13:02 +0000 Subject: [PATCH 3/3] prettier --- homeassistant/components/nanoleaf/config_flow.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/nanoleaf/config_flow.py b/homeassistant/components/nanoleaf/config_flow.py index be39e4ad72970..87239f5fd8032 100644 --- a/homeassistant/components/nanoleaf/config_flow.py +++ b/homeassistant/components/nanoleaf/config_flow.py @@ -15,7 +15,7 @@ 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 JsonObjectType, load_json_object +from homeassistant.util.json import JsonObjectType, JsonValueType, load_json_object from .const import DOMAIN @@ -136,17 +136,15 @@ async def _async_discovery_handler( load_json_object, self.hass.config.path(CONFIG_FILE) ) - auth_token: str | None = None + auth_token: JsonValueType = None if device_conf := self.discovery_conf.get(self.device_id): # >= 2021.4 - auth_token = cast( - str | None, cast(JsonObjectType, device_conf).get("token") - ) + 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(str | None, cast(JsonObjectType, host_conf).get("token")) + 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