Skip to content

Commit

Permalink
Add typing to tests with single hass argument (home-assistant#87631)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Feb 7, 2023
1 parent fe9f682 commit 59ca778
Show file tree
Hide file tree
Showing 33 changed files with 104 additions and 75 deletions.
5 changes: 3 additions & 2 deletions tests/auth/permissions/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
compile_entities,
)
from homeassistant.auth.permissions.models import PermissionLookup
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.entity_registry import RegistryEntry

Expand Down Expand Up @@ -149,7 +150,7 @@ def test_entities_all_control() -> None:
assert compiled("switch.kitchen", "control") is True


def test_entities_device_id_boolean(hass):
def test_entities_device_id_boolean(hass: HomeAssistant) -> None:
"""Test entity ID policy applying control on device id."""
entity_registry = mock_registry(
hass,
Expand Down Expand Up @@ -189,7 +190,7 @@ def test_entities_areas_true() -> None:
assert compiled("light.kitchen", "read") is True


def test_entities_areas_area_true(hass):
def test_entities_areas_area_true(hass: HomeAssistant) -> None:
"""Test entity ID policy for areas with specific area."""
entity_registry = mock_registry(
hass,
Expand Down
4 changes: 2 additions & 2 deletions tests/components/alexa/test_smart_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import homeassistant.components.vacuum as vacuum
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import STATE_UNKNOWN, UnitOfTemperature
from homeassistant.core import Context
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers import entityfilter
from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
Expand Down Expand Up @@ -54,7 +54,7 @@ async def mock_stream(hass):
await hass.async_block_till_done()


def test_create_api_message_defaults(hass):
def test_create_api_message_defaults(hass: HomeAssistant) -> None:
"""Create an API message response of a request with defaults."""
request = get_new_request("Alexa.PowerController", "TurnOn", "switch#xy")
directive_header = request["directive"]["header"]
Expand Down
5 changes: 3 additions & 2 deletions tests/components/demo/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import METRIC_SYSTEM

Expand All @@ -57,7 +58,7 @@ async def setup_demo_climate(hass):
await hass.async_block_till_done()


def test_setup_params(hass):
def test_setup_params(hass: HomeAssistant) -> None:
"""Test the initial parameters."""
state = hass.states.get(ENTITY_CLIMATE)
assert state.state == HVACMode.COOL
Expand All @@ -78,7 +79,7 @@ def test_setup_params(hass):
]


def test_default_setup_params(hass):
def test_default_setup_params(hass: HomeAssistant) -> None:
"""Test the setup with default parameters."""
state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get(ATTR_MIN_TEMP) == 7
Expand Down
5 changes: 3 additions & 2 deletions tests/components/demo/test_humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

ENTITY_DEHUMIDIFIER = "humidifier.dehumidifier"
Expand All @@ -37,14 +38,14 @@ async def setup_demo_humidifier(hass):
await hass.async_block_till_done()


def test_setup_params(hass):
def test_setup_params(hass: HomeAssistant) -> None:
"""Test the initial parameters."""
state = hass.states.get(ENTITY_DEHUMIDIFIER)
assert state.state == STATE_ON
assert state.attributes.get(ATTR_HUMIDITY) == 54


def test_default_setup_params(hass):
def test_default_setup_params(hass: HomeAssistant) -> None:
"""Test the setup with default parameters."""
state = hass.states.get(ENTITY_DEHUMIDIFIER)
assert state.attributes.get(ATTR_MIN_HUMIDITY) == 0
Expand Down
5 changes: 3 additions & 2 deletions tests/components/demo/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
NumberMode,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_MODE
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

ENTITY_VOLUME = "number.volume"
Expand All @@ -28,13 +29,13 @@ async def setup_demo_number(hass):
await hass.async_block_till_done()


def test_setup_params(hass):
def test_setup_params(hass: HomeAssistant) -> None:
"""Test the initial parameters."""
state = hass.states.get(ENTITY_VOLUME)
assert state.state == "42.0"


def test_default_setup_params(hass):
def test_default_setup_params(hass: HomeAssistant) -> None:
"""Test the setup with default parameters."""
state = hass.states.get(ENTITY_VOLUME)
assert state.attributes.get(ATTR_MIN) == 0.0
Expand Down
5 changes: 3 additions & 2 deletions tests/components/demo/test_siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

ENTITY_SIREN = "siren.siren"
Expand All @@ -30,14 +31,14 @@ async def setup_demo_siren(hass):
await hass.async_block_till_done()


def test_setup_params(hass):
def test_setup_params(hass: HomeAssistant) -> None:
"""Test the initial parameters."""
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
assert ATTR_AVAILABLE_TONES not in state.attributes


def test_all_setup_params(hass):
def test_all_setup_params(hass: HomeAssistant) -> None:
"""Test the setup with all parameters."""
state = hass.states.get(ENTITY_SIREN_WITH_ALL_FEATURES)
assert state.attributes.get(ATTR_AVAILABLE_TONES) == ["fire", "alarm"]
Expand Down
3 changes: 2 additions & 1 deletion tests/components/demo/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_MODE, MAX_LENGTH_STATE_STATE
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

ENTITY_TEXT = "text.text"
Expand All @@ -22,7 +23,7 @@ async def setup_demo_text(hass):
await hass.async_block_till_done()


def test_setup_params(hass):
def test_setup_params(hass: HomeAssistant) -> None:
"""Test the initial parameters."""
state = hass.states.get(ENTITY_TEXT)
assert state.state == "Hello world"
Expand Down
5 changes: 3 additions & 2 deletions tests/components/eight_sleep/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Test the Eight Sleep config flow."""
from homeassistant import config_entries
from homeassistant.components.eight_sleep.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType


async def test_form(hass) -> None:
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
Expand Down Expand Up @@ -48,7 +49,7 @@ async def test_form_invalid_auth(hass, token_error) -> None:
assert result2["errors"] == {"base": "cannot_connect"}


async def test_import(hass) -> None:
async def test_import(hass: HomeAssistant) -> None:
"""Test import works."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
Expand Down
5 changes: 3 additions & 2 deletions tests/components/generic/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
CONF_VERIFY_SSL,
HTTP_BASIC_AUTHENTICATION,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry

from tests.common import MockConfigEntry
Expand Down Expand Up @@ -736,7 +737,7 @@ async def test_unload_entry(hass, fakeimg_png):
assert mock_entry.state is config_entries.ConfigEntryState.NOT_LOADED


async def test_reload_on_title_change(hass) -> None:
async def test_reload_on_title_change(hass: HomeAssistant) -> None:
"""Test the integration gets reloaded when the title is updated."""

test_data = TESTDATA_OPTIONS
Expand All @@ -758,7 +759,7 @@ async def test_reload_on_title_change(hass) -> None:
assert hass.states.get("camera.my_title").attributes["friendly_name"] == "New Title"


async def test_migrate_existing_ids(hass) -> None:
async def test_migrate_existing_ids(hass: HomeAssistant) -> None:
"""Test that existing ids are migrated for issue #70568."""

registry = entity_registry.async_get(hass)
Expand Down
3 changes: 2 additions & 1 deletion tests/components/hassio/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from homeassistant.components.hassio.http import _need_auth
from homeassistant.core import HomeAssistant


async def test_forward_request(hassio_client, aioclient_mock):
Expand Down Expand Up @@ -170,7 +171,7 @@ async def test_backup_download_headers(hassio_client, aioclient_mock):
assert resp.headers["Content-Disposition"] == content_disposition


def test_need_auth(hass):
def test_need_auth(hass: HomeAssistant) -> None:
"""Test if the requested path needs authentication."""
assert not _need_auth(hass, "addons/test/logo")
assert _need_auth(hass, "backups/new/upload")
Expand Down
2 changes: 1 addition & 1 deletion tests/components/integration/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tests.common import mock_restore_cache


async def test_state(hass) -> None:
async def test_state(hass: HomeAssistant) -> None:
"""Test integration sensor state."""
config = {
"sensor": {
Expand Down
3 changes: 2 additions & 1 deletion tests/components/kira/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import MagicMock

from homeassistant.components.kira import remote as kira
from homeassistant.core import HomeAssistant

SERVICE_SEND_COMMAND = "send_command"

Expand All @@ -18,7 +19,7 @@ def add_entities(devices):
DEVICES.append(device)


def test_service_call(hass):
def test_service_call(hass: HomeAssistant) -> None:
"""Test Kira's ability to send commands."""
mock_kira = MagicMock()
hass.data[kira.DOMAIN] = {kira.CONF_REMOTE: {}}
Expand Down
7 changes: 4 additions & 3 deletions tests/components/mazda/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import init_integration


async def test_button_setup_non_electric_vehicle(hass) -> None:
async def test_button_setup_non_electric_vehicle(hass: HomeAssistant) -> None:
"""Test creation of button entities."""
await init_integration(hass)

Expand Down Expand Up @@ -58,7 +59,7 @@ async def test_button_setup_non_electric_vehicle(hass) -> None:
assert state is None


async def test_button_setup_electric_vehicle(hass) -> None:
async def test_button_setup_electric_vehicle(hass: HomeAssistant) -> None:
"""Test creation of button entities for an electric vehicle."""
await init_integration(hass, electric_vehicle=True)

Expand Down Expand Up @@ -133,7 +134,7 @@ async def test_button_press(hass, entity_id_suffix, api_method_name) -> None:
api_method.assert_called_once_with(12345)


async def test_button_press_error(hass) -> None:
async def test_button_press_error(hass: HomeAssistant) -> None:
"""Test the Mazda API raising an error when a button entity is pressed."""
client_mock = await init_integration(hass)

Expand Down
6 changes: 4 additions & 2 deletions tests/components/mqtt/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def test_controlling_validation_state_via_topic(
assert state.state == "no"


async def test_attribute_validation_max_greater_then_min(hass) -> None:
async def test_attribute_validation_max_greater_then_min(hass: HomeAssistant) -> None:
"""Test the validation of min and max configuration attributes."""
assert not await async_setup_component(
hass,
Expand All @@ -190,7 +190,9 @@ async def test_attribute_validation_max_greater_then_min(hass) -> None:
)


async def test_attribute_validation_max_not_greater_then_max_state_length(hass) -> None:
async def test_attribute_validation_max_not_greater_then_max_state_length(
hass: HomeAssistant,
) -> None:
"""Test the max value of of max configuration attribute."""
assert not await async_setup_component(
hass,
Expand Down
3 changes: 2 additions & 1 deletion tests/components/nx584/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests

from homeassistant.components.nx584 import binary_sensor as nx584
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

DEFAULT_CONFIG = {
Expand Down Expand Up @@ -133,7 +134,7 @@ async def test_nx584_sensor_setup_version_too_old(hass):


@pytest.mark.usefixtures("client")
def test_nx584_sensor_setup_no_zones(hass):
def test_nx584_sensor_setup_no_zones(hass: HomeAssistant) -> None:
"""Test the setup with no zones."""
nx584_client.Client.return_value.list_zones.return_value = []
add_entities = mock.MagicMock()
Expand Down
3 changes: 2 additions & 1 deletion tests/components/pilight/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from voluptuous import MultipleInvalid

from homeassistant.components import pilight
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util

Expand Down Expand Up @@ -388,7 +389,7 @@ async def test_call_rate_delay_throttle_enabled(hass):
assert runs == exp


def test_call_rate_delay_throttle_disabled(hass):
def test_call_rate_delay_throttle_disabled(hass: HomeAssistant) -> None:
"""Test that the limiter is a noop if no delay set."""
runs = []

Expand Down
8 changes: 4 additions & 4 deletions tests/components/plant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
STATE_PROBLEM,
STATE_UNAVAILABLE,
)
from homeassistant.core import State
from homeassistant.core import HomeAssistant, State
from homeassistant.setup import async_setup_component

from tests.components.recorder.common import async_wait_recording_done
Expand Down Expand Up @@ -192,13 +192,13 @@ async def test_brightness_history(hass):
assert state.state == STATE_OK


def test_daily_history_no_data(hass):
def test_daily_history_no_data(hass: HomeAssistant) -> None:
"""Test with empty history."""
dh = plant.DailyHistory(3)
assert dh.max is None


def test_daily_history_one_day(hass):
def test_daily_history_one_day(hass: HomeAssistant) -> None:
"""Test storing data for the same day."""
dh = plant.DailyHistory(3)
values = [-2, 10, 0, 5, 20]
Expand All @@ -209,7 +209,7 @@ def test_daily_history_one_day(hass):
assert dh.max == max_value


def test_daily_history_multiple_days(hass):
def test_daily_history_multiple_days(hass: HomeAssistant) -> None:
"""Test storing data for different days."""
dh = plant.DailyHistory(3)
today = datetime.now()
Expand Down
5 changes: 4 additions & 1 deletion tests/components/powerwall/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.components import dhcp
from homeassistant.components.powerwall.const import DOMAIN
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from .mocks import (
Expand Down Expand Up @@ -507,7 +508,9 @@ async def test_dhcp_discovery_updates_unique_id_when_entry_is_failed(hass):
assert entry.unique_id == MOCK_GATEWAY_DIN


async def test_discovered_wifi_does_not_update_ip_if_is_still_online(hass) -> None:
async def test_discovered_wifi_does_not_update_ip_if_is_still_online(
hass: HomeAssistant,
) -> None:
"""Test a discovery does not update the ip unless the powerwall at the old ip is offline."""
entry = MockConfigEntry(
domain=DOMAIN,
Expand Down
Loading

0 comments on commit 59ca778

Please sign in to comment.