init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
"""Tests for the Modern Forms integration."""
from collections.abc import Callable
import json
from aiomodernforms.const import COMMAND_QUERY_STATIC_DATA
from homeassistant.components.modern_forms.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_MAC, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse
async def modern_forms_call_mock(method, url, data):
"""Set up the basic returns based on info or status request."""
if COMMAND_QUERY_STATIC_DATA in data:
fixture = "modern_forms/device_info.json"
else:
fixture = "modern_forms/device_status.json"
return AiohttpClientMockResponse(
method=method, url=url, json=json.loads(load_fixture(fixture))
)
async def modern_forms_no_light_call_mock(method, url, data):
"""Set up the basic returns based on info or status request."""
if COMMAND_QUERY_STATIC_DATA in data:
fixture = "modern_forms/device_info_no_light.json"
else:
fixture = "modern_forms/device_status_no_light.json"
return AiohttpClientMockResponse(
method=method, url=url, json=json.loads(load_fixture(fixture))
)
async def modern_forms_timers_set_mock(method, url, data):
"""Set up the basic returns based on info or status request."""
if COMMAND_QUERY_STATIC_DATA in data:
fixture = "modern_forms/device_info.json"
else:
fixture = "modern_forms/device_status_timers_active.json"
return AiohttpClientMockResponse(
method=method, url=url, json=json.loads(load_fixture(fixture))
)
async def init_integration(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
rgbw: bool = False,
skip_setup: bool = False,
mock_type: Callable = modern_forms_call_mock,
) -> MockConfigEntry:
"""Set up the Modern Forms integration in Home Assistant."""
aioclient_mock.post(
"http://192.168.1.123:80/mf",
side_effect=mock_type,
headers={"Content-Type": CONTENT_TYPE_JSON},
)
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.1.123", CONF_MAC: "AA:BB:CC:DD:EE:FF"},
unique_id="AA:BB:CC:DD:EE:FF",
)
entry.add_to_hass(hass)
if not skip_setup:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry

View File

@@ -0,0 +1,15 @@
{
"clientId": "MF_000000000000",
"mac": "AA:BB:CC:DD:EE:FF",
"lightType": "F6IN-120V-R1-30",
"fanType": "1818-56",
"fanMotorType": "DC125X25",
"productionLotNumber": "",
"productSku": "",
"owner": "someone@somewhere.com",
"federatedIdentity": "us-east-1:f3da237b-c19c-4f61-b387-0e6dde2e470b",
"deviceName": "ModernFormsFan",
"firmwareVersion": "01.03.0025",
"mainMcuFirmwareVersion": "01.03.3008",
"firmwareUrl": ""
}

View File

@@ -0,0 +1,14 @@
{
"clientId": "MF_000000000000",
"mac": "AA:BB:CC:DD:EE:FF",
"fanType": "1818-56",
"fanMotorType": "DC125X25",
"productionLotNumber": "",
"productSku": "",
"owner": "someone@somewhere.com",
"federatedIdentity": "us-east-1:f3da237b-c19c-4f61-b387-0e6dde2e470b",
"deviceName": "ModernFormsFan",
"firmwareVersion": "01.03.0025",
"mainMcuFirmwareVersion": "01.03.3008",
"firmwareUrl": ""
}

View File

@@ -0,0 +1,17 @@
{
"adaptiveLearning": false,
"awayModeEnabled": false,
"clientId": "MF_000000000000",
"decommission": false,
"factoryReset": false,
"fanDirection": "forward",
"fanOn": true,
"fanSleepTimer": 0,
"fanSpeed": 3,
"lightBrightness": 50,
"lightOn": true,
"lightSleepTimer": 0,
"resetRfPairList": false,
"rfPairModeActive": false,
"schedule": ""
}

View File

