core/tests/components/laundrify/test_init.py

75 lines
2.6 KiB
Python
Raw Normal View History

"""Test the laundrify init file."""
from laundrify_aio import exceptions
from homeassistant.components.laundrify.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from .const import VALID_ACCESS_TOKEN
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
from tests.common import MockConfigEntry
async def test_setup_entry_api_unauthorized(
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
hass: HomeAssistant,
laundrify_api_mock,
laundrify_config_entry: MockConfigEntry,
) -> None:
"""Test that ConfigEntryAuthFailed is thrown when authentication fails."""
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
laundrify_api_mock.validate_token.side_effect = exceptions.UnauthorizedException
await hass.config_entries.async_reload(laundrify_config_entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
assert laundrify_config_entry.state is ConfigEntryState.SETUP_ERROR
assert not hass.data.get(DOMAIN)
async def test_setup_entry_api_cannot_connect(
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
hass: HomeAssistant,
laundrify_api_mock,
laundrify_config_entry: MockConfigEntry,
) -> None:
"""Test that ApiConnectionException is thrown when connection fails."""
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
laundrify_api_mock.validate_token.side_effect = exceptions.ApiConnectionException
await hass.config_entries.async_reload(laundrify_config_entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
assert laundrify_config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
async def test_setup_entry_successful(
hass: HomeAssistant, laundrify_config_entry: MockConfigEntry
) -> None:
"""Test entry can be setup successfully."""
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
assert laundrify_config_entry.state is ConfigEntryState.LOADED
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
async def test_setup_entry_unload(
hass: HomeAssistant, laundrify_config_entry: MockConfigEntry
) -> None:
"""Test unloading the laundrify entry."""
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
await hass.config_entries.async_unload(laundrify_config_entry.entry_id)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
Add sensor platform to laundrify integration (#121378) * feat: initial implementation of sensor platform * refactor(tests): await setup of config_entry in parent function * feat(tests): add tests for laundrify sensor platform * refactor: set name property for laundrify binary_sensor * refactor(tests): add missing type hints * refactor(tests): remove global change of the logging level * refactor: address minor changes from code review * refactor(tests): transform setup_config_entry into fixture * refactor: leverage entity descriptions to define common entity properties * refactor: change native unit to Wh * fix(tests): use fixture to create the config entry * fix: remove redundant raise of LaundrifyDeviceException * fix(tests): raise a LaundrifyDeviceException to test the update failure behavior * refactor(tests): merge several library fixtures into a single one * refactor(tests): create a separate UpdateCoordinator instead of using the internal * refactor(tests): avoid using LaundrifyPowerSensor * refactor: simplify value retrieval by directly accessing the coordinator * refactor: remove non-raising code from try-block * refactor(sensor): revert usage of entity descriptions * refactor(sensor): consolidate common attributes and init func to LaundrifyBaseSensor * refactor(sensor): instantiate DeviceInfo obj instead of using dict * refactor(tests): use freezer to trigger coordinator update * refactor(tests): assert on entity state instead of coordinator * refactor(tests): make use of freezer * chore(tests): typo in comment
2024-09-16 14:21:16 +00:00
assert laundrify_config_entry.state is ConfigEntryState.NOT_LOADED
async def test_migrate_entry_minor_version_1_2(hass: HomeAssistant) -> None:
"""Test migrating a 1.1 config entry to 1.2."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_ACCESS_TOKEN: VALID_ACCESS_TOKEN},
version=1,
minor_version=1,
unique_id=123456,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
assert entry.version == 1
assert entry.minor_version == 2
assert entry.unique_id == "123456"