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>
pull/115832/head
Antoine Reversat 2024-08-18 09:37:33 -04:00 committed by GitHub
parent 10c27c3189
commit 1afed8ae15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 1111 additions and 4 deletions

View File

@ -196,6 +196,7 @@ homeassistant.components.fritzbox.*
homeassistant.components.fritzbox_callmonitor.*
homeassistant.components.fronius.*
homeassistant.components.frontend.*
homeassistant.components.fujitsu_fglair.*
homeassistant.components.fully_kiosk.*
homeassistant.components.fyta.*
homeassistant.components.generic_hygrostat.*

View File

@ -499,6 +499,8 @@ build.json @home-assistant/supervisor
/tests/components/frontend/ @home-assistant/frontend
/homeassistant/components/frontier_silicon/ @wlcrs
/tests/components/frontier_silicon/ @wlcrs
/homeassistant/components/fujitsu_fglair/ @crevetor
/tests/components/fujitsu_fglair/ @crevetor
/homeassistant/components/fully_kiosk/ @cgarwood
/tests/components/fully_kiosk/ @cgarwood
/homeassistant/components/fyta/ @dontinelli

View File

@ -0,0 +1,5 @@
{
"domain": "fujitsu",
"name": "Fujitsu",
"integrations": ["fujitsu_anywair", "fujitsu_fglair"]
}

View File

@ -0,0 +1,49 @@
"""The Fujitsu HVAC (based on Ayla IOT) integration."""
from __future__ import annotations
from contextlib import suppress
from ayla_iot_unofficial import new_ayla_api
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from .const import API_TIMEOUT, CONF_EUROPE, FGLAIR_APP_ID, FGLAIR_APP_SECRET
from .coordinator import FGLairCoordinator
PLATFORMS: list[Platform] = [Platform.CLIMATE]
type FGLairConfigEntry = ConfigEntry[FGLairCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: FGLairConfigEntry) -> bool:
"""Set up Fujitsu HVAC (based on Ayla IOT) from a config entry."""
api = new_ayla_api(
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
FGLAIR_APP_ID,
FGLAIR_APP_SECRET,
europe=entry.data[CONF_EUROPE],
websession=aiohttp_client.async_get_clientsession(hass),
timeout=API_TIMEOUT,
)
coordinator = FGLairCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: FGLairConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
with suppress(TimeoutError):
await entry.runtime_data.api.async_sign_out()
return unload_ok

View File