@@ -0,0 +1,14 @@
{
"adaptiveLearning": false,
"awayModeEnabled": false,
"clientId": "MF_000000000000",
"decommission": false,
"factoryReset": false,
"fanDirection": "forward",
"fanOn": true,
"fanSleepTimer": 0,
"fanSpeed": 3,
"resetRfPairList": false,
"rfPairModeActive": false,
"schedule": ""
}

View File

@@ -0,0 +1,17 @@
{
"adaptiveLearning": false,
"awayModeEnabled": false,
"clientId": "MF_000000000000",
"decommission": false,
"factoryReset": false,
"fanDirection": "forward",
"fanOn": true,
"fanSleepTimer": 9999999999,
"fanSpeed": 3,
"lightBrightness": 50,
"lightOn": true,
"lightSleepTimer": 9999999999,
"resetRfPairList": false,
"rfPairModeActive": false,
"schedule": ""
}

View File

@@ -0,0 +1,52 @@
# serializer version: 1
# name: test_entry_diagnostics
dict({
'config_entry': dict({
'data': dict({
'host': '192.168.1.123',
'mac': '**REDACTED**',
}),
'disabled_by': None,
'discovery_keys': dict({
}),
'domain': 'modern_forms',
'minor_version': 1,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Mock Title',
'unique_id': 'AA:BB:CC:DD:EE:FF',
'version': 1,
}),
'device': dict({
'info': dict({
'client_id': 'MF_000000000000',
'device_name': 'ModernFormsFan',
'fan_motor_type': 'DC125X25',
'fan_type': '1818-56',
'federated_identity': 'us-east-1:f3da237b-c19c-4f61-b387-0e6dde2e470b',
'firmware_url': '',
'firmware_version': '01.03.0025',
'light_type': 'F6IN-120V-R1-30',
'mac_address': '**REDACTED**',
'main_mcu_firmware_version': '01.03.3008',
'owner': '**REDACTED**',
'product_sku': '',
'production_lot_number': '',
}),
'status': dict({
'adaptive_learning_enabled': False,
'away_mode_enabled': False,
'fan_direction': 'forward',
'fan_on': True,
'fan_sleep_timer': 0,
'fan_speed': 3,
'light_brightness': 50,
'light_on': True,
'light_sleep_timer': 0,
}),
}),
})
# ---

View File

