Skip to content

Commit

Permalink
Add vacuum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Oct 13, 2024
1 parent 0aec756 commit bd3eef9
Show file tree
Hide file tree
Showing 2 changed files with 296 additions and 13 deletions.
95 changes: 91 additions & 4 deletions tests/components/vacuum/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
"""Fixtures for Vacuum platform tests."""

from collections.abc import Generator
from unittest.mock import MagicMock

import pytest

from homeassistant.config_entries import ConfigFlow
from homeassistant.components.vacuum import DOMAIN as VACUUM_DOMAIN, VacuumEntityFeature
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from tests.common import mock_config_flow, mock_platform
from . import MockVacuum

from tests.common import (
MockConfigEntry,
MockModule,
MockPlatform,
mock_config_flow,
mock_integration,
mock_platform,
)

TEST_DOMAIN = "test"


class MockFlow(ConfigFlow):
Expand All @@ -17,7 +32,79 @@ class MockFlow(ConfigFlow):
@pytest.fixture
def config_flow_fixture(hass: HomeAssistant) -> Generator[None]:
"""Mock config flow."""
mock_platform(hass, "test.config_flow")
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")

with mock_config_flow("test", MockFlow):
with mock_config_flow(TEST_DOMAIN, MockFlow):
yield


@pytest.fixture(name="supported_features")
async def alarm_control_panel_supported_features() -> VacuumEntityFeature:
"""Return the supported features for the test alarm control panel entity."""
return (
VacuumEntityFeature.PAUSE
| VacuumEntityFeature.STOP
| VacuumEntityFeature.RETURN_HOME
| VacuumEntityFeature.FAN_SPEED
| VacuumEntityFeature.BATTERY
| VacuumEntityFeature.CLEAN_SPOT
| VacuumEntityFeature.MAP
| VacuumEntityFeature.STATE
| VacuumEntityFeature.START
)


@pytest.fixture(name="mock_vacuum_entity")
async def setup_vacuum_platform_test_entity(
hass: HomeAssistant,
config_flow_fixture: None,
entity_registry: er.EntityRegistry,
supported_features: VacuumEntityFeature,
) -> MagicMock:
"""Set up alarm control panel entity using an entity platform."""

async def async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
await hass.config_entries.async_forward_entry_setups(
config_entry, [VACUUM_DOMAIN]
)
return True

mock_integration(
hass,
MockModule(
TEST_DOMAIN,
async_setup_entry=async_setup_entry_init,
),
)

# Unnamed sensor without device class -> no name
entity = MockVacuum(
supported_features=supported_features,
)

async def async_setup_entry_platform(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up test vacuum platform via config entry."""
async_add_entities([entity])

mock_platform(
hass,
f"{TEST_DOMAIN}.{VACUUM_DOMAIN}",
MockPlatform(async_setup_entry=async_setup_entry_platform),
)

config_entry = MockConfigEntry(domain=TEST_DOMAIN)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get(entity.entity_id)
assert state is not None

return entity
Loading

0 comments on commit bd3eef9

Please sign in to comment.