Update homeassistant integration tests to async (#124117)

This commit is contained in:
Erik Montnemery 2024-08-17 21:11:32 +02:00 committed by GitHub
parent d72d4286db
commit 63d1cc10e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,5 @@
"""The tests for Core components.""" """The tests for Core components."""
import asyncio
import unittest
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
import pytest import pytest
@ -44,142 +42,67 @@ from tests.common import (
MockUser, MockUser,
async_capture_events, async_capture_events,
async_mock_service, async_mock_service,
get_test_home_assistant,
mock_service,
patch_yaml_files, patch_yaml_files,
) )
def turn_on(hass, entity_id=None, **service_data): async def test_is_on(hass: HomeAssistant) -> None:
"""Turn specified entity on if possible.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_ON, service_data)
def turn_off(hass, entity_id=None, **service_data):
"""Turn specified entity off.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_OFF, service_data)
def toggle(hass, entity_id=None, **service_data):
"""Toggle specified entity.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TOGGLE, service_data)
def stop(hass):
"""Stop Home Assistant.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP)
def restart(hass):
"""Stop Home Assistant.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
def check_config(hass):
"""Check the config files.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_CHECK_CONFIG)
def reload_core_config(hass):
"""Reload the core config.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
class TestComponentsCore(unittest.TestCase):
"""Test homeassistant.components module."""
def setUp(self):
"""Set up things to be run when tests are started."""
self._manager = get_test_home_assistant()
self.hass = self._manager.__enter__()
assert asyncio.run_coroutine_threadsafe(
async_setup_component(self.hass, "homeassistant", {}), self.hass.loop
).result()
self.hass.states.set("light.Bowl", STATE_ON)
self.hass.states.set("light.Ceiling", STATE_OFF)
def tearDown(self) -> None:
"""Tear down hass object."""
self.hass.stop()
self._manager.__exit__(None, None, None)
def test_is_on(self):
"""Test is_on method.""" """Test is_on method."""
with pytest.raises( with pytest.raises(
RuntimeError, RuntimeError,
match="Detected code that uses homeassistant.components.is_on. This is deprecated and will stop working", match="Detected code that uses homeassistant.components.is_on. This is deprecated and will stop working",
): ):
assert comps.is_on(self.hass, "light.Bowl") assert comps.is_on(hass, "light.Bowl")
def test_turn_on_without_entities(self):
async def test_turn_on_without_entities(hass: HomeAssistant) -> None:
"""Test turn_on method without entities.""" """Test turn_on method without entities."""
calls = mock_service(self.hass, "light", SERVICE_TURN_ON) await async_setup_component(hass, ha.DOMAIN, {})
turn_on(self.hass) calls = async_mock_service(hass, "light", SERVICE_TURN_ON)
self.hass.block_till_done() await hass.services.async_call(ha.DOMAIN, SERVICE_TURN_ON, blocking=True)
assert len(calls) == 0 assert len(calls) == 0
def test_turn_on(self):
async def test_turn_on(hass: HomeAssistant) -> None:
"""Test turn_on method.""" """Test turn_on method."""
calls = mock_service(self.hass, "light", SERVICE_TURN_ON) await async_setup_component(hass, ha.DOMAIN, {})
turn_on(self.hass, "light.Ceiling") calls = async_mock_service(hass, "light", SERVICE_TURN_ON)
self.hass.block_till_done() await hass.services.async_call(
ha.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "light.Ceiling"}, blocking=True
)
assert len(calls) == 1 assert len(calls) == 1
def test_turn_off(self):
async def test_turn_off(hass: HomeAssistant) -> None:
"""Test turn_off method.""" """Test turn_off method."""
calls = mock_service(self.hass, "light", SERVICE_TURN_OFF) await async_setup_component(hass, ha.DOMAIN, {})
turn_off(self.hass, "light.Bowl") calls = async_mock_service(hass, "light", SERVICE_TURN_OFF)
self.hass.block_till_done() await hass.services.async_call(
ha.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "light.Bowl"}, blocking=True
)
assert len(calls) == 1 assert len(calls) == 1
def test_toggle(self):
async def test_toggle(hass: HomeAssistant) -> None:
"""Test toggle method.""" """Test toggle method."""
calls = mock_service(self.hass, "light", SERVICE_TOGGLE) await async_setup_component(hass, ha.DOMAIN, {})
toggle(self.hass, "light.Bowl") calls = async_mock_service(hass, "light", SERVICE_TOGGLE)
self.hass.block_till_done() await hass.services.async_call(
ha.DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: "light.Bowl"}, blocking=True
)
assert len(calls) == 1 assert len(calls) == 1
@patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
def test_reload_core_conf(self): @patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
async def test_reload_core_conf(hass: HomeAssistant) -> None:
"""Test reload core conf service.""" """Test reload core conf service."""
await async_setup_component(hass, ha.DOMAIN, {})
ent = entity.Entity() ent = entity.Entity()
ent.entity_id = "test.entity" ent.entity_id = "test.entity"
ent.hass = self.hass ent.hass = hass
ent.schedule_update_ha_state() ent.async_write_ha_state()
self.hass.block_till_done()
state = self.hass.states.get("test.entity") state = hass.states.get("test.entity")
assert state is not None assert state is not None
assert state.state == "unknown" assert state.state == "unknown"
assert state.attributes == {} assert state.attributes == {}
@ -197,51 +120,63 @@ class TestComponentsCore(unittest.TestCase):
) )
} }
with patch_yaml_files(files, True): with patch_yaml_files(files, True):
reload_core_config(self.hass) await hass.services.async_call(
self.hass.block_till_done() ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, blocking=True
)
assert self.hass.config.latitude == 10 assert hass.config.latitude == 10
assert self.hass.config.longitude == 20 assert hass.config.longitude == 20
ent.schedule_update_ha_state() ent.async_write_ha_state()
self.hass.block_till_done()
state = self.hass.states.get("test.entity") state = hass.states.get("test.entity")
assert state is not None assert state is not None
assert state.state == "unknown" assert state.state == "unknown"
assert state.attributes.get("hello") == "world" assert state.attributes.get("hello") == "world"
@patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
@patch("homeassistant.components.homeassistant._LOGGER.error") @patch("homeassistant.config.os.path.isfile", Mock(return_value=True))
@patch("homeassistant.config.async_process_ha_core_config") @patch("homeassistant.components.homeassistant._LOGGER.error")
def test_reload_core_with_wrong_conf(self, mock_process, mock_error): @patch("homeassistant.config.async_process_ha_core_config")
async def test_reload_core_with_wrong_conf(
mock_process, mock_error, hass: HomeAssistant
) -> None:
"""Test reload core conf service.""" """Test reload core conf service."""
files = {config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])} files = {config.YAML_CONFIG_FILE: yaml.dump(["invalid", "config"])}
await async_setup_component(hass, ha.DOMAIN, {})
with patch_yaml_files(files, True): with patch_yaml_files(files, True):
reload_core_config(self.hass) await hass.services.async_call(
self.hass.block_till_done() ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, blocking=True
)
assert mock_error.called assert mock_error.called
assert mock_process.called is False assert mock_process.called is False
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=None)
@patch( @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None)
@patch(
"homeassistant.config.async_check_ha_config_file", "homeassistant.config.async_check_ha_config_file",
side_effect=HomeAssistantError("Test error"), side_effect=HomeAssistantError("Test error"),
)
async def test_restart_homeassistant_wrong_conf(
mock_check, mock_restart, hass: HomeAssistant
) -> None:
"""Test restart service with error."""
await async_setup_component(hass, ha.DOMAIN, {})
with pytest.raises(HomeAssistantError, match="Test error"):
await hass.services.async_call(
ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART, blocking=True
) )
def test_restart_homeassistant_wrong_conf(self, mock_check, mock_restart):
"""Test stop service."""
restart(self.hass)
self.hass.block_till_done()
assert mock_check.called assert mock_check.called
assert not mock_restart.called assert not mock_restart.called
@patch("homeassistant.core.HomeAssistant.async_stop", return_value=None)
@patch("homeassistant.config.async_check_ha_config_file", return_value=None) @patch("homeassistant.core.HomeAssistant.async_stop", return_value=None)
def test_check_config(self, mock_check, mock_stop): @patch("homeassistant.config.async_check_ha_config_file", return_value=None)
async def test_check_config(mock_check, mock_stop, hass: HomeAssistant) -> None:
"""Test stop service.""" """Test stop service."""
check_config(self.hass) await async_setup_component(hass, ha.DOMAIN, {})
self.hass.block_till_done() await hass.services.async_call(ha.DOMAIN, SERVICE_CHECK_CONFIG, blocking=True)
assert mock_check.called assert mock_check.called
assert not mock_stop.called assert not mock_stop.called