@ -0,0 +1,141 @@
"""Support for Fujitsu HVAC devices that use the Ayla Iot platform."""
from typing import Any
from ayla_iot_unofficial.fujitsu_hvac import Capability, FujitsuHVAC
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityFeature,
HVACMode,
)
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_HALVES, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import FGLairConfigEntry
from .const import (
DOMAIN,
FUJI_TO_HA_FAN,
FUJI_TO_HA_HVAC,
FUJI_TO_HA_SWING,
HA_TO_FUJI_FAN,
HA_TO_FUJI_HVAC,
HA_TO_FUJI_SWING,
)
from .coordinator import FGLairCoordinator
async def async_setup_entry(
hass: HomeAssistant,
entry: FGLairConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up one Fujitsu HVAC device."""
async_add_entities(
FGLairDevice(entry.runtime_data, device)
for device in entry.runtime_data.data.values()
)
class FGLairDevice(CoordinatorEntity[FGLairCoordinator], ClimateEntity):
"""Represent a Fujitsu HVAC device."""
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_precision = PRECISION_HALVES
_attr_target_temperature_step = 0.5
_attr_has_entity_name = True
_attr_name = None
_enable_turn_on_off_backwards_compatibility: bool = False
def __init__(self, coordinator: FGLairCoordinator, device: FujitsuHVAC) -> None:
"""Store the representation of the device and set the static attributes."""
super().__init__(coordinator, context=device.device_serial_number)
self._attr_unique_id = device.device_serial_number
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device.device_serial_number)},
name=device.device_name,
manufacturer="Fujitsu",
model=device.property_values["model_name"],
serial_number=device.device_serial_number,
sw_version=device.property_values["mcu_firmware_version"],
)
self._attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
if device.has_capability(Capability.OP_FAN):
self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
if device.has_capability(Capability.SWING_HORIZONTAL) or device.has_capability(
Capability.SWING_VERTICAL
):
self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
self._set_attr()
@property
def device(self) -> FujitsuHVAC:
"""Return the device object from the coordinator data."""
return self.coordinator.data[self.coordinator_context]
@property
def available(self) -> bool:
"""Return if the device is available."""
return super().available and self.coordinator_context in self.coordinator.data
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set Fan mode."""
await self.device.async_set_fan_speed(HA_TO_FUJI_FAN[fan_mode])
await self.coordinator.async_request_refresh()
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set HVAC mode."""
await self.device.async_set_op_mode(HA_TO_FUJI_HVAC[hvac_mode])
await self.coordinator.async_request_refresh()
async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set swing mode."""
await self.device.async_set_swing_mode(HA_TO_FUJI_SWING[swing_mode])
await self.coordinator.async_request_refresh()
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set target temperature."""
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return
await self.device.async_set_set_temp(temperature)
await self.coordinator.async_request_refresh()
def _set_attr(self) -> None:
if self.coordinator_context in self.coordinator.data:
self._attr_fan_mode = FUJI_TO_HA_FAN.get(self.device.fan_speed)
self._attr_fan_modes = [
FUJI_TO_HA_FAN[mode]
for mode in self.device.supported_fan_speeds
if mode in FUJI_TO_HA_FAN
]
self._attr_hvac_mode = FUJI_TO_HA_HVAC.get(self.device.op_mode)
self._attr_hvac_modes = [
FUJI_TO_HA_HVAC[mode]
for mode in self.device.supported_op_modes
if mode in FUJI_TO_HA_HVAC
]
self._attr_swing_mode = FUJI_TO_HA_SWING.get(self.device.swing_mode)
self._attr_swing_modes = [
FUJI_TO_HA_SWING[mode]
for mode in self.device.supported_swing_modes
if mode in FUJI_TO_HA_SWING
]
self._attr_min_temp = self.device.temperature_range[0]
self._attr_max_temp = self.device.temperature_range[1]
self._attr_current_temperature = self.device.sensed_temp
self._attr_target_temperature = self.device.set_temp
def _handle_coordinator_update(self) -> None:
self._set_attr()
super()._handle_coordinator_update()

View File

@ -0,0 +1,73 @@
"""Config flow for Fujitsu HVAC (based on Ayla IOT) integration."""
import logging
from typing import Any
from ayla_iot_unofficial import AylaAuthError, new_ayla_api
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import aiohttp_client
from .const import API_TIMEOUT, CONF_EUROPE, DOMAIN, FGLAIR_APP_ID, FGLAIR_APP_SECRET
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_EUROPE): bool,
}
)
class FGLairConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Fujitsu HVAC (based on Ayla IOT)."""
async def _async_validate_credentials(
self, user_input: dict[str, Any]
) -> dict[str, str]:
errors: dict[str, str] = {}
api = new_ayla_api(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
FGLAIR_APP_ID,
FGLAIR_APP_SECRET,
europe=user_input[CONF_EUROPE],
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 len(errors) == 0:
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
)

View File

@ -0,0 +1,54 @@
"""Constants for the Fujitsu HVAC (based on Ayla IOT) integration."""
from datetime import timedelta
from ayla_iot_unofficial.fujitsu_consts import ( # noqa: F401
FGLAIR_APP_ID,
FGLAIR_APP_SECRET,
)
from ayla_iot_unofficial.fujitsu_hvac import FanSpeed, OpMode, SwingMode
from homeassistant.components.climate import (
FAN_AUTO,
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
SWING_BOTH,
SWING_HORIZONTAL,
SWING_OFF,
SWING_VERTICAL,
HVACMode,
)
API_TIMEOUT = 10
API_REFRESH = timedelta(minutes=5)
DOMAIN = "fujitsu_fglair"
CONF_EUROPE = "is_europe"
HA_TO_FUJI_FAN = {
FAN_LOW: FanSpeed.LOW,
FAN_MEDIUM: FanSpeed.MEDIUM,
FAN_HIGH: FanSpeed.HIGH,
FAN_AUTO: FanSpeed.AUTO,
}
FUJI_TO_HA_FAN = {value: key for key, value in HA_TO_FUJI_FAN.items()}
HA_TO_FUJI_HVAC = {
HVACMode.OFF: OpMode.OFF,
HVACMode.HEAT: OpMode.HEAT,
HVACMode.COOL: OpMode.COOL,
HVACMode.HEAT_COOL: OpMode.AUTO,
HVACMode.DRY: OpMode.DRY,
HVACMode.FAN_ONLY: OpMode.FAN,
}
FUJI_TO_HA_HVAC = {value: key for key, value in HA_TO_FUJI_HVAC.items()}
HA_TO_FUJI_SWING = {
SWING_OFF: SwingMode.OFF,
SWING_VERTICAL: SwingMode.SWING_VERTICAL,
SWING_HORIZONTAL: SwingMode.SWING_HORIZONTAL,
SWING_BOTH: SwingMode.SWING_BOTH,
}
FUJI_TO_HA_SWING = {value: key for key, value in HA_TO_FUJI_SWING.items()}

View File

@ -0,0 +1,63 @@
"""Coordinator for Fujitsu HVAC integration."""
import logging
from ayla_iot_unofficial import AylaApi, AylaAuthError
from ayla_iot_unofficial.fujitsu_hvac import FujitsuHVAC
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import API_REFRESH
_LOGGER = logging.getLogger(__name__)
class FGLairCoordinator(DataUpdateCoordinator[dict[str, FujitsuHVAC]]):
"""Coordinator for Fujitsu HVAC integration."""
def __init__(self, hass: HomeAssistant, api: AylaApi) -> None:
"""Initialize coordinator for Fujitsu HVAC integration."""
super().__init__(
hass,
_LOGGER,
name="Fujitsu HVAC data",
update_interval=API_REFRESH,
)
self.api = api
async def _async_setup(self) -> None:
try:
await self.api.async_sign_in()
except AylaAuthError as e:
raise ConfigEntryError("Credentials expired for Ayla IoT API") from e
async def _async_update_data(self) -> dict[str, FujitsuHVAC]:
"""Fetch data from api endpoint."""
listening_entities = set(self.async_contexts())
try:
if self.api.token_expired:
await self.api.async_sign_in()
if self.api.token_expiring_soon:
await self.api.async_refresh_auth()
devices = await self.api.async_get_devices()
except AylaAuthError as e:
raise ConfigEntryError("Credentials expired for Ayla IoT API") from e
if len(listening_entities) == 0:
devices = list(filter(lambda x: isinstance(x, FujitsuHVAC), devices))
else:
devices = list(
filter(lambda x: x.device_serial_number in listening_entities, devices)
)
try:
for dev in devices:
await dev.async_update()
except AylaAuthError as e:
raise ConfigEntryError("Credentials expired for Ayla IoT API") from e
return {d.device_serial_number: d for d in devices}

View File

@ -0,0 +1,9 @@
{
"domain": "fujitsu_fglair",
"name": "FGLair",
"codeowners": ["@crevetor"],
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/fujitsu_fglair",
"iot_class": "cloud_polling",
"requirements": ["ayla-iot-unofficial==1.3.1"]
}

View File

@ -0,0 +1,25 @@
{
"config": {
"step": {
"user": {
"title": "Enter your FGLair credentials",
"data": {
"is_europe": "Use european servers",
"username": "[%key:common::config_flow::data::username%]",
"password": "[%key:common::config_flow::data::password%]"
},
"data_description": {
"is_europe": "Allows the user to choose whether to use european servers or not since the API uses different endoint URLs for european vs non-european users"
}
}
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
}
}

View File

@ -200,6 +200,7 @@ FLOWS = {
"fritzbox_callmonitor",
"fronius",
"frontier_silicon",
"fujitsu_fglair",
"fully_kiosk",
"fyta",
"garages_amsterdam",

View File

@ -2054,10 +2054,22 @@
"config_flow": true,
"iot_class": "local_polling"
},
"fujitsu_anywair": {
"name": "Fujitsu anywAIR",
"integration_type": "virtual",
"supported_by": "advantage_air"
"fujitsu": {
"name": "Fujitsu",
"integrations": {
"fujitsu_anywair": {
"integration_type": "virtual",
"config_flow": false,
"supported_by": "advantage_air",
"name": "Fujitsu anywAIR"
},
"fujitsu_fglair": {
"integration_type": "hub",
"config_flow": true,
"iot_class": "cloud_polling",
"name": "FGLair"
}
}
},
"fully_kiosk": {
"name": "Fully Kiosk Browser",

View File

@ -1716,6 +1716,16 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.fujitsu_fglair.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.fully_kiosk.*]
check_untyped_defs = true
disallow_incomplete_defs = true

View File

@ -522,6 +522,9 @@ autarco==2.0.0
# homeassistant.components.axis
axis==62
# homeassistant.components.fujitsu_fglair
ayla-iot-unofficial==1.3.1
# homeassistant.components.azure_event_hub
azure-eventhub==5.11.1

View File

@ -471,6 +471,9 @@ autarco==2.0.0
# homeassistant.components.axis
axis==62
# homeassistant.components.fujitsu_fglair
ayla-iot-unofficial==1.3.1
# homeassistant.components.azure_event_hub
azure-eventhub==5.11.1

View File

@ -0,0 +1,21 @@
"""Tests for the Fujitsu HVAC (based on Ayla IOT) integration."""
from ayla_iot_unofficial.fujitsu_hvac import FujitsuHVAC
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Fixture for setting up the component."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
def entity_id(device: FujitsuHVAC) -> str:
"""Generate the entity id for the given serial."""
return f"{Platform.CLIMATE}.{device.device_serial_number}"

View File

@ -0,0 +1,113 @@
"""Common fixtures for the Fujitsu HVAC (based on Ayla IOT) tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, create_autospec, patch
from ayla_iot_unofficial import AylaApi
from ayla_iot_unofficial.fujitsu_hvac import FanSpeed, FujitsuHVAC, OpMode, SwingMode
import pytest
from homeassistant.components.fujitsu_fglair.const import CONF_EUROPE, DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from tests.common import MockConfigEntry
TEST_DEVICE_NAME = "Test device"
TEST_DEVICE_SERIAL = "testserial"
TEST_USERNAME = "test-username"
TEST_PASSWORD = "test-password"
TEST_USERNAME2 = "test-username2"
TEST_PASSWORD2 = "test-password2"
TEST_SERIAL_NUMBER = "testserial123"
TEST_SERIAL_NUMBER2 = "testserial345"
TEST_PROPERTY_VALUES = {
"model_name": "mock_fujitsu_device",
"mcu_firmware_version": "1",
}
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.fujitsu_fglair.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_ayla_api(mock_devices: list[AsyncMock]) -> Generator[AsyncMock]:
"""Override AylaApi creation."""
my_mock = create_autospec(AylaApi)
with (
patch(
"homeassistant.components.fujitsu_fglair.new_ayla_api", return_value=my_mock
),
patch(
"homeassistant.components.fujitsu_fglair.config_flow.new_ayla_api",
return_value=my_mock,
),
):
my_mock.async_get_devices.return_value = mock_devices
yield my_mock
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return a regular config entry."""
return MockConfigEntry(
domain=DOMAIN,
unique_id=TEST_USERNAME,
data={
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: False,
},
)
def _create_device(serial_number: str) -> AsyncMock:
dev = AsyncMock(spec=FujitsuHVAC)
dev.device_serial_number = serial_number
dev.device_name = serial_number
dev.property_values = TEST_PROPERTY_VALUES
dev.has_capability.return_value = True
dev.fan_speed = FanSpeed.AUTO
dev.supported_fan_speeds = [
FanSpeed.LOW,
FanSpeed.MEDIUM,
FanSpeed.HIGH,
FanSpeed.AUTO,
]
dev.op_mode = OpMode.COOL
dev.supported_op_modes = [
OpMode.OFF,
OpMode.ON,
OpMode.AUTO,
OpMode.COOL,
OpMode.DRY,
]
dev.swing_mode = SwingMode.SWING_BOTH
dev.supported_swing_modes = [
SwingMode.OFF,
SwingMode.SWING_HORIZONTAL,
SwingMode.SWING_VERTICAL,
SwingMode.SWING_BOTH,
]
dev.temperature_range = [18.0, 26.0]
dev.sensed_temp = 22.0
dev.set_temp = 21.0
return dev
@pytest.fixture
def mock_devices() -> list[AsyncMock]:
"""Generate a list of mock devices that the API can return."""
return [
_create_device(serial) for serial in (TEST_SERIAL_NUMBER, TEST_SERIAL_NUMBER2)
]

View File

@ -0,0 +1,189 @@
# serializer version: 1
# name: test_entities[climate.testserial123-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'fan_modes': list([
'low',
'medium',
'high',
'auto',
]),
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.HEAT_COOL: 'heat_cool'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 26.0,
'min_temp': 18.0,
'swing_modes': list([
'off',
'horizontal',
'vertical',
'both',
]),
'target_temp_step': 0.5,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.testserial123',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'fujitsu_fglair',
'previous_unique_id': None,
'supported_features': <ClimateEntityFeature: 425>,
'translation_key': None,
'unique_id': 'testserial123',
'unit_of_measurement': None,
})
# ---
# name: test_entities[climate.testserial123-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 22.0,
'fan_mode': 'auto',
'fan_modes': list([
'low',
'medium',
'high',
'auto',
]),
'friendly_name': 'testserial123',
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.HEAT_COOL: 'heat_cool'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 26.0,
'min_temp': 18.0,
'supported_features': <ClimateEntityFeature: 425>,
'swing_mode': 'both',
'swing_modes': list([
'off',
'horizontal',
'vertical',
'both',
]),
'target_temp_step': 0.5,
'temperature': 21.0,
}),
'context': <ANY>,
'entity_id': 'climate.testserial123',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'cool',
})
# ---
# name: test_entities[climate.testserial345-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'fan_modes': list([
'low',
'medium',
'high',
'auto',
]),
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.HEAT_COOL: 'heat_cool'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 26.0,
'min_temp': 18.0,
'swing_modes': list([
'off',
'horizontal',
'vertical',
'both',
]),
'target_temp_step': 0.5,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.testserial345',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'fujitsu_fglair',
'previous_unique_id': None,
'supported_features': <ClimateEntityFeature: 425>,
'translation_key': None,
'unique_id': 'testserial345',
'unit_of_measurement': None,
})
# ---
# name: test_entities[climate.testserial345-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 22.0,
'fan_mode': 'auto',
'fan_modes': list([
'low',
'medium',
'high',
'auto',
]),
'friendly_name': 'testserial345',
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.HEAT_COOL: 'heat_cool'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 26.0,
'min_temp': 18.0,
'supported_features': <ClimateEntityFeature: 425>,
'swing_mode': 'both',
'swing_modes': list([
'off',
'horizontal',
'vertical',
'both',
]),
'target_temp_step': 0.5,
'temperature': 21.0,
}),
'context': <ANY>,
'entity_id': 'climate.testserial345',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'cool',
})
# ---

