From cd3ba989bde15dd0293bbcbb92f6493f856a7fa7 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Fri, 8 Mar 2024 13:46:17 -0600 Subject: [PATCH] More tests, remove temporary test_test.py --- tests/test_init.py | 159 +++++++++++++++++++++++++++++++-------------- tests/test_test.py | 14 ---- 2 files changed, 109 insertions(+), 64 deletions(-) delete mode 100644 tests/test_test.py diff --git a/tests/test_init.py b/tests/test_init.py index 6ea9914..2b5bd24 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -3,6 +3,7 @@ from unittest.mock import patch from pytest_homeassistant_custom_component.common import assert_setup_component +from homeassistant.const import EVENT_CORE_CONFIG_UPDATE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.setup import async_setup_component @@ -10,13 +11,48 @@ from custom_components.sun2.const import DOMAIN +CONFIG_1 = {"unique_id": "1"} + +LOC_2A = { + "latitude": 40.68954412564642, + "longitude": -74.04486696480146, + "elevation": 0, + "time_zone": "America/New_York", +} +CONFIG_2A = { + "unique_id": "Test 2", + "location": "Statue of Liberty", +} | LOC_2A + +LOC_2B = { + "latitude": 39.50924426436838, + "longitude": -98.43369506033378, + "elevation": 10, + "time_zone": "CST", +} +CONFIG_2B = { + "unique_id": "Test 2", + "location": "World's Largest Ball of Twine", +} | LOC_2B + +LOC_3 = { + "latitude": 34.134092337996336, + "longitude": -118.32154780135669, + "elevation": 391, + "time_zone": "America/Los_Angeles", +} +CONFIG_3 = { + "unique_id": "3", + "location": "Hollywood Sign", +} | LOC_3 + async def test_basic_yaml_config( hass: HomeAssistant, entity_registry: EntityRegistry ) -> None: """Test basic YAML configuration.""" with assert_setup_component(1, DOMAIN): - await async_setup_component(hass, DOMAIN, {DOMAIN: [{"unique_id": 1}]}) + await async_setup_component(hass, DOMAIN, {DOMAIN: [CONFIG_1]}) await hass.async_block_till_done() expected_entities = ( @@ -72,42 +108,7 @@ async def test_reload_service( hass: HomeAssistant, entity_registry: EntityRegistry ) -> None: """Test basic YAML configuration.""" - config_1 = {"unique_id": "1"} - - loc_2a = { - "latitude": 40.68954412564642, - "longitude": -74.04486696480146, - "elevation": 0, - "time_zone": "America/New_York", - } - config_2a = { - "unique_id": "Test 2", - "location": "Statue of Liberty", - } | loc_2a - - loc_2b = { - "latitude": 39.50924426436838, - "longitude": -98.43369506033378, - "elevation": 10, - "time_zone": "CST", - } - config_2b = { - "unique_id": "Test 2", - "location": "World's Largest Ball of Twine", - } | loc_2b - - loc_3 = { - "latitude": 34.134092337996336, - "longitude": -118.32154780135669, - "elevation": 391, - "time_zone": "America/Los_Angeles", - } - config_3 = { - "unique_id": "3", - "location": "Hollywood Sign", - } | loc_3 - - init_config = [config_1, config_2a] + init_config = [CONFIG_1, CONFIG_2A] with assert_setup_component(len(init_config), DOMAIN): await async_setup_component(hass, DOMAIN, {DOMAIN: init_config}) await hass.async_block_till_done() @@ -116,19 +117,19 @@ async def test_reload_service( config_entries = hass.config_entries.async_entries(DOMAIN) assert len(config_entries) == 2 config_entry_1 = config_entries[0] - assert config_entry_1.unique_id == config_1["unique_id"] + assert config_entry_1.unique_id == CONFIG_1["unique_id"] assert config_entry_1.title == hass.config.location_name assert config_entry_1.options == {} config_entry_2 = config_entries[1] - assert config_entry_2.unique_id == config_2a["unique_id"] - assert config_entry_2.title == config_2a["location"] - assert config_entry_2.options == loc_2a + assert config_entry_2.unique_id == CONFIG_2A["unique_id"] + assert config_entry_2.title == CONFIG_2A["location"] + assert config_entry_2.options == LOC_2A # Check reload service exists. assert hass.services.has_service(DOMAIN, "reload") # Reload new config. - reload_config = [config_2b, config_3] + reload_config = [CONFIG_2B, CONFIG_3] with patch( "custom_components.sun2.async_integration_yaml_config", autospec=True, @@ -141,16 +142,16 @@ async def test_reload_service( config_entries = hass.config_entries.async_entries(DOMAIN) assert len(config_entries) == 2 config_entry_1 = config_entries[0] - assert config_entry_1.unique_id == config_2b["unique_id"] - assert config_entry_1.title == config_2b["location"] - assert config_entry_1.options == loc_2b + assert config_entry_1.unique_id == CONFIG_2B["unique_id"] + assert config_entry_1.title == CONFIG_2B["location"] + assert config_entry_1.options == LOC_2B config_entry_2 = config_entries[1] - assert config_entry_2.unique_id == config_3["unique_id"] - assert config_entry_2.title == config_3["location"] - assert config_entry_2.options == loc_3 + assert config_entry_2.unique_id == CONFIG_3["unique_id"] + assert config_entry_2.title == CONFIG_3["location"] + assert config_entry_2.options == LOC_3 # Reload with same config. - reload_config = [config_2b, config_3] + reload_config = [CONFIG_2B, CONFIG_3] with patch( "custom_components.sun2.async_integration_yaml_config", autospec=True, @@ -160,7 +161,9 @@ async def test_reload_service( await hass.async_block_till_done() # Check config entries haven't changed. - assert config_entries == hass.config_entries.async_entries(DOMAIN) + orig = [config_entry.as_dict() for config_entry in config_entries] + config_entries == hass.config_entries.async_entries(DOMAIN) + assert [config_entry.as_dict() for config_entry in config_entries] == orig # Reload config with config removed. with patch( @@ -187,3 +190,59 @@ async def test_reload_service( # Check there are still no config entries. assert not hass.config_entries.async_entries(DOMAIN) + + +async def test_ha_config_update( + hass: HomeAssistant, entity_registry: EntityRegistry +) -> None: + """Test when HA config is updated.""" + new_time_zone = "America/New_York" + new_location_name = "New York, NY" + + # Check some assumptions. + assert hass.config.time_zone != new_time_zone + assert hass.config.location_name != new_location_name + + await async_setup_component(hass, DOMAIN, {DOMAIN: [CONFIG_1]}) + await hass.async_block_till_done() + + # ConfigEntry object may change values, but it should not be replaced by a new + # object. + config_entry = hass.config_entries.async_entries(DOMAIN)[0] + + # Get baseline. + old_values = config_entry.as_dict() + assert old_values["title"] == hass.config.location_name + + # Fire an EVENT_CORE_CONFIG_UPDATE event with no data and check that nothing has + # changed. + hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE) + await hass.async_block_till_done() + assert hass.config_entries.async_entries(DOMAIN)[0] is config_entry + new_values = config_entry.as_dict() + assert new_values == old_values + old_values = new_values + + # Change anything except for location_name and language and check that nothing + # changes. + await hass.config.async_update(time_zone=new_time_zone) + await hass.async_block_till_done() + assert hass.config_entries.async_entries(DOMAIN)[0] is config_entry + new_values = config_entry.as_dict() + assert new_values == old_values + old_values = new_values + + # Change location_name and check that config entry reflects this change. + # Note that this will cause a reload of YAML config. + with patch( + "custom_components.sun2.async_integration_yaml_config", + autospec=True, + return_value={DOMAIN: [CONFIG_1]}, + ): + await hass.config.async_update(location_name=new_location_name) + await hass.async_block_till_done() + + assert hass.config_entries.async_entries(DOMAIN)[0] is config_entry + new_values = config_entry.as_dict() + assert new_values != old_values + assert new_values["title"] == new_location_name diff --git a/tests/test_test.py b/tests/test_test.py deleted file mode 100644 index ec078c1..0000000 --- a/tests/test_test.py +++ /dev/null @@ -1,14 +0,0 @@ -from __future__ import annotations - -from homeassistant.core import HomeAssistant -from homeassistant.util import dt as dt_util - - -async def test_test(hass: HomeAssistant): - print(hass.config.latitude, hass.config.longitude, hass.config.time_zone, dt_util.now()) - await hass.config.async_update( - latitude=41.5593425366867, - longitude=-88.20533931255342, - time_zone="America/Chicago" - ) - print(hass.config.latitude, hass.config.longitude, hass.config.time_zone, dt_util.now())