Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions homeassistant/components/nanoleaf/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, JsonValueType, load_json_object

from .const import DOMAIN

Expand All @@ -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
Expand Down Expand Up @@ -134,19 +132,19 @@ 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: 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
Expand Down
6 changes: 3 additions & 3 deletions tests/components/nanoleaf/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down