From d96fd518e730f06ba9c2941a0c0c28476a7440f8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 30 Sep 2024 14:45:37 +0200 Subject: [PATCH] Use HassKey in azure_data_explorer (#127087) * Use HassKey in azure_data_explorer * Adjust tests * Adjust * Remove test --- .../azure_data_explorer/__init__.py | 18 ++++++------ .../components/azure_data_explorer/const.py | 3 +- .../azure_data_explorer/test_init.py | 28 +------------------ 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/azure_data_explorer/__init__.py b/homeassistant/components/azure_data_explorer/__init__.py index 34f2c438d14..c416fc1cba9 100644 --- a/homeassistant/components/azure_data_explorer/__init__.py +++ b/homeassistant/components/azure_data_explorer/__init__.py @@ -16,19 +16,18 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import MATCH_ALL from homeassistant.core import Event, HomeAssistant, State from homeassistant.exceptions import ConfigEntryError -from homeassistant.helpers.entityfilter import FILTER_SCHEMA +from homeassistant.helpers.entityfilter import FILTER_SCHEMA, EntityFilter from homeassistant.helpers.event import async_call_later from homeassistant.helpers.json import ExtendedJSONEncoder from homeassistant.helpers.typing import ConfigType from homeassistant.util.dt import utcnow +from homeassistant.util.hass_dict import HassKey from .client import AzureDataExplorerClient from .const import ( CONF_APP_REG_SECRET, CONF_FILTER, CONF_SEND_INTERVAL, - DATA_FILTER, - DATA_HUB, DEFAULT_MAX_DELAY, DOMAIN, FILTER_STATES, @@ -46,6 +45,7 @@ CONFIG_SCHEMA = vol.Schema( }, extra=vol.ALLOW_EXTRA, ) +DATA_COMPONENT: HassKey[EntityFilter] = HassKey(DOMAIN) # fixtures for both init and config flow tests @@ -63,10 +63,10 @@ async def async_setup(hass: HomeAssistant, yaml_config: ConfigType) -> bool: Adds an empty filter to hass data. Tries to get a filter from yaml, if present set to hass data. """ - - hass.data.setdefault(DOMAIN, {DATA_FILTER: FILTER_SCHEMA({})}) if DOMAIN in yaml_config: - hass.data[DOMAIN][DATA_FILTER] = yaml_config[DOMAIN].pop(CONF_FILTER) + hass.data[DATA_COMPONENT] = yaml_config[DOMAIN].pop(CONF_FILTER) + else: + hass.data[DATA_COMPONENT] = FILTER_SCHEMA({}) return True @@ -83,15 +83,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: except KustoAuthenticationError: return False - hass.data[DOMAIN][DATA_HUB] = adx + entry.async_on_unload(adx.async_stop) await adx.async_start() return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - adx = hass.data[DOMAIN].pop(DATA_HUB) - await adx.async_stop() return True @@ -107,7 +105,7 @@ class AzureDataExplorer: self.hass = hass self._entry = entry - self._entities_filter = hass.data[DOMAIN][DATA_FILTER] + self._entities_filter = hass.data[DATA_COMPONENT] self._client = AzureDataExplorerClient(entry.data) diff --git a/homeassistant/components/azure_data_explorer/const.py b/homeassistant/components/azure_data_explorer/const.py index a88a6b8b94f..d6ab0bb499c 100644 --- a/homeassistant/components/azure_data_explorer/const.py +++ b/homeassistant/components/azure_data_explorer/const.py @@ -16,9 +16,8 @@ CONF_APP_REG_SECRET = "client_secret" CONF_AUTHORITY_ID = "authority_id" CONF_SEND_INTERVAL = "send_interval" CONF_MAX_DELAY = "max_delay" -CONF_FILTER = DATA_FILTER = "filter" +CONF_FILTER = "filter" CONF_USE_QUEUED_CLIENT = "use_queued_ingestion" -DATA_HUB = "hub" STEP_USER = "user" diff --git a/tests/components/azure_data_explorer/test_init.py b/tests/components/azure_data_explorer/test_init.py index 4d339728d09..10633154efd 100644 --- a/tests/components/azure_data_explorer/test_init.py +++ b/tests/components/azure_data_explorer/test_init.py @@ -9,14 +9,10 @@ from azure.kusto.ingest import StreamDescriptor import pytest from homeassistant.components import azure_data_explorer -from homeassistant.components.azure_data_explorer.const import ( - CONF_SEND_INTERVAL, - DOMAIN, -) +from homeassistant.components.azure_data_explorer.const import CONF_SEND_INTERVAL from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_ON from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from homeassistant.util.dt import utcnow from . import FilterTest @@ -99,27 +95,6 @@ async def test_put_event_on_queue_with_queueing_client( assert type(mock_queued_ingest.call_args.args[0]) is StreamDescriptor -async def test_import(hass: HomeAssistant) -> None: - """Test the popping of the filter and further import of the config.""" - config = { - DOMAIN: { - "filter": { - "include_domains": ["light"], - "include_entity_globs": ["sensor.included_*"], - "include_entities": ["binary_sensor.included"], - "exclude_domains": ["light"], - "exclude_entity_globs": ["sensor.excluded_*"], - "exclude_entities": ["binary_sensor.excluded"], - }, - } - } - - assert await async_setup_component(hass, DOMAIN, config) - await hass.async_block_till_done() - - assert "filter" in hass.data[DOMAIN] - - async def test_unload_entry( hass: HomeAssistant, entry_managed: MockConfigEntry, @@ -239,7 +214,6 @@ async def test_filter( ) await hass.async_block_till_done() assert mock_managed_streaming.called == test.expect_called - assert "filter" in hass.data[DOMAIN] @pytest.mark.parametrize(