@@ -0,0 +1,45 @@
"""Tests for the Modern Forms sensor platform."""
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.modern_forms.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_binary_sensors(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the creation and values of the Modern Forms sensors."""
entity_registry.async_get_or_create(
BINARY_SENSOR_DOMAIN,
DOMAIN,
"AA:BB:CC:DD:EE:FF_light_sleep_timer_active",
suggested_object_id="modernformsfan_light_sleep_timer_active",
disabled_by=None,
)
entity_registry.async_get_or_create(
BINARY_SENSOR_DOMAIN,
DOMAIN,
"AA:BB:CC:DD:EE:FF_fan_sleep_timer_active",
suggested_object_id="modernformsfan_fan_sleep_timer_active",
disabled_by=None,
)
await init_integration(hass, aioclient_mock)
# Light timer remaining time
state = hass.states.get("binary_sensor.modernformsfan_light_sleep_timer_active")
assert state
assert state.state == "off"
# Fan timer remaining time
state = hass.states.get("binary_sensor.modernformsfan_fan_sleep_timer_active")
assert state
assert state.state == "off"

View File

@@ -0,0 +1,235 @@
"""Tests for the Modern Forms config flow."""
from ipaddress import ip_address
from unittest.mock import MagicMock, patch
import aiohttp
from aiomodernforms import ModernFormsConnectionError
from homeassistant.components import zeroconf
from homeassistant.components.modern_forms.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import init_integration
from tests.common import load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_full_user_flow_implementation(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the full manual user flow from start to finish."""
aioclient_mock.post(
"http://192.168.1.123:80/mf",
text=load_fixture("modern_forms/device_info.json"),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
assert result.get("step_id") == "user"
assert result.get("type") is FlowResultType.FORM
with patch(
"homeassistant.components.modern_forms.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "192.168.1.123"}
)
assert result2.get("title") == "ModernFormsFan"
assert "data" in result2
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert result2["data"][CONF_HOST] == "192.168.1.123"
assert result2["data"][CONF_MAC] == "AA:BB:CC:DD:EE:FF"
assert len(mock_setup_entry.mock_calls) == 1
async def test_full_zeroconf_flow_implementation(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the full manual user flow from start to finish."""
aioclient_mock.post(
"http://192.168.1.123:80/mf",
text=load_fixture("modern_forms/device_info.json"),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("192.168.1.123"),
ip_addresses=[ip_address("192.168.1.123")],
hostname="example.local.",
name="mock_name",
port=None,
properties={},
type="mock_type",
),
)
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert result.get("description_placeholders") == {CONF_NAME: "example"}
assert result.get("step_id") == "zeroconf_confirm"
assert result.get("type") is FlowResultType.FORM
flow = hass.config_entries.flow._progress[flows[0]["flow_id"]]
assert flow.host == "192.168.1.123"
assert flow.name == "example"
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result2.get("title") == "example"
assert result2.get("type") is FlowResultType.CREATE_ENTRY
assert "data" in result2
assert result2["data"][CONF_HOST] == "192.168.1.123"
assert result2["data"][CONF_MAC] == "AA:BB:CC:DD:EE:FF"
@patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_connection_error(
update_mock: MagicMock, hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we show user form on Modern Forms connection error."""
aioclient_mock.post("http://example.com/mf", exc=aiohttp.ClientError)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "example.com"},
)
assert result.get("type") is FlowResultType.FORM
assert result.get("step_id") == "user"
assert result.get("errors") == {"base": "cannot_connect"}
@patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_zeroconf_connection_error(
update_mock: MagicMock, hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow on Modern Forms connection error."""
aioclient_mock.post("http://192.168.1.123/mf", exc=aiohttp.ClientError)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("192.168.1.123"),
ip_addresses=[ip_address("192.168.1.123")],
hostname="example.local.",
name="mock_name",
port=None,
properties={},
type="mock_type",
),
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "cannot_connect"
@patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_zeroconf_confirm_connection_error(
update_mock: MagicMock, hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow on Modern Forms connection error."""
aioclient_mock.post("http://192.168.1.123:80/mf", exc=aiohttp.ClientError)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": SOURCE_ZEROCONF,
CONF_HOST: "example.com",
CONF_NAME: "test",
},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("192.168.1.123"),
ip_addresses=[ip_address("192.168.1.123")],
hostname="example.com.",
name="mock_name",
port=None,
properties={},
type="mock_type",
),
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "cannot_connect"
async def test_user_device_exists_abort(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow if Modern Forms device already configured."""
aioclient_mock.post(
"http://192.168.1.123:80/mf",
text=load_fixture("modern_forms/device_info.json"),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
await init_integration(hass, aioclient_mock, skip_setup=True)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_HOST: "192.168.1.123"},
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "already_configured"
async def test_zeroconf_with_mac_device_exists_abort(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow if a Modern Forms device already configured."""
await init_integration(hass, aioclient_mock, skip_setup=True)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
ip_address=ip_address("192.168.1.123"),
ip_addresses=[ip_address("192.168.1.123")],
hostname="example.local.",
name="mock_name",
port=None,
properties={CONF_MAC: "AA:BB:CC:DD:EE:FF"},
type="mock_type",
),
)
assert result.get("type") is FlowResultType.ABORT
assert result.get("reason") == "already_configured"

View File

