From 7d326ff076dab194105850828694d3112cc3339e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 18 Aug 2024 18:57:29 +0200 Subject: [PATCH] Add missing hass type hint in component tests (d) (#124074) --- .../components/denonavr/test_media_player.py | 2 +- tests/components/derivative/test_sensor.py | 15 ++++++++--- tests/components/duckdns/test_init.py | 4 +-- tests/components/dynalite/common.py | 9 ++++--- tests/components/dynalite/test_cover.py | 26 +++++++++++++------ 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/tests/components/denonavr/test_media_player.py b/tests/components/denonavr/test_media_player.py index c294c44951875f..6550b31b1f97da 100644 --- a/tests/components/denonavr/test_media_player.py +++ b/tests/components/denonavr/test_media_player.py @@ -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, diff --git a/tests/components/derivative/test_sensor.py b/tests/components/derivative/test_sensor.py index df050c58f10668..3646340cac30f4 100644 --- a/tests/components/derivative/test_sensor.py +++ b/tests/components/derivative/test_sensor.py @@ -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 @@ -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", @@ -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) diff --git a/tests/components/duckdns/test_init.py b/tests/components/duckdns/test_init.py index c06add7156a692..313cc91aa1806d 100644 --- a/tests/components/duckdns/test_init.py +++ b/tests/components/duckdns/test_init.py @@ -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 @@ -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. diff --git a/tests/components/dynalite/common.py b/tests/components/dynalite/common.py index 91458b0aaff936..640b6b3e24f1d1 100644 --- a/tests/components/dynalite/common.py +++ b/tests/components/dynalite/common.py @@ -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 @@ -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}) @@ -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] diff --git a/tests/components/dynalite/test_cover.py b/tests/components/dynalite/test_cover.py index c43d349d184fe0..930318978fc7f7 100644 --- a/tests/components/dynalite/test_cover.py +++ b/tests/components/dynalite/test_cover.py @@ -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 ( @@ -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 @@ -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") @@ -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) @@ -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 @@ -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( @@ -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, @@ -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,