Remove rainbird yaml config test fixtures (#103607)

This commit is contained in:
Allen Porter 2023-11-07 14:24:34 -08:00 committed by GitHub
parent c29b0cd05b
commit e8c568a243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 97 additions and 149 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Awaitable, Callable, Generator
from http import HTTPStatus from http import HTTPStatus
from typing import Any from typing import Any
from unittest.mock import patch from unittest.mock import patch
@ -17,13 +16,10 @@ from homeassistant.components.rainbird.const import (
) )
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse
ComponentSetup = Callable[[], Awaitable[bool]]
HOST = "example.com" HOST = "example.com"
URL = "http://example.com/stick" URL = "http://example.com/stick"
PASSWORD = "password" PASSWORD = "password"
@ -79,12 +75,6 @@ def platforms() -> list[Platform]:
return [] return []
@pytest.fixture
def yaml_config() -> dict[str, Any]:
"""Fixture for configuration.yaml."""
return {}
@pytest.fixture @pytest.fixture
async def config_entry_unique_id() -> str: async def config_entry_unique_id() -> str:
"""Fixture for serial number used in the config entry.""" """Fixture for serial number used in the config entry."""
@ -122,22 +112,15 @@ async def add_config_entry(
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
@pytest.fixture @pytest.fixture(autouse=True)
async def setup_integration( def setup_platforms(
hass: HomeAssistant, hass: HomeAssistant,
platforms: list[str], platforms: list[str],
yaml_config: dict[str, Any], ) -> None:
) -> Generator[ComponentSetup, None, None]: """Fixture for setting up the default platforms."""
"""Fixture for setting up the component."""
with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms): with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms):
yield
async def func() -> bool:
result = await async_setup_component(hass, DOMAIN, yaml_config)
await hass.async_block_till_done()
return result
yield func
def rainbird_response(data: str) -> bytes: def rainbird_response(data: str) -> bytes:

View File