View File

@ -0,0 +1,98 @@
"""Test for the climate entities of Fujitsu HVAC."""
from unittest.mock import AsyncMock
from syrupy import SnapshotAssertion
from homeassistant.components.climate import (
ATTR_FAN_MODE,
ATTR_HVAC_MODE,
ATTR_SWING_MODE,
ATTR_TEMPERATURE,
DOMAIN as CLIMATE_DOMAIN,
FAN_AUTO,
SERVICE_SET_FAN_MODE,
SERVICE_SET_HVAC_MODE,
SERVICE_SET_SWING_MODE,
SERVICE_SET_TEMPERATURE,
SWING_BOTH,
HVACMode,
)
from homeassistant.components.fujitsu_fglair.const import (
HA_TO_FUJI_FAN,
HA_TO_FUJI_HVAC,
HA_TO_FUJI_SWING,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import entity_id, setup_integration
from tests.common import MockConfigEntry, snapshot_platform
async def test_entities(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that coordinator returns the data we expect after the first refresh."""
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_set_attributes(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_ayla_api: AsyncMock,
mock_devices: list[AsyncMock],
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that setting the attributes calls the correct functions on the device."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
service_data={ATTR_HVAC_MODE: HVACMode.COOL},
target={ATTR_ENTITY_ID: entity_id(mock_devices[0])},
blocking=True,
)
mock_devices[0].async_set_op_mode.assert_called_once_with(
HA_TO_FUJI_HVAC[HVACMode.COOL]
)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_FAN_MODE,
service_data={ATTR_FAN_MODE: FAN_AUTO},
target={ATTR_ENTITY_ID: entity_id(mock_devices[0])},
blocking=True,
)
mock_devices[0].async_set_fan_speed.assert_called_once_with(
HA_TO_FUJI_FAN[FAN_AUTO]
)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_SWING_MODE,
service_data={ATTR_SWING_MODE: SWING_BOTH},
target={ATTR_ENTITY_ID: entity_id(mock_devices[0])},
blocking=True,
)
mock_devices[0].async_set_swing_mode.assert_called_once_with(
HA_TO_FUJI_SWING[SWING_BOTH]
)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
service_data={ATTR_TEMPERATURE: 23.0},
target={ATTR_ENTITY_ID: entity_id(mock_devices[0])},
blocking=True,
)
mock_devices[0].async_set_set_temp.assert_called_once_with(23.0)

View File

@ -0,0 +1,107 @@
"""Test the Fujitsu HVAC (based on Ayla IOT) config flow."""
from unittest.mock import AsyncMock
from ayla_iot_unofficial import AylaAuthError
import pytest
from homeassistant.components.fujitsu_fglair.const import CONF_EUROPE, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult, FlowResultType
from .conftest import TEST_PASSWORD, TEST_USERNAME
from tests.common import MockConfigEntry
async def _initial_step(hass: HomeAssistant) -> FlowResult:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
return await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: False,
},
)
async def test_full_flow(
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_ayla_api: AsyncMock
) -> None:
"""Test full config flow."""
result = await _initial_step(hass)
mock_ayla_api.async_sign_in.assert_called_once()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == f"FGLair ({TEST_USERNAME})"
assert result["data"] == {
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: False,
}
async def test_duplicate_entry(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that re-adding the same account fails."""
mock_config_entry.add_to_hass(hass)
result = await _initial_step(hass)
mock_ayla_api.async_sign_in.assert_not_called()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
@pytest.mark.parametrize(
("exception", "err_msg"),
[
(AylaAuthError, "invalid_auth"),
(TimeoutError, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_form_exceptions(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_ayla_api: AsyncMock,
exception: Exception,
err_msg: str,
) -> None:
"""Test we handle exceptions."""
mock_ayla_api.async_sign_in.side_effect = exception
result = await _initial_step(hass)
mock_ayla_api.async_sign_in.assert_called_once()
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": err_msg}
mock_ayla_api.async_sign_in.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: False,
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == f"FGLair ({TEST_USERNAME})"
assert result["data"] == {
CONF_USERNAME: TEST_USERNAME,
CONF_PASSWORD: TEST_PASSWORD,
CONF_EUROPE: False,
}

View File

@ -0,0 +1,128 @@
"""Test the initialization of fujitsu_fglair entities."""
from unittest.mock import AsyncMock
from ayla_iot_unofficial import AylaAuthError
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.fujitsu_fglair.const import API_REFRESH, DOMAIN
from homeassistant.const import STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import entity_id, setup_integration
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
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_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
async def test_one_device_disabled(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
mock_devices: list[AsyncMock],
mock_ayla_api: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that coordinator only updates devices that are currently listening."""
await setup_integration(hass, mock_config_entry)
for d in mock_devices:
d.async_update.assert_called_once()
d.reset_mock()
entity = entity_registry.async_get(
entity_registry.async_get_entity_id(
Platform.CLIMATE, DOMAIN, mock_devices[0].device_serial_number
)
)
entity_registry.async_update_entity(
entity.entity_id, disabled_by=er.RegistryEntryDisabler.USER
)
await hass.async_block_till_done()
freezer.tick(API_REFRESH)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == len(mock_devices) - 1
mock_devices[0].async_update.assert_not_called()
mock_devices[1].async_update.assert_called_once()