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.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.json import save_json 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 from .const import DOMAIN
@ -36,16 +36,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
reauth_entry: config_entries.ConfigEntry | None = None reauth_entry: config_entries.ConfigEntry | None = None
nanoleaf: Nanoleaf
# For discovery integration import
discovery_conf: JsonObjectType
device_id: str
VERSION = 1 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( async def async_step_user(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> FlowResult: ) -> FlowResult:
@ -134,19 +132,19 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# Import from discovery integration # Import from discovery integration
self.device_id = device_id self.device_id = device_id
self.discovery_conf = cast( self.discovery_conf = await self.hass.async_add_executor_job(
dict, load_json_object, self.hass.config.path(CONFIG_FILE)
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
) )
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: if auth_token is not None:
self.nanoleaf = Nanoleaf( self.nanoleaf = Nanoleaf(
async_get_clientsession(self.hass), host, auth_token async_get_clientsession(self.hass), host, cast(str, auth_token)
) )
_LOGGER.warning( _LOGGER.warning(
"Importing Nanoleaf %s from the discovery integration", name "Importing Nanoleaf %s from the discovery integration", name

View File

@ -230,7 +230,7 @@ async def test_discovery_link_unavailable(
with patch( with patch(
"homeassistant.components.nanoleaf.config_flow.Nanoleaf.get_info", "homeassistant.components.nanoleaf.config_flow.Nanoleaf.get_info",
), patch( ), patch(
"homeassistant.components.nanoleaf.config_flow.load_json", "homeassistant.components.nanoleaf.config_flow.load_json_object",
return_value={}, return_value={},
): ):
result = await hass.config_entries.flow.async_init( 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. Test updating the .nanoleaf_conf file if it was not the only device in the file.
""" """
with patch( with patch(
"homeassistant.components.nanoleaf.config_flow.load_json", "homeassistant.components.nanoleaf.config_flow.load_json_object",
return_value=dict(nanoleaf_conf_file), return_value=dict(nanoleaf_conf_file),
), patch( ), patch(
"homeassistant.components.nanoleaf.config_flow.Nanoleaf", "homeassistant.components.nanoleaf.config_flow.Nanoleaf",
@ -402,7 +402,7 @@ async def test_import_discovery_integration(
async def test_ssdp_discovery(hass: HomeAssistant) -> None: async def test_ssdp_discovery(hass: HomeAssistant) -> None:
"""Test SSDP discovery.""" """Test SSDP discovery."""
with patch( with patch(
"homeassistant.components.nanoleaf.config_flow.load_json", "homeassistant.components.nanoleaf.config_flow.load_json_object",
return_value={}, return_value={},
), patch( ), patch(
"homeassistant.components.nanoleaf.config_flow.Nanoleaf", "homeassistant.components.nanoleaf.config_flow.Nanoleaf",