Ensure knx tests add config entry before updating it (#110425)

This commit is contained in:
J. Nick Koston 2024-02-12 16:13:10 -06:00 committed by GitHub
parent 92842c28d2
commit da50e459bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View file

@ -29,6 +29,7 @@ from homeassistant.components.knx.const import (
) )
from homeassistant.components.knx.project import STORAGE_KEY as KNX_PROJECT_STORAGE_KEY from homeassistant.components.knx.project import STORAGE_KEY as KNX_PROJECT_STORAGE_KEY
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
@ -57,7 +58,9 @@ class KNXTestKit:
for attribute, value in attributes.items(): for attribute, value in attributes.items():
assert test_state.attributes.get(attribute) == value assert test_state.attributes.get(attribute) == value
async def setup_integration(self, config): async def setup_integration(
self, config: ConfigType, add_entry_to_hass: bool = True
) -> None:
"""Create the KNX integration.""" """Create the KNX integration."""
async def patch_xknx_start(): async def patch_xknx_start():
@ -87,12 +90,14 @@ class KNXTestKit:
self.xknx = args[0] self.xknx = args[0]
return DEFAULT return DEFAULT
if add_entry_to_hass:
self.mock_config_entry.add_to_hass(self.hass)
with patch( with patch(
"xknx.xknx.knx_interface_factory", "xknx.xknx.knx_interface_factory",
return_value=knx_ip_interface_mock(), return_value=knx_ip_interface_mock(),
side_effect=fish_xknx, side_effect=fish_xknx,
): ):
self.mock_config_entry.add_to_hass(self.hass)
await async_setup_component(self.hass, KNX_DOMAIN, {KNX_DOMAIN: config}) await async_setup_component(self.hass, KNX_DOMAIN, {KNX_DOMAIN: config})
await self.hass.async_block_till_done() await self.hass.async_block_till_done()

View file

@ -105,11 +105,12 @@ async def test_remove_telegam_history(
): ):
"""Test telegram history removal when configured to size 0.""" """Test telegram history removal when configured to size 0."""
hass_storage["knx/telegrams_history.json"] = {"version": 1, "data": MOCK_TELEGRAMS} hass_storage["knx/telegrams_history.json"] = {"version": 1, "data": MOCK_TELEGRAMS}
knx.mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
knx.mock_config_entry, knx.mock_config_entry,
data=knx.mock_config_entry.data | {CONF_KNX_TELEGRAM_LOG_SIZE: 0}, data=knx.mock_config_entry.data | {CONF_KNX_TELEGRAM_LOG_SIZE: 0},
) )
await knx.setup_integration({}) await knx.setup_integration({}, add_entry_to_hass=False)
# Store.async_remove() is mocked by hass_storage - check that data was removed. # Store.async_remove() is mocked by hass_storage - check that data was removed.
assert "knx/telegrams_history.json" not in hass_storage assert "knx/telegrams_history.json" not in hass_storage
assert not hass.data[DOMAIN].telegrams.recent_telegrams assert not hass.data[DOMAIN].telegrams.recent_telegrams