Skip to content

Commit

Permalink
Add missing hass type hint in component tests (d) (#124074)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 18, 2024
1 parent 49c5933 commit 7d326ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/components/denonavr/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def client_fixture():
yield mock_client_class.return_value


async def setup_denonavr(hass):
async def setup_denonavr(hass: HomeAssistant) -> None:
"""Initialize media_player for tests."""
entry_data = {
CONF_HOST: TEST_HOST,
Expand Down
15 changes: 12 additions & 3 deletions tests/components/derivative/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from datetime import timedelta
from math import sin
import random
from typing import Any

from freezegun import freeze_time

from homeassistant.components.derivative.const import DOMAIN
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.setup import async_setup_component
import homeassistant.util.dt as dt_util
Expand Down Expand Up @@ -49,7 +50,9 @@ async def test_state(hass: HomeAssistant) -> None:
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 = {
"platform": "derivative",
"name": "power",
Expand All @@ -67,7 +70,13 @@ async def _setup_sensor(hass, config):
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."""
config, entity_id = await _setup_sensor(hass, config)

Expand Down
4 changes: 1 addition & 3 deletions tests/components/duckdns/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from homeassistant.components import duckdns
from homeassistant.components.duckdns import async_track_time_interval_backoff
from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow

Expand All @@ -21,8 +20,7 @@
INTERVAL = duckdns.INTERVAL


@bind_hass
async def async_set_txt(hass, txt):
async def async_set_txt(hass: HomeAssistant, txt: str | None) -> None:
"""Set the txt record. Pass in None to remove it.
This is a legacy helper method. Do not use it for new tests.
Expand Down
9 changes: 6 additions & 3 deletions tests/components/dynalite/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from unittest.mock import AsyncMock, Mock, call, patch

from dynalite_devices_lib.dynalitebase import DynaliteBaseDevice

from homeassistant.components import dynalite
from homeassistant.const import ATTR_SERVICE
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry

Expand All @@ -21,14 +24,14 @@ def create_mock_device(platform, spec):
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."""
conf_entries = hass.config_entries.async_entries(dynalite.DOMAIN)
assert len(conf_entries) == 1
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."""
host = "1.2.3.4"
entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host})
Expand All @@ -45,7 +48,7 @@ async def create_entity_from_device(hass, device):
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."""
for cur_item in services:
service = cur_item[ATTR_SERVICE]
Expand Down
26 changes: 18 additions & 8 deletions tests/components/dynalite/test_cover.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test Dynalite cover."""

from collections.abc import Callable
from unittest.mock import Mock

from dynalite_devices_lib.cover import DynaliteTimeCoverWithTiltDevice
from dynalite_devices_lib.dynalitebase import DynaliteBaseDevice
import pytest

from homeassistant.components.cover import (
Expand Down Expand Up @@ -36,7 +38,7 @@


@pytest.fixture
def mock_device():
def mock_device() -> Mock:
"""Mock a Dynalite device."""
mock_dev = create_mock_device("cover", DynaliteTimeCoverWithTiltDevice)
mock_dev.device_class = CoverDeviceClass.BLIND.value
Expand All @@ -54,7 +56,7 @@ def mock_init_level(target):
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."""
await create_entity_from_device(hass, mock_device)
entity_state = hass.states.get("cover.name")
Expand Down Expand Up @@ -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."""
mock_device.has_tilt = False
await create_entity_from_device(hass, mock_device)
Expand All @@ -106,8 +108,14 @@ async def test_cover_without_tilt(hass: HomeAssistant, mock_device) -> None:


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."""
device.is_closing = closing
device.is_opening = opening
Expand All @@ -118,7 +126,7 @@ async def check_cover_position(
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."""
update_func = await create_entity_from_device(hass, mock_device)
await check_cover_position(
Expand All @@ -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."""
mock_restore_cache(
hass,
Expand All @@ -147,7 +155,9 @@ async def test_cover_restore_state(hass: HomeAssistant, mock_device) -> None:
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."""
mock_restore_cache(
hass,
Expand Down

0 comments on commit 7d326ff

Please sign in to comment.