@ -3,12 +3,14 @@
import pytest import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import RAIN_SENSOR_OFF, RAIN_SENSOR_ON, SERIAL_NUMBER, ComponentSetup from .conftest import RAIN_SENSOR_OFF, RAIN_SENSOR_ON, SERIAL_NUMBER
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMockResponse
@ -18,21 +20,27 @@ def platforms() -> list[Platform]:
return [Platform.BINARY_SENSOR] return [Platform.BINARY_SENSOR]
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> list[Platform]:
"""Fixture to setup the config entry."""
await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
@pytest.mark.parametrize( @pytest.mark.parametrize(
("rain_response", "expected_state"), ("rain_response", "expected_state"),
[(RAIN_SENSOR_OFF, "off"), (RAIN_SENSOR_ON, "on")], [(RAIN_SENSOR_OFF, "off"), (RAIN_SENSOR_ON, "on")],
) )
async def test_rainsensor( async def test_rainsensor(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
expected_state: bool, expected_state: bool,
) -> None: ) -> None:
"""Test rainsensor binary sensor.""" """Test rainsensor binary sensor."""
assert await setup_integration()
rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor") rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor")
assert rainsensor is not None assert rainsensor is not None
assert rainsensor.state == expected_state assert rainsensor.state == expected_state
@ -53,14 +61,10 @@ async def test_rainsensor(
) )
async def test_unique_id( async def test_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
entity_unique_id: str, entity_unique_id: str,
) -> None: ) -> None:
"""Test rainsensor binary sensor.""" """Test rainsensor binary sensor."""
assert await setup_integration()
rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor") rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor")
assert rainsensor is not None assert rainsensor is not None
assert rainsensor.attributes == { assert rainsensor.attributes == {
@ -83,14 +87,11 @@ async def test_unique_id(
) )
async def test_no_unique_id( async def test_no_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test rainsensor binary sensor with no unique id.""" """Test rainsensor binary sensor with no unique id."""
assert await setup_integration()
rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor") rainsensor = hass.states.get("binary_sensor.rain_bird_controller_rainsensor")
assert rainsensor is not None assert rainsensor is not None
assert ( assert (

View File

@ -12,12 +12,14 @@ from aiohttp import ClientSession
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import ComponentSetup, mock_response, mock_response_error from .conftest import mock_response, mock_response_error
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMockResponse
TEST_ENTITY = "calendar.rain_bird_controller" TEST_ENTITY = "calendar.rain_bird_controller"
@ -80,6 +82,15 @@ def platforms() -> list[str]:
return [Platform.CALENDAR] return [Platform.CALENDAR]
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> list[Platform]:
"""Fixture to setup the config entry."""
await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def set_time_zone(hass: HomeAssistant): def set_time_zone(hass: HomeAssistant):
"""Set the time zone for the tests.""" """Set the time zone for the tests."""
@ -121,13 +132,9 @@ def get_events_fixture(
@pytest.mark.freeze_time("2023-01-21 09:32:00") @pytest.mark.freeze_time("2023-01-21 09:32:00")
async def test_get_events( async def test_get_events(hass: HomeAssistant, get_events: GetEventsFn) -> None:
hass: HomeAssistant, setup_integration: ComponentSetup, get_events: GetEventsFn
) -> None:
"""Test calendar event fetching APIs.""" """Test calendar event fetching APIs."""
assert await setup_integration()
events = await get_events("2023-01-20T00:00:00Z", "2023-02-05T00:00:00Z") events = await get_events("2023-01-20T00:00:00Z", "2023-02-05T00:00:00Z")
assert events == [ assert events == [
# Monday # Monday
@ -158,31 +165,34 @@ async def test_get_events(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("freeze_time", "expected_state"), ("freeze_time", "expected_state", "setup_config_entry"),
[ [
( (
datetime.datetime(2023, 1, 23, 3, 50, tzinfo=ZoneInfo("America/Regina")), datetime.datetime(2023, 1, 23, 3, 50, tzinfo=ZoneInfo("America/Regina")),
"off", "off",
None,
), ),
( (
datetime.datetime(2023, 1, 23, 4, 30, tzinfo=ZoneInfo("America/Regina")), datetime.datetime(2023, 1, 23, 4, 30, tzinfo=ZoneInfo("America/Regina")),
"on", "on",
None,
), ),
], ],
) )
async def test_event_state( async def test_event_state(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
get_events: GetEventsFn, get_events: GetEventsFn,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
freeze_time: datetime.datetime, freeze_time: datetime.datetime,
expected_state: str, expected_state: str,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test calendar upcoming event state.""" """Test calendar upcoming event state."""
freezer.move_to(freeze_time) freezer.move_to(freeze_time)
assert await setup_integration() await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
state = hass.states.get(TEST_ENTITY) state = hass.states.get(TEST_ENTITY)
assert state is not None assert state is not None
@ -213,13 +223,10 @@ async def test_event_state(
) )
async def test_calendar_not_supported_by_device( async def test_calendar_not_supported_by_device(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
has_entity: bool, has_entity: bool,
) -> None: ) -> None:
"""Test calendar upcoming event state.""" """Test calendar upcoming event state."""
assert await setup_integration()
state = hass.states.get(TEST_ENTITY) state = hass.states.get(TEST_ENTITY)
assert (state is not None) == has_entity assert (state is not None) == has_entity
@ -229,7 +236,6 @@ async def test_calendar_not_supported_by_device(
) )
async def test_no_schedule( async def test_no_schedule(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
get_events: GetEventsFn, get_events: GetEventsFn,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
hass_client: Callable[..., Awaitable[ClientSession]], hass_client: Callable[..., Awaitable[ClientSession]],
@ -237,8 +243,6 @@ async def test_no_schedule(
"""Test calendar error when fetching the calendar.""" """Test calendar error when fetching the calendar."""
responses.extend([mock_response_error(HTTPStatus.BAD_GATEWAY)]) # Arbitrary error responses.extend([mock_response_error(HTTPStatus.BAD_GATEWAY)]) # Arbitrary error
assert await setup_integration()
state = hass.states.get(TEST_ENTITY) state = hass.states.get(TEST_ENTITY)
assert state.state == "unavailable" assert state.state == "unavailable"
assert state.attributes == { assert state.attributes == {
@ -260,13 +264,10 @@ async def test_no_schedule(
) )
async def test_program_schedule_disabled( async def test_program_schedule_disabled(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
get_events: GetEventsFn, get_events: GetEventsFn,
) -> None: ) -> None:
"""Test calendar when the program is disabled with no upcoming events.""" """Test calendar when the program is disabled with no upcoming events."""
assert await setup_integration()
events = await get_events("2023-01-20T00:00:00Z", "2023-02-05T00:00:00Z") events = await get_events("2023-01-20T00:00:00Z", "2023-02-05T00:00:00Z")
assert events == [] assert events == []
@ -286,14 +287,11 @@ async def test_program_schedule_disabled(
) )
async def test_no_unique_id( async def test_no_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
get_events: GetEventsFn, get_events: GetEventsFn,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test calendar entity with no unique id.""" """Test calendar entity with no unique id."""
assert await setup_integration()
state = hass.states.get(TEST_ENTITY) state = hass.states.get(TEST_ENTITY)
assert state is not None assert state is not None
assert state.attributes.get("friendly_name") == "Rain Bird Controller" assert state.attributes.get("friendly_name") == "Rain Bird Controller"

View File

@ -24,10 +24,10 @@ from .conftest import (
SERIAL_RESPONSE, SERIAL_RESPONSE,
URL, URL,
ZERO_SERIAL_RESPONSE, ZERO_SERIAL_RESPONSE,
ComponentSetup,
mock_response, mock_response,
) )
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse
@ -129,17 +129,14 @@ async def test_controller_flow(
) )
async def test_multiple_config_entries( async def test_multiple_config_entries(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup, config_entry: MockConfigEntry,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
config_flow_responses: list[AiohttpClientMockResponse], config_flow_responses: list[AiohttpClientMockResponse],
expected_config_entry: dict[str, Any] | None, expected_config_entry: dict[str, Any] | None,
) -> None: ) -> None:
"""Test setting up multiple config entries that refer to different devices.""" """Test setting up multiple config entries that refer to different devices."""
assert await setup_integration() await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
responses.clear() responses.clear()
responses.extend(config_flow_responses) responses.extend(config_flow_responses)
@ -177,16 +174,13 @@ async def test_multiple_config_entries(
) )
async def test_duplicate_config_entries( async def test_duplicate_config_entries(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup, config_entry: MockConfigEntry,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
config_flow_responses: list[AiohttpClientMockResponse], config_flow_responses: list[AiohttpClientMockResponse],
) -> None: ) -> None:
"""Test that a device can not be registered twice.""" """Test that a device can not be registered twice."""
assert await setup_integration() await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
responses.clear() responses.clear()
responses.extend(config_flow_responses) responses.extend(config_flow_responses)

View File

@ -6,31 +6,30 @@ from http import HTTPStatus
import pytest import pytest
from homeassistant.components.rainbird import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .conftest import ( from .conftest import (
CONFIG_ENTRY_DATA, CONFIG_ENTRY_DATA,
MODEL_AND_VERSION_RESPONSE, MODEL_AND_VERSION_RESPONSE,
ComponentSetup,
mock_response, mock_response,
mock_response_error, mock_response_error,
) )
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMockResponse
@pytest.mark.parametrize( @pytest.mark.parametrize(
("yaml_config", "config_entry_data", "initial_response"), ("config_entry_data", "initial_response"),
[ [
({}, CONFIG_ENTRY_DATA, None), (CONFIG_ENTRY_DATA, None),
], ],
ids=["config_entry"], ids=["config_entry"],
) )
async def test_init_success( async def test_init_success(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup, config_entry: MockConfigEntry,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
initial_response: AiohttpClientMockResponse | None, initial_response: AiohttpClientMockResponse | None,
) -> None: ) -> None:
@ -38,49 +37,42 @@ async def test_init_success(
if initial_response: if initial_response:
responses.insert(0, initial_response) responses.insert(0, initial_response)
assert await setup_integration() await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
entries = hass.config_entries.async_entries(DOMAIN) await hass.config_entries.async_unload(config_entry.entry_id)
assert len(entries) == 1
assert entries[0].state == ConfigEntryState.LOADED
await hass.config_entries.async_unload(entries[0].entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entries[0].state is ConfigEntryState.NOT_LOADED assert config_entry.state is ConfigEntryState.NOT_LOADED
@pytest.mark.parametrize( @pytest.mark.parametrize(
("yaml_config", "config_entry_data", "responses", "config_entry_states"), ("config_entry_data", "responses", "config_entry_state"),
[ [
( (
{},
CONFIG_ENTRY_DATA, CONFIG_ENTRY_DATA,
[mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE)], [mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE)],
[ConfigEntryState.SETUP_RETRY], ConfigEntryState.SETUP_RETRY,
), ),
( (
{},
CONFIG_ENTRY_DATA, CONFIG_ENTRY_DATA,
[mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR)], [mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR)],
[ConfigEntryState.SETUP_RETRY], ConfigEntryState.SETUP_RETRY,
), ),
( (
{},
CONFIG_ENTRY_DATA, CONFIG_ENTRY_DATA,
[ [
mock_response(MODEL_AND_VERSION_RESPONSE), mock_response(MODEL_AND_VERSION_RESPONSE),
mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE), mock_response_error(HTTPStatus.SERVICE_UNAVAILABLE),
], ],
[ConfigEntryState.SETUP_RETRY], ConfigEntryState.SETUP_RETRY,
), ),
( (
{},
CONFIG_ENTRY_DATA, CONFIG_ENTRY_DATA,
[ [
mock_response(MODEL_AND_VERSION_RESPONSE), mock_response(MODEL_AND_VERSION_RESPONSE),
mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR), mock_response_error(HTTPStatus.INTERNAL_SERVER_ERROR),
], ],
[ConfigEntryState.SETUP_RETRY], ConfigEntryState.SETUP_RETRY,
), ),
], ],
ids=[ ids=[
@ -92,13 +84,10 @@ async def test_init_success(
) )
async def test_communication_failure( async def test_communication_failure(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup, config_entry: MockConfigEntry,
config_entry_states: list[ConfigEntryState], config_entry_state: list[ConfigEntryState],
) -> None: ) -> None:
"""Test unable to talk to device on startup, which fails setup.""" """Test unable to talk to device on startup, which fails setup."""
assert await setup_integration() await config_entry.async_setup(hass)
assert config_entry.state == config_entry_state
assert [
entry.state for entry in hass.config_entries.async_entries(DOMAIN)
] == config_entry_states

View File

@ -6,7 +6,7 @@ import pytest
from homeassistant.components import number from homeassistant.components import number
from homeassistant.components.rainbird import DOMAIN from homeassistant.components.rainbird import DOMAIN
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -17,11 +17,11 @@ from .conftest import (
RAIN_DELAY, RAIN_DELAY,
RAIN_DELAY_OFF, RAIN_DELAY_OFF,
SERIAL_NUMBER, SERIAL_NUMBER,
ComponentSetup,
mock_response, mock_response,
mock_response_error, mock_response_error,
) )
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
@ -31,20 +31,26 @@ def platforms() -> list[str]:
return [Platform.NUMBER] return [Platform.NUMBER]
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> list[Platform]:
"""Fixture to setup the config entry."""
await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
@pytest.mark.parametrize( @pytest.mark.parametrize(
("rain_delay_response", "expected_state"), ("rain_delay_response", "expected_state"),
[(RAIN_DELAY, "16"), (RAIN_DELAY_OFF, "0")], [(RAIN_DELAY, "16"), (RAIN_DELAY_OFF, "0")],
) )
async def test_number_values( async def test_number_values(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
expected_state: str, expected_state: str,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test number platform.""" """Test number platform."""
assert await setup_integration()
raindelay = hass.states.get("number.rain_bird_controller_rain_delay") raindelay = hass.states.get("number.rain_bird_controller_rain_delay")
assert raindelay is not None assert raindelay is not None
assert raindelay.state == expected_state assert raindelay.state == expected_state
@ -74,14 +80,11 @@ async def test_number_values(
) )
async def test_unique_id( async def test_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
entity_unique_id: str, entity_unique_id: str,
) -> None: ) -> None:
"""Test number platform.""" """Test number platform."""
assert await setup_integration()
raindelay = hass.states.get("number.rain_bird_controller_rain_delay") raindelay = hass.states.get("number.rain_bird_controller_rain_delay")
assert raindelay is not None assert raindelay is not None
assert ( assert (
@ -95,15 +98,12 @@ async def test_unique_id(
async def test_set_value( async def test_set_value(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[str], responses: list[str],
config_entry: ConfigEntry, config_entry: ConfigEntry,
) -> None: ) -> None:
"""Test setting the rain delay number.""" """Test setting the rain delay number."""
assert await setup_integration()
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
device = device_registry.async_get_device(identifiers={(DOMAIN, SERIAL_NUMBER)}) device = device_registry.async_get_device(identifiers={(DOMAIN, SERIAL_NUMBER)})
assert device assert device
@ -136,7 +136,6 @@ async def test_set_value(
) )
async def test_set_value_error( async def test_set_value_error(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[str], responses: list[str],
config_entry: ConfigEntry, config_entry: ConfigEntry,
@ -145,8 +144,6 @@ async def test_set_value_error(
) -> None: ) -> None:
"""Test an error while talking to the device.""" """Test an error while talking to the device."""
assert await setup_integration()
aioclient_mock.mock_calls.clear() aioclient_mock.mock_calls.clear()
responses.append(mock_response_error(status=status)) responses.append(mock_response_error(status=status))
@ -172,13 +169,10 @@ async def test_set_value_error(
) )
async def test_no_unique_id( async def test_no_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test number platform with no unique id.""" """Test number platform with no unique id."""
assert await setup_integration()
raindelay = hass.states.get("number.rain_bird_controller_rain_delay") raindelay = hass.states.get("number.rain_bird_controller_rain_delay")
assert raindelay is not None assert raindelay is not None
assert ( assert (

View File

@ -3,11 +3,14 @@
import pytest import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .conftest import CONFIG_ENTRY_DATA, RAIN_DELAY, RAIN_DELAY_OFF, ComponentSetup from .conftest import CONFIG_ENTRY_DATA, RAIN_DELAY, RAIN_DELAY_OFF
from tests.common import MockConfigEntry
@pytest.fixture @pytest.fixture
@ -16,20 +19,26 @@ def platforms() -> list[str]:
return [Platform.SENSOR] return [Platform.SENSOR]
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> list[Platform]:
"""Fixture to setup the config entry."""
await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
@pytest.mark.parametrize( @pytest.mark.parametrize(
("rain_delay_response", "expected_state"), ("rain_delay_response", "expected_state"),
[(RAIN_DELAY, "16"), (RAIN_DELAY_OFF, "0")], [(RAIN_DELAY, "16"), (RAIN_DELAY_OFF, "0")],
) )
async def test_sensors( async def test_sensors(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
expected_state: str, expected_state: str,
) -> None: ) -> None:
"""Test sensor platform.""" """Test sensor platform."""
assert await setup_integration()
raindelay = hass.states.get("sensor.rain_bird_controller_raindelay") raindelay = hass.states.get("sensor.rain_bird_controller_raindelay")
assert raindelay is not None assert raindelay is not None
assert raindelay.state == expected_state assert raindelay.state == expected_state
@ -66,14 +75,11 @@ async def test_sensors(
) )
async def test_sensor_no_unique_id( async def test_sensor_no_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
config_entry_unique_id: str | None, config_entry_unique_id: str | None,
) -> None: ) -> None:
"""Test sensor platform with no unique id.""" """Test sensor platform with no unique id."""
assert await setup_integration()
raindelay = hass.states.get("sensor.rain_bird_controller_raindelay") raindelay = hass.states.get("sensor.rain_bird_controller_raindelay")
assert raindelay is not None assert raindelay is not None
assert raindelay.attributes.get("friendly_name") == "Rain Bird Controller Raindelay" assert raindelay.attributes.get("friendly_name") == "Rain Bird Controller Raindelay"

View File

@ -5,6 +5,7 @@ from http import HTTPStatus
import pytest import pytest
from homeassistant.components.rainbird import DOMAIN from homeassistant.components.rainbird import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -21,11 +22,11 @@ from .conftest import (
ZONE_3_ON_RESPONSE, ZONE_3_ON_RESPONSE,
ZONE_5_ON_RESPONSE, ZONE_5_ON_RESPONSE,
ZONE_OFF_RESPONSE, ZONE_OFF_RESPONSE,
ComponentSetup,
mock_response, mock_response,
mock_response_error, mock_response_error,
) )
from tests.common import MockConfigEntry
from tests.components.switch import common as switch_common from tests.components.switch import common as switch_common
from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse from tests.test_util.aiohttp import AiohttpClientMocker, AiohttpClientMockResponse
@ -36,18 +37,24 @@ def platforms() -> list[str]:
return [Platform.SWITCH] return [Platform.SWITCH]
@pytest.fixture(autouse=True)
async def setup_config_entry(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> list[Platform]:
"""Fixture to setup the config entry."""
await config_entry.async_setup(hass)
assert config_entry.state == ConfigEntryState.LOADED
@pytest.mark.parametrize( @pytest.mark.parametrize(
"stations_response", "stations_response",
[EMPTY_STATIONS_RESPONSE], [EMPTY_STATIONS_RESPONSE],
) )
async def test_no_zones( async def test_no_zones(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
) -> None: ) -> None:
"""Test case where listing stations returns no stations.""" """Test case where listing stations returns no stations."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_1") zone = hass.states.get("switch.rain_bird_sprinkler_1")
assert zone is None assert zone is None
@ -58,13 +65,10 @@ async def test_no_zones(
) )
async def test_zones( async def test_zones(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test switch platform with fake data that creates 7 zones with one enabled.""" """Test switch platform with fake data that creates 7 zones with one enabled."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_1") zone = hass.states.get("switch.rain_bird_sprinkler_1")
assert zone is not None assert zone is not None
assert zone.state == "off" assert zone.state == "off"
@ -110,14 +114,11 @@ async def test_zones(
async def test_switch_on( async def test_switch_on(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
) -> None: ) -> None:
"""Test turning on irrigation switch.""" """Test turning on irrigation switch."""
assert await setup_integration()
# Initially all zones are off. Pick zone3 as an arbitrary to assert # Initially all zones are off. Pick zone3 as an arbitrary to assert
# state, then update below as a switch. # state, then update below as a switch.
zone = hass.states.get("switch.rain_bird_sprinkler_3") zone = hass.states.get("switch.rain_bird_sprinkler_3")
@ -149,14 +150,11 @@ async def test_switch_on(
) )
async def test_switch_off( async def test_switch_off(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
) -> None: ) -> None:
"""Test turning off irrigation switch.""" """Test turning off irrigation switch."""
assert await setup_integration()
# Initially the test zone is on # Initially the test zone is on
zone = hass.states.get("switch.rain_bird_sprinkler_3") zone = hass.states.get("switch.rain_bird_sprinkler_3")
assert zone is not None assert zone is not None
@ -182,15 +180,12 @@ async def test_switch_off(
async def test_irrigation_service( async def test_irrigation_service(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
api_responses: list[str], api_responses: list[str],
) -> None: ) -> None:
"""Test calling the irrigation service.""" """Test calling the irrigation service."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_3") zone = hass.states.get("switch.rain_bird_sprinkler_3")
assert zone is not None assert zone is not None
assert zone.state == "off" assert zone.state == "off"
@ -219,10 +214,9 @@ async def test_irrigation_service(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("yaml_config", "config_entry_data"), ("config_entry_data"),
[ [
( (
{},
{ {
"host": HOST, "host": HOST,
"password": PASSWORD, "password": PASSWORD,
@ -232,17 +226,15 @@ async def test_irrigation_service(
"1": "Garden Sprinkler", "1": "Garden Sprinkler",
"2": "Back Yard", "2": "Back Yard",
}, },
}, }
) )
], ],
) )
async def test_yaml_imported_config( async def test_yaml_imported_config(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
) -> None: ) -> None:
"""Test a config entry that was previously imported from yaml.""" """Test a config entry that was previously imported from yaml."""
assert await setup_integration()
assert hass.states.get("switch.garden_sprinkler") assert hass.states.get("switch.garden_sprinkler")
assert not hass.states.get("switch.rain_bird_sprinkler_1") assert not hass.states.get("switch.rain_bird_sprinkler_1")
@ -260,7 +252,6 @@ async def test_yaml_imported_config(
) )
async def test_switch_error( async def test_switch_error(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
status: HTTPStatus, status: HTTPStatus,
@ -268,8 +259,6 @@ async def test_switch_error(
) -> None: ) -> None:
"""Test an error talking to the device.""" """Test an error talking to the device."""
assert await setup_integration()
aioclient_mock.mock_calls.clear() aioclient_mock.mock_calls.clear()
responses.append(mock_response_error(status=status)) responses.append(mock_response_error(status=status))
@ -292,15 +281,12 @@ async def test_switch_error(
) )
async def test_no_unique_id( async def test_no_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test an irrigation switch with no unique id.""" """Test an irrigation switch with no unique id."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_3") zone = hass.states.get("switch.rain_bird_sprinkler_3")
assert zone is not None assert zone is not None
assert zone.attributes.get("friendly_name") == "Rain Bird Sprinkler 3" assert zone.attributes.get("friendly_name") == "Rain Bird Sprinkler 3"
@ -321,7 +307,6 @@ async def test_no_unique_id(
) )
async def test_has_unique_id( async def test_has_unique_id(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
responses: list[AiohttpClientMockResponse], responses: list[AiohttpClientMockResponse],
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
@ -329,8 +314,6 @@ async def test_has_unique_id(
) -> None: ) -> None:
"""Test an irrigation switch with no unique id.""" """Test an irrigation switch with no unique id."""
assert await setup_integration()
zone = hass.states.get("switch.rain_bird_sprinkler_3") zone = hass.states.get("switch.rain_bird_sprinkler_3")
assert zone is not None assert zone is not None
assert zone.attributes.get("friendly_name") == "Rain Bird Sprinkler 3" assert zone.attributes.get("friendly_name") == "Rain Bird Sprinkler 3"