@@ -0,0 +1,26 @@
"""Tests for the Modern Forms diagnostics platform."""
from syrupy import SnapshotAssertion
from syrupy.filters import props
from homeassistant.core import HomeAssistant
from . import init_integration
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.test_util.aiohttp import AiohttpClientMocker
from tests.typing import ClientSessionGenerator
async def test_entry_diagnostics(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
) -> None:
"""Test the creation and values of the Modern Forms fans."""
entry = await init_integration(hass, aioclient_mock)
result = await get_diagnostics_for_config_entry(hass, hass_client, entry)
assert result == snapshot(exclude=props("created_at", "modified_at", "entry_id"))

View File

@@ -0,0 +1,233 @@
"""Tests for the Modern Forms fan platform."""
from unittest.mock import patch
from aiomodernforms import ModernFormsConnectionError
import pytest
from homeassistant.components.fan import (
ATTR_DIRECTION,
ATTR_PERCENTAGE,
DIRECTION_FORWARD,
DIRECTION_REVERSE,
DOMAIN as FAN_DOMAIN,
SERVICE_SET_DIRECTION,
SERVICE_SET_PERCENTAGE,
)
from homeassistant.components.modern_forms.const import (
ATTR_SLEEP_TIME,
DOMAIN,
SERVICE_CLEAR_FAN_SLEEP_TIMER,
SERVICE_SET_FAN_SLEEP_TIMER,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_fan_state(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the creation and values of the Modern Forms fans."""
await init_integration(hass, aioclient_mock)
state = hass.states.get("fan.modernformsfan_fan")
assert state
assert state.attributes.get(ATTR_PERCENTAGE) == 50
assert state.attributes.get(ATTR_DIRECTION) == DIRECTION_FORWARD
assert state.state == STATE_ON
entry = entity_registry.async_get("fan.modernformsfan_fan")
assert entry
assert entry.unique_id == "AA:BB:CC:DD:EE:FF"
async def test_change_state(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of state of the Modern Forms fan."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "fan.modernformsfan_fan"},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(
on=False,
)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "fan.modernformsfan_fan",
ATTR_PERCENTAGE: 100,
},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(on=True, speed=6)
async def test_sleep_timer_services(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of state of the Modern Forms segments."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
DOMAIN,
SERVICE_SET_FAN_SLEEP_TIMER,
{ATTR_ENTITY_ID: "fan.modernformsfan_fan", ATTR_SLEEP_TIME: 1},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(sleep=60)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
DOMAIN,
SERVICE_CLEAR_FAN_SLEEP_TIMER,
{ATTR_ENTITY_ID: "fan.modernformsfan_fan"},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(sleep=0)
async def test_change_direction(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of state of the Modern Forms segments."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_SET_DIRECTION,
{
ATTR_ENTITY_ID: "fan.modernformsfan_fan",
ATTR_DIRECTION: DIRECTION_REVERSE,
},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(
direction=DIRECTION_REVERSE,
)
async def test_set_percentage(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of percentage for the Modern Forms fan."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_SET_PERCENTAGE,
{
ATTR_ENTITY_ID: "fan.modernformsfan_fan",
ATTR_PERCENTAGE: 100,
},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(
on=True,
speed=6,
)
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.fan") as fan_mock:
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_SET_PERCENTAGE,
{
ATTR_ENTITY_ID: "fan.modernformsfan_fan",
ATTR_PERCENTAGE: 0,
},
blocking=True,
)
await hass.async_block_till_done()
fan_mock.assert_called_once_with(on=False)
async def test_fan_error(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the Modern Forms fans."""
await init_integration(hass, aioclient_mock)
aioclient_mock.clear_requests()
aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "fan.modernformsfan_fan"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("fan.modernformsfan_fan")
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
async def test_fan_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error handling of the Moder Forms fans."""
await init_integration(hass, aioclient_mock)
with (
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.fan",
side_effect=ModernFormsConnectionError,
),
):
await hass.services.async_call(
FAN_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "fan.modernformsfan_fan"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("fan.modernformsfan_fan")
assert state.state == STATE_UNAVAILABLE

View File

@@ -0,0 +1,54 @@
"""Tests for the Modern Forms integration."""
from unittest.mock import MagicMock, patch
from aiomodernforms import ModernFormsConnectionError
from homeassistant.components.modern_forms.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration, modern_forms_no_light_call_mock
from tests.test_util.aiohttp import AiohttpClientMocker
@patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update",
side_effect=ModernFormsConnectionError,
)
async def test_config_entry_not_ready(
mock_update: MagicMock, hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the Modern Forms configuration entry not ready."""
entry = await init_integration(hass, aioclient_mock)
assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_unload_config_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the Modern Forms configuration entry unloading."""
entry = await init_integration(hass, aioclient_mock)
assert hass.data[DOMAIN]
await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert not hass.data.get(DOMAIN)
async def test_fan_only_device(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test we set unique ID if not set yet."""
await init_integration(
hass, aioclient_mock, mock_type=modern_forms_no_light_call_mock
)
fan_entry = entity_registry.async_get("fan.modernformsfan_fan")
assert fan_entry
light_entry = entity_registry.async_get("light.modernformsfan_light")
assert light_entry is None

View File

@@ -0,0 +1,161 @@
"""Tests for the Modern Forms light platform."""
from unittest.mock import patch
from aiomodernforms import ModernFormsConnectionError
import pytest
from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN
from homeassistant.components.modern_forms.const import (
ATTR_SLEEP_TIME,
DOMAIN,
SERVICE_CLEAR_LIGHT_SLEEP_TIMER,
SERVICE_SET_LIGHT_SLEEP_TIMER,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_light_state(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the creation and values of the Modern Forms lights."""
await init_integration(hass, aioclient_mock)
state = hass.states.get("light.modernformsfan_light")
assert state
assert state.attributes.get(ATTR_BRIGHTNESS) == 128
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "ModernFormsFan Light"
assert state.state == STATE_ON
entry = entity_registry.async_get("light.modernformsfan_light")
assert entry
assert entry.unique_id == "AA:BB:CC:DD:EE:FF"
async def test_change_state(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of state of the Modern Forms segments."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.modernformsfan_light"},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(
on=False,
)
with patch("aiomodernforms.ModernFormsDevice.light") as light_mock:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.modernformsfan_light", ATTR_BRIGHTNESS: 255},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(on=True, brightness=100)
async def test_sleep_timer_services(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the change of state of the Modern Forms segments."""
await init_integration(hass, aioclient_mock)
with patch("aiomodernforms.ModernFormsDevice.light") as light_mock:
await hass.services.async_call(
DOMAIN,
SERVICE_SET_LIGHT_SLEEP_TIMER,
{ATTR_ENTITY_ID: "light.modernformsfan_light", ATTR_SLEEP_TIME: 1},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(sleep=60)
with patch("aiomodernforms.ModernFormsDevice.light") as light_mock:
await hass.services.async_call(
DOMAIN,
SERVICE_CLEAR_LIGHT_SLEEP_TIMER,
{ATTR_ENTITY_ID: "light.modernformsfan_light"},
blocking=True,
)
await hass.async_block_till_done()
light_mock.assert_called_once_with(sleep=0)
async def test_light_error(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the Modern Forms lights."""
await init_integration(hass, aioclient_mock)
aioclient_mock.clear_requests()
aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.modernformsfan_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.modernformsfan_light")
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
async def test_light_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error handling of the Moder Forms lights."""
await init_integration(hass, aioclient_mock)
with (
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.light",
side_effect=ModernFormsConnectionError,
),
):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.modernformsfan_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.modernformsfan_light")
assert state.state == STATE_UNAVAILABLE

View File

@@ -0,0 +1,53 @@
"""Tests for the Modern Forms sensor platform."""
from datetime import datetime
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import ATTR_DEVICE_CLASS
from homeassistant.core import HomeAssistant
from . import init_integration, modern_forms_timers_set_mock
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_sensors(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the creation and values of the Modern Forms sensors."""
# await init_integration(hass, aioclient_mock)
await init_integration(hass, aioclient_mock)
# Light timer remaining time
state = hass.states.get("sensor.modernformsfan_light_sleep_time")
assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert state.state == "unknown"
# Fan timer remaining time
state = hass.states.get("sensor.modernformsfan_fan_sleep_time")
assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
assert state.state == "unknown"
async def test_active_sensors(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the creation and values of the Modern Forms sensors."""
# await init_integration(hass, aioclient_mock)
await init_integration(hass, aioclient_mock, mock_type=modern_forms_timers_set_mock)
# Light timer remaining time
state = hass.states.get("sensor.modernformsfan_light_sleep_time")
assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
datetime.fromisoformat(state.state)
# Fan timer remaining time
state = hass.states.get("sensor.modernformsfan_fan_sleep_time")
assert state
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.TIMESTAMP
datetime.fromisoformat(state.state)

View File

@@ -0,0 +1,153 @@
"""Tests for the Modern Forms switch platform."""
from unittest.mock import patch
from aiomodernforms import ModernFormsConnectionError
import pytest
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_switch_state(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
) -> None:
"""Test the creation and values of the Modern Forms switches."""
await init_integration(hass, aioclient_mock)
state = hass.states.get("switch.modernformsfan_away_mode")
assert state
assert state.state == STATE_OFF
entry = entity_registry.async_get("switch.modernformsfan_away_mode")
assert entry
assert entry.unique_id == "AA:BB:CC:DD:EE:FF_away_mode"
state = hass.states.get("switch.modernformsfan_adaptive_learning")
assert state
assert state.state == STATE_OFF
entry = entity_registry.async_get("switch.modernformsfan_adaptive_learning")
assert entry
assert entry.unique_id == "AA:BB:CC:DD:EE:FF_adaptive_learning"
async def test_switch_change_state(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the change of state of the Modern Forms switches."""
await init_integration(hass, aioclient_mock)
# Away Mode
with patch("aiomodernforms.ModernFormsDevice.away") as away_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.modernformsfan_away_mode"},
blocking=True,
)
await hass.async_block_till_done()
away_mock.assert_called_once_with(away=True)
with patch("aiomodernforms.ModernFormsDevice.away") as away_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.modernformsfan_away_mode"},
blocking=True,
)
await hass.async_block_till_done()
away_mock.assert_called_once_with(away=False)
# Adaptive Learning
with patch(
"aiomodernforms.ModernFormsDevice.adaptive_learning"
) as adaptive_learning_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.modernformsfan_adaptive_learning"},
blocking=True,
)
await hass.async_block_till_done()
adaptive_learning_mock.assert_called_once_with(adaptive_learning=True)
with patch(
"aiomodernforms.ModernFormsDevice.adaptive_learning"
) as adaptive_learning_mock:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.modernformsfan_adaptive_learning"},
blocking=True,
)
await hass.async_block_till_done()
adaptive_learning_mock.assert_called_once_with(adaptive_learning=False)
async def test_switch_error(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the Modern Forms switches."""
await init_integration(hass, aioclient_mock)
aioclient_mock.clear_requests()
aioclient_mock.post("http://192.168.1.123:80/mf", text="", status=400)
with patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.modernformsfan_away_mode"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.modernformsfan_away_mode")
assert state.state == STATE_OFF
assert "Invalid response from API" in caplog.text
async def test_switch_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error handling of the Modern Forms switches."""
await init_integration(hass, aioclient_mock)
with (
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.update"
),
patch(
"homeassistant.components.modern_forms.coordinator.ModernFormsDevice.away",
side_effect=ModernFormsConnectionError,
),
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.modernformsfan_away_mode"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.modernformsfan_away_mode")
assert state.state == STATE_UNAVAILABLE