Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic testing framework to LG ThinQ #127785

Merged
merged 16 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion tests/components/lg_thinq/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
"""Tests for the lgthinq integration."""
"""Tests for the LG ThinQ integration."""

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()
34 changes: 29 additions & 5 deletions tests/components/lg_thinq/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .const import MOCK_CONNECT_CLIENT_ID, MOCK_COUNTRY, MOCK_PAT, MOCK_UUID

from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, load_json_object_fixture


def mock_thinq_api_response(
Expand Down Expand Up @@ -45,6 +45,15 @@ def mock_config_entry() -> MockConfigEntry:
)


@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.lg_thinq.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry


@pytest.fixture
def mock_uuid() -> Generator[AsyncMock]:
"""Mock a uuid."""
Expand All @@ -59,22 +68,37 @@ def mock_uuid() -> Generator[AsyncMock]:


@pytest.fixture
def mock_thinq_api() -> Generator[AsyncMock]:
def mock_thinq_api(mock_thinq_mqtt_client: AsyncMock) -> Generator[AsyncMock]:
"""Mock a thinq api."""
with (
patch("thinqconnect.ThinQApi", autospec=True) as mock_api,
patch("homeassistant.components.lg_thinq.ThinQApi", autospec=True) as mock_api,
patch(
"homeassistant.components.lg_thinq.config_flow.ThinQApi",
new=mock_api,
),
):
thinq_api = mock_api.return_value
thinq_api.async_get_device_list = AsyncMock(
return_value=mock_thinq_api_response(status=200, body={})
thinq_api.async_get_device_list.return_value = [
load_json_object_fixture("air_conditioner/device.json", DOMAIN)
]
thinq_api.async_get_device_profile.return_value = load_json_object_fixture(
"air_conditioner/profile.json", DOMAIN
)
thinq_api.async_get_device_status.return_value = load_json_object_fixture(
"air_conditioner/status.json", DOMAIN
)
yield thinq_api


@pytest.fixture
def mock_thinq_mqtt_client() -> Generator[AsyncMock]:
"""Mock a thinq api."""
with patch(
"homeassistant.components.lg_thinq.mqtt.ThinQMQTTClient", autospec=True
) as mock_api:
yield mock_api


@pytest.fixture
def mock_invalid_thinq_api(mock_thinq_api: AsyncMock) -> AsyncMock:
"""Mock an invalid thinq api."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"deviceId": "MW2-2E247F93-B570-46A6-B827-920E9E10F966",
"deviceInfo": {
"deviceType": "DEVICE_AIR_CONDITIONER",
"modelName": "PAC_910604_WW",
"alias": "Test air conditioner",
"reportable": true
}
}
154 changes: 154 additions & 0 deletions tests/components/lg_thinq/fixtures/air_conditioner/profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"notification": {
"push": ["WATER_IS_FULL"]
},
"property": {
"airConJobMode": {
"currentJobMode": {
"mode": ["r", "w"],
"type": "enum",
"value": {
"r": ["AIR_CLEAN", "COOL", "AIR_DRY"],
"w": ["AIR_CLEAN", "COOL", "AIR_DRY"]
}
}
},
"airFlow": {
"windStrength": {
"mode": ["r", "w"],
"type": "enum",
"value": {
"r": ["LOW", "HIGH", "MID"],
"w": ["LOW", "HIGH", "MID"]
}
}
},
"airQualitySensor": {
"PM1": {
"mode": ["r"],
"type": "number"
},
"PM10": {
"mode": ["r"],
"type": "number"
},
"PM2": {
"mode": ["r"],
"type": "number"
},
"humidity": {
"mode": ["r"],
"type": "number"
},
"monitoringEnabled": {
"mode": ["r", "w"],
"type": "enum",
"value": {
"r": ["ON_WORKING", "ALWAYS"],
"w": ["ON_WORKING", "ALWAYS"]
}
},
"oder": {
"mode": ["r"],
"type": "number"
},
"totalPollution": {
"mode": ["r"],
"type": "number"
}
},
"operation": {
"airCleanOperationMode": {
"mode": ["w"],
"type": "enum",
"value": {
"w": ["START", "STOP"]
}
},
"airConOperationMode": {
"mode": ["r", "w"],
"type": "enum",
"value": {
"r": ["POWER_ON", "POWER_OFF"],
"w": ["POWER_ON", "POWER_OFF"]
}
}
},
"powerSave": {
"powerSaveEnabled": {
"mode": ["r", "w"],
"type": "boolean",
"value": {
"r": [false, true],
"w": [false, true]
}
}
},
"temperature": {
"coolTargetTemperature": {
"mode": ["w"],
"type": "range",
"value": {
"w": {
"max": 30,
"min": 18,
"step": 1
}
}
},
"currentTemperature": {
"mode": ["r"],
"type": "number"
},
"targetTemperature": {
"mode": ["r", "w"],
"type": "range",
"value": {
"r": {
"max": 30,
"min": 18,
"step": 1
},
"w": {
"max": 30,
"min": 18,
"step": 1
}
}
},
"unit": {
"mode": ["r"],
"type": "enum",
"value": {
"r": ["C", "F"]
}
}
},
"timer": {
"relativeHourToStart": {
"mode": ["r", "w"],
"type": "number"
},
"relativeHourToStop": {
"mode": ["r", "w"],
"type": "number"
},
"relativeMinuteToStart": {
"mode": ["r", "w"],
"type": "number"
},
"relativeMinuteToStop": {
"mode": ["r", "w"],
"type": "number"
},
"absoluteHourToStart": {
"mode": ["r", "w"],
"type": "number"
},
"absoluteMinuteToStart": {
"mode": ["r", "w"],
"type": "number"
}
}
}
}
43 changes: 43 additions & 0 deletions tests/components/lg_thinq/fixtures/air_conditioner/status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"airConJobMode": {
"currentJobMode": "COOL"
},
"airFlow": {
"windStrength": "MID"
},
"airQualitySensor": {
"PM1": 12,
"PM10": 7,
"PM2": 24,
"humidity": 40,
"monitoringEnabled": "ON_WORKING",
"totalPollution": 3,
"totalPollutionLevel": "GOOD"
},
"filterInfo": {
"filterLifetime": 540,
"usedTime": 180
},
"operation": {
"airConOperationMode": "POWER_ON"
},
"powerSave": {
"powerSaveEnabled": false
},
"sleepTimer": {
"relativeStopTimer": "UNSET"
},
"temperature": {
"currentTemperature": 25,
"targetTemperature": 19,
"unit": "C"
},
"timer": {
"relativeStartTimer": "UNSET",
"relativeStopTimer": "UNSET",
"absoluteStartTimer": "SET",
"absoluteStopTimer": "UNSET",
"absoluteHourToStart": 13,
"absoluteMinuteToStart": 14
}
}
86 changes: 86 additions & 0 deletions tests/components/lg_thinq/snapshots/test_climate.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# serializer version: 1
# name: test_all_entities[climate.test_air_conditioner-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'fan_modes': list([
'low',
'high',
'mid',
]),
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 30,
'min_temp': 18,
'preset_modes': list([
'air_clean',
]),
'target_temp_step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.test_air_conditioner',
'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': 'lg_thinq',
'previous_unique_id': None,
'supported_features': <ClimateEntityFeature: 409>,
'translation_key': <ExtendedProperty.CLIMATE_AIR_CONDITIONER: 'climate_air_conditioner'>,
'unique_id': 'MW2-2E247F93-B570-46A6-B827-920E9E10F966_climate_air_conditioner',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[climate.test_air_conditioner-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_humidity': 40,
'current_temperature': 25,
'fan_mode': 'mid',
'fan_modes': list([
'low',
'high',
'mid',
]),
'friendly_name': 'Test air conditioner',
'hvac_modes': list([
<HVACMode.OFF: 'off'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 30,
'min_temp': 18,
'preset_mode': None,
'preset_modes': list([
'air_clean',
]),
'supported_features': <ClimateEntityFeature: 409>,
'target_temp_step': 1,
'temperature': 19,
}),
'context': <ANY>,
'entity_id': 'climate.test_air_conditioner',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'cool',
})
# ---
Loading