core/homeassistant/components/fujitsu_fglair/config_flow.py

129 lines
4.3 KiB
Python
Raw Normal View History

Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
"""Config flow for Fujitsu HVAC (based on Ayla IOT) integration."""
from collections.abc import Mapping
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
import logging
from typing import Any
from ayla_iot_unofficial import AylaAuthError, new_ayla_api
from ayla_iot_unofficial.fujitsu_consts import FGLAIR_APP_CREDENTIALS
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
from .const import API_TIMEOUT, CONF_REGION, DOMAIN, REGION_DEFAULT, REGION_EU
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_REGION, default=REGION_DEFAULT): SelectSelector(
SelectSelectorConfig(
options=[region.lower() for region in FGLAIR_APP_CREDENTIALS],
translation_key=CONF_REGION,
)
),
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
}
)
STEP_REAUTH_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_PASSWORD): str,
}
)
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
class FGLairConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Fujitsu HVAC (based on Ayla IOT)."""
MINOR_VERSION = 2
_reauth_entry: ConfigEntry | None = None
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
async def _async_validate_credentials(
self, user_input: dict[str, Any]
) -> dict[str, str]:
errors: dict[str, str] = {}
app_id, app_secret = FGLAIR_APP_CREDENTIALS[user_input[CONF_REGION]]
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
api = new_ayla_api(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
app_id,
app_secret,
europe=user_input[CONF_REGION] == REGION_EU,
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
websession=aiohttp_client.async_get_clientsession(self.hass),
timeout=API_TIMEOUT,
)
try:
await api.async_sign_in()
except TimeoutError:
errors["base"] = "cannot_connect"
except AylaAuthError:
errors["base"] = "invalid_auth"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
return errors
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input:
await self.async_set_unique_id(user_input[CONF_USERNAME].lower())
self._abort_if_unique_id_configured()
errors = await self._async_validate_credentials(user_input)
if not errors:
Add Fujitsu FGLair integration (#109335) * Add support for Fujitsu HVAC devices * Add the entity code to .coveragerc * Only include code that can fail in the try/except block Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> * Remove empty keys from manifest * Remove VERSION as it's already the default * Remve the get_devices function and use asyncio.gather to parallelize dev updates * Move initial step to a function * Let KeyError bubble up. If we are passed an invalid mode it's probably worth raising an exception. * Await the gather * Use the async version of the refresh_auth call * Use the serial number as unique id * Use HA constant for precision * Use dev instead of self._dev * Move to property decorated methods * Remove bidict dependency * Setup one config entry for our api credentials instead of per device * Remove bidict from requirements * Signout and remove our api object on unload * Use app credentials from ayla_iot_unofficial * Use entry_id as a key to store our API object * Delete unused code * Create reverse mappings from forward mapping instead of hardcoding them * Clean up the property methods * Only import part of config_entries we are using * Implement suggested changes * Fix tests to use new API consts * Add support for reauth * Use a coordinator instead of doing per-entity refresh * Auto is equivalent to HEAT_COOL not AUTO * Add ON and OFF to list of supported features * Use the mock_setup_entry fixture for the reauth tests * Parametrize testing of config flow exceptions * Only wrap fallable code in try/except * Add tests for coordinator * Use self.coordinator_context instead of self._dev.device_serial_number * Move timeout to ayla_iot_unofficial * Add description for is_europe field * Bump version of ayla-iot-unofficial * Remove turn_on/turn_off warning * Move coordinator creating to __init__ * Add the type of coordinator to the CoordiatorEntity * Update docstring for FujitsuHVACDevice constructor * Fix missed self._dev to dev * Abort instead of showing the form again with an error when usernames are different * Remove useless argument * Fix tests * Implement some suggestions * Use a device property the maps to the coordinator data * Fix api sign out when unloading the entry * Address comments * Fix device lookup * Move API sign in to coordinator setup * Get rid of FujitsuHVACConfigData * Fix async_setup_entry signature * Fix mock_ayla_api * Cleanup common errors * Add test to check that re adding the same account fails * Also patch new_ayla_api in __init__.py * Create a fixture to generate test devices * Add a setup_integration function that does the setup for a mock config entry * Rework unit tests for the coordinator * Fix typos * Use hass session * Rework reauth config flow to only modify password * Update name to be more use-friendly * Fix wrong type for entry in async_unload_entry * Let TimeoutError bubble up as teh base class handles it * Make the mock ayla api return some devices by default * Move test to test_climate.py * Move tests to test_init.py * Remove reauth flow * Remove useless mock setup * Make our mock devices look real * Fix tests * Rename fujitsu_hvac to fujitsu_fglair and rename the integration to FGLair * Add the Fujitsu brand * Add a helper function to generate an entity_id from a device * Use entity_id to remove hardcoded entity ids * Add a test to increase code coverage --------- Co-authored-by: Josef Zweck <24647999+zweckj@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Joostlek <joostlek@outlook.com>
2024-08-18 13:37:33 +00:00
return self.async_create_entry(
title=f"FGLair ({user_input[CONF_USERNAME]})",
data=user_input,
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
self._reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Dialog that informs the user that reauth is required."""
errors: dict[str, str] = {}
assert self._reauth_entry
if user_input:
reauth_data = {
**self._reauth_entry.data,
CONF_PASSWORD: user_input[CONF_PASSWORD],
}
errors = await self._async_validate_credentials(reauth_data)
if len(errors) == 0:
return self.async_update_reload_and_abort(
self._reauth_entry, data=reauth_data
)
return self.async_show_form(
step_id="reauth_confirm",
data_schema=STEP_REAUTH_DATA_SCHEMA,
description_placeholders={
CONF_USERNAME: self._reauth_entry.data[CONF_USERNAME],
**self.context["title_placeholders"],
},
errors=errors,
)