mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add missing hass type hint in component tests (d) (#124074)
This commit is contained in:
parent
49c59339d9
commit
7d326ff076
@ -60,7 +60,7 @@ def client_fixture():
|
|||||||
yield mock_client_class.return_value
|
yield mock_client_class.return_value
|
||||||
|
|
||||||
|
|
||||||
async def setup_denonavr(hass):
|
async def setup_denonavr(hass: HomeAssistant) -> None:
|
||||||
"""Initialize media_player for tests."""
|
"""Initialize media_player for tests."""
|
||||||
entry_data = {
|
entry_data = {
|
||||||
CONF_HOST: TEST_HOST,
|
CONF_HOST: TEST_HOST,
|
||||||
|
@ -3,12 +3,13 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from math import sin
|
from math import sin
|
||||||
import random
|
import random
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from homeassistant.components.derivative.const import DOMAIN
|
from homeassistant.components.derivative.const import DOMAIN
|
||||||
from homeassistant.const import UnitOfPower, UnitOfTime
|
from homeassistant.const import UnitOfPower, UnitOfTime
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, State
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@ -49,7 +50,9 @@ async def test_state(hass: HomeAssistant) -> None:
|
|||||||
assert state.attributes.get("unit_of_measurement") == "kW"
|
assert state.attributes.get("unit_of_measurement") == "kW"
|
||||||
|
|
||||||
|
|
||||||
async def _setup_sensor(hass, config):
|
async def _setup_sensor(
|
||||||
|
hass: HomeAssistant, config: dict[str, Any]
|
||||||
|
) -> tuple[dict[str, Any], str]:
|
||||||
default_config = {
|
default_config = {
|
||||||
"platform": "derivative",
|
"platform": "derivative",
|
||||||
"name": "power",
|
"name": "power",
|
||||||
@ -67,7 +70,13 @@ async def _setup_sensor(hass, config):
|
|||||||
return config, entity_id
|
return config, entity_id
|
||||||
|
|
||||||
|
|
||||||
async def setup_tests(hass, config, times, values, expected_state):
|
async def setup_tests(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: dict[str, Any],
|
||||||
|
times: list[int],
|
||||||
|
values: list[float],
|
||||||
|
expected_state: float,
|
||||||
|
) -> State:
|
||||||
"""Test derivative sensor state."""
|
"""Test derivative sensor state."""
|
||||||
config, entity_id = await _setup_sensor(hass, config)
|
config, entity_id = await _setup_sensor(hass, config)
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ import pytest
|
|||||||
from homeassistant.components import duckdns
|
from homeassistant.components import duckdns
|
||||||
from homeassistant.components.duckdns import async_track_time_interval_backoff
|
from homeassistant.components.duckdns import async_track_time_interval_backoff
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.loader import bind_hass
|
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
@ -21,8 +20,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
INTERVAL = duckdns.INTERVAL
|
INTERVAL = duckdns.INTERVAL
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
async def async_set_txt(hass: HomeAssistant, txt: str | None) -> None:
|
||||||
async def async_set_txt(hass, txt):
|
|
||||||
"""Set the txt record. Pass in None to remove it.
|
"""Set the txt record. Pass in None to remove it.
|
||||||
|
|
||||||
This is a legacy helper method. Do not use it for new tests.
|
This is a legacy helper method. Do not use it for new tests.
|
||||||
|
@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock, Mock, call, patch
|
from unittest.mock import AsyncMock, Mock, call, patch
|
||||||
|
|
||||||
|
from dynalite_devices_lib.dynalitebase import DynaliteBaseDevice
|
||||||
|
|
||||||
from homeassistant.components import dynalite
|
from homeassistant.components import dynalite
|
||||||
from homeassistant.const import ATTR_SERVICE
|
from homeassistant.const import ATTR_SERVICE
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -21,14 +24,14 @@ def create_mock_device(platform, spec):
|
|||||||
return device
|
return device
|
||||||
|
|
||||||
|
|
||||||
async def get_entry_id_from_hass(hass):
|
async def get_entry_id_from_hass(hass: HomeAssistant) -> str:
|
||||||
"""Get the config entry id from hass."""
|
"""Get the config entry id from hass."""
|
||||||
conf_entries = hass.config_entries.async_entries(dynalite.DOMAIN)
|
conf_entries = hass.config_entries.async_entries(dynalite.DOMAIN)
|
||||||
assert len(conf_entries) == 1
|
assert len(conf_entries) == 1
|
||||||
return conf_entries[0].entry_id
|
return conf_entries[0].entry_id
|
||||||
|
|
||||||
|
|
||||||
async def create_entity_from_device(hass, device):
|
async def create_entity_from_device(hass: HomeAssistant, device: DynaliteBaseDevice):
|
||||||
"""Set up the component and platform and create a light based on the device provided."""
|
"""Set up the component and platform and create a light based on the device provided."""
|
||||||
host = "1.2.3.4"
|
host = "1.2.3.4"
|
||||||
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host})
|
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host})
|
||||||
@ -45,7 +48,7 @@ async def create_entity_from_device(hass, device):
|
|||||||
return mock_dyn_dev.mock_calls[1][2]["update_device_func"]
|
return mock_dyn_dev.mock_calls[1][2]["update_device_func"]
|
||||||
|
|
||||||
|
|
||||||
async def run_service_tests(hass, device, platform, services):
|
async def run_service_tests(hass: HomeAssistant, device, platform, services):
|
||||||
"""Run a series of service calls and check that the entity and device behave correctly."""
|
"""Run a series of service calls and check that the entity and device behave correctly."""
|
||||||
for cur_item in services:
|
for cur_item in services:
|
||||||
service = cur_item[ATTR_SERVICE]
|
service = cur_item[ATTR_SERVICE]
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
"""Test Dynalite cover."""
|
"""Test Dynalite cover."""
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from unittest.mock import Mock
|
from unittest.mock import Mock
|
||||||
|
|
||||||
from dynalite_devices_lib.cover import DynaliteTimeCoverWithTiltDevice
|
from dynalite_devices_lib.cover import DynaliteTimeCoverWithTiltDevice
|
||||||
|
from dynalite_devices_lib.dynalitebase import DynaliteBaseDevice
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.cover import (
|
from homeassistant.components.cover import (
|
||||||
@ -36,7 +38,7 @@ from tests.common import mock_restore_cache
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_device():
|
def mock_device() -> Mock:
|
||||||
"""Mock a Dynalite device."""
|
"""Mock a Dynalite device."""
|
||||||
mock_dev = create_mock_device("cover", DynaliteTimeCoverWithTiltDevice)
|
mock_dev = create_mock_device("cover", DynaliteTimeCoverWithTiltDevice)
|
||||||
mock_dev.device_class = CoverDeviceClass.BLIND.value
|
mock_dev.device_class = CoverDeviceClass.BLIND.value
|
||||||
@ -54,7 +56,7 @@ def mock_device():
|
|||||||
return mock_dev
|
return mock_dev
|
||||||
|
|
||||||
|
|
||||||
async def test_cover_setup(hass: HomeAssistant, mock_device) -> None:
|
async def test_cover_setup(hass: HomeAssistant, mock_device: Mock) -> None:
|
||||||
"""Test a successful setup."""
|
"""Test a successful setup."""
|
||||||
await create_entity_from_device(hass, mock_device)
|
await create_entity_from_device(hass, mock_device)
|
||||||
entity_state = hass.states.get("cover.name")
|
entity_state = hass.states.get("cover.name")
|
||||||
@ -93,7 +95,7 @@ async def test_cover_setup(hass: HomeAssistant, mock_device) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_cover_without_tilt(hass: HomeAssistant, mock_device) -> None:
|
async def test_cover_without_tilt(hass: HomeAssistant, mock_device: Mock) -> None:
|
||||||
"""Test a cover with no tilt."""
|
"""Test a cover with no tilt."""
|
||||||
mock_device.has_tilt = False
|
mock_device.has_tilt = False
|
||||||
await create_entity_from_device(hass, mock_device)
|
await create_entity_from_device(hass, mock_device)
|
||||||
@ -106,8 +108,14 @@ async def test_cover_without_tilt(hass: HomeAssistant, mock_device) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def check_cover_position(
|
async def check_cover_position(
|
||||||
hass, update_func, device, closing, opening, closed, expected
|
hass: HomeAssistant,
|
||||||
):
|
update_func: Callable[[DynaliteBaseDevice | None], None],
|
||||||
|
device: Mock,
|
||||||
|
closing: bool,
|
||||||
|
opening: bool,
|
||||||
|
closed: bool,
|
||||||
|
expected: str,
|
||||||
|
) -> None:
|
||||||
"""Check that a given position behaves correctly."""
|
"""Check that a given position behaves correctly."""
|
||||||
device.is_closing = closing
|
device.is_closing = closing
|
||||||
device.is_opening = opening
|
device.is_opening = opening
|
||||||
@ -118,7 +126,7 @@ async def check_cover_position(
|
|||||||
assert entity_state.state == expected
|
assert entity_state.state == expected
|
||||||
|
|
||||||
|
|
||||||
async def test_cover_positions(hass: HomeAssistant, mock_device) -> None:
|
async def test_cover_positions(hass: HomeAssistant, mock_device: Mock) -> None:
|
||||||
"""Test that the state updates in the various positions."""
|
"""Test that the state updates in the various positions."""
|
||||||
update_func = await create_entity_from_device(hass, mock_device)
|
update_func = await create_entity_from_device(hass, mock_device)
|
||||||
await check_cover_position(
|
await check_cover_position(
|
||||||
@ -135,7 +143,7 @@ async def test_cover_positions(hass: HomeAssistant, mock_device) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_cover_restore_state(hass: HomeAssistant, mock_device) -> None:
|
async def test_cover_restore_state(hass: HomeAssistant, mock_device: Mock) -> None:
|
||||||
"""Test restore from cache."""
|
"""Test restore from cache."""
|
||||||
mock_restore_cache(
|
mock_restore_cache(
|
||||||
hass,
|
hass,
|
||||||
@ -147,7 +155,9 @@ async def test_cover_restore_state(hass: HomeAssistant, mock_device) -> None:
|
|||||||
assert entity_state.state == STATE_OPEN
|
assert entity_state.state == STATE_OPEN
|
||||||
|
|
||||||
|
|
||||||
async def test_cover_restore_state_bad_cache(hass: HomeAssistant, mock_device) -> None:
|
async def test_cover_restore_state_bad_cache(
|
||||||
|
hass: HomeAssistant, mock_device: Mock
|
||||||
|
) -> None:
|
||||||
"""Test restore from a cache without the attribute."""
|
"""Test restore from a cache without the attribute."""
|
||||||
mock_restore_cache(
|
mock_restore_cache(
|
||||||
hass,
|
hass,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user