core/tests/components/fujitsu_fglair/test_init.py

185 lines
6.1 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
"""Test the initialization of fujitsu_fglair entities."""
from unittest.mock import AsyncMock, patch
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 ayla_iot_unofficial import AylaAuthError
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
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.climate import HVACMode
from homeassistant.components.fujitsu_fglair.const import (
API_REFRESH,
API_TIMEOUT,
CONF_EUROPE,
CONF_REGION,
DOMAIN,
REGION_DEFAULT,
REGION_EU,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, STATE_UNAVAILABLE
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.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
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 . import entity_id, setup_integration
from .conftest import TEST_PASSWORD, TEST_USERNAME
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 tests.common import MockConfigEntry, async_fire_time_changed
async def test_auth_failure(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_devices: list[AsyncMock],
) -> None:
"""Test entities become unavailable after auth failure."""
await setup_integration(hass, mock_config_entry)
mock_ayla_api.async_get_devices.side_effect = AylaAuthError
freezer.tick(API_REFRESH)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id(mock_devices[0])).state == STATE_UNAVAILABLE
assert hass.states.get(entity_id(mock_devices[1])).state == STATE_UNAVAILABLE
@pytest.mark.parametrize(
"mock_config_entry", FGLAIR_APP_CREDENTIALS.keys(), indirect=True
)
async def test_auth_regions(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_devices: list[AsyncMock],
) -> None:
"""Test that we use the correct credentials if europe is selected."""
with patch(
"homeassistant.components.fujitsu_fglair.new_ayla_api", return_value=AsyncMock()
) as new_ayla_api_patch:
await setup_integration(hass, mock_config_entry)
new_ayla_api_patch.assert_called_once_with(
TEST_USERNAME,
TEST_PASSWORD,
FGLAIR_APP_CREDENTIALS[mock_config_entry.data[CONF_REGION]][0],
FGLAIR_APP_CREDENTIALS[mock_config_entry.data[CONF_REGION]][1],
europe=mock_config_entry.data[CONF_REGION] == "EU",
websession=aiohttp_client.async_get_clientsession(hass),
timeout=API_TIMEOUT,
)
@pytest.mark.parametrize("is_europe", [True, False])
async def test_migrate_entry_v11_v12(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
is_europe: bool,
mock_devices: list[AsyncMock],
) -> None:
"""Test migration from schema 1.1 to 1.2."""
v11_config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=TEST_USERNAME,
data={
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: is_europe,
},
)
await setup_integration(hass, v11_config_entry)
updated_entry = hass.config_entries.async_get_entry(v11_config_entry.entry_id)
assert updated_entry.state is ConfigEntryState.LOADED
assert updated_entry.version == 1
assert updated_entry.minor_version == 2
if is_europe:
assert updated_entry.data[CONF_REGION] is REGION_EU
else:
assert updated_entry.data[CONF_REGION] is REGION_DEFAULT
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 test_device_auth_failure(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_devices: list[AsyncMock],
) -> None:
"""Test entities become unavailable after auth failure with updating devices."""
await setup_integration(hass, mock_config_entry)
for d in mock_ayla_api.async_get_devices.return_value:
d.async_update.side_effect = AylaAuthError
freezer.tick(API_REFRESH)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id(mock_devices[0])).state == STATE_UNAVAILABLE
assert hass.states.get(entity_id(mock_devices[1])).state == STATE_UNAVAILABLE
async def test_device_offline(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
mock_devices: list[AsyncMock],
) -> None:
"""Test entities become unavailable if device if offline."""
await setup_integration(hass, mock_config_entry)
mock_ayla_api.async_get_devices.return_value[0].is_online.return_value = False
freezer.tick(API_REFRESH)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id(mock_devices[0])).state == STATE_UNAVAILABLE
assert hass.states.get(entity_id(mock_devices[1])).state == HVACMode.COOL
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 test_token_expired(
hass: HomeAssistant,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Make sure sign_in is called if the token expired."""
mock_ayla_api.token_expired = True
await setup_integration(hass, mock_config_entry)
# Called once during setup and once during update
assert mock_ayla_api.async_sign_in.call_count == 2
async def test_token_expiring_soon(
hass: HomeAssistant,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Make sure sign_in is called if the token expired."""
mock_ayla_api.token_expiring_soon = True
await setup_integration(hass, mock_config_entry)
mock_ayla_api.async_refresh_auth.assert_called_once()
@pytest.mark.parametrize("exception", [AylaAuthError, TimeoutError])
async def test_startup_exception(
hass: HomeAssistant,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
exception: Exception,
) -> None:
"""Make sure that no devices are added if there was an exception while logging in."""
mock_ayla_api.async_sign_in.side_effect = exception
await setup_integration(hass, mock_config_entry)
assert len(hass.states.async_all()) == 0