Use load_json_object in nanoleaf (#88592)

* Use load_json_object in nanoleaf

* pretty

* prettier
This commit is contained in:
epenet 2023-02-22 13:54:02 +01:00 committed by GitHub
parent 79adfbc862
commit 1278fe1f81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 22 deletions

View file

@ -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

View file

@ -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",