Fix Azure data explorer (#119089)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
kaareseras 2024-06-11 09:18:06 +02:00 committed by GitHub
parent 08eb8232e5
commit 511547c29a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 32 deletions

View file

@ -62,13 +62,12 @@ 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.
If config is empty after getting the filter, return, otherwise emit
deprecated warning and pass the rest to the config flow.
"""
hass.data.setdefault(DOMAIN, {DATA_FILTER: {}})
hass.data.setdefault(DOMAIN, {DATA_FILTER: FILTER_SCHEMA({})})
if DOMAIN in yaml_config:
hass.data[DOMAIN][DATA_FILTER] = yaml_config[DOMAIN][CONF_FILTER]
hass.data[DOMAIN][DATA_FILTER] = yaml_config[DOMAIN].pop(CONF_FILTER)
return True
@ -207,6 +206,6 @@ class AzureDataExplorer:
if "\n" in state.state:
return None, dropped + 1
json_event = str(json.dumps(obj=state, cls=JSONEncoder).encode("utf-8"))
json_event = json.dumps(obj=state, cls=JSONEncoder)
return (json_event, dropped)

View file

@ -23,7 +23,7 @@ from .const import (
CONF_APP_REG_ID,
CONF_APP_REG_SECRET,
CONF_AUTHORITY_ID,
CONF_USE_FREE,
CONF_USE_QUEUED_CLIENT,
)
_LOGGER = logging.getLogger(__name__)
@ -35,7 +35,6 @@ class AzureDataExplorerClient:
def __init__(self, data: Mapping[str, Any]) -> None:
"""Create the right class."""
self._cluster_ingest_uri = data[CONF_ADX_CLUSTER_INGEST_URI]
self._database = data[CONF_ADX_DATABASE_NAME]
self._table = data[CONF_ADX_TABLE_NAME]
self._ingestion_properties = IngestionProperties(
@ -45,24 +44,36 @@ class AzureDataExplorerClient:
ingestion_mapping_reference="ha_json_mapping",
)
# Create cLient for ingesting and querying data
kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(
self._cluster_ingest_uri,
# Create client for ingesting data
kcsb_ingest = (
KustoConnectionStringBuilder.with_aad_application_key_authentication(
data[CONF_ADX_CLUSTER_INGEST_URI],
data[CONF_APP_REG_ID],
data[CONF_APP_REG_SECRET],
data[CONF_AUTHORITY_ID],
)
)
if data[CONF_USE_FREE] is True:
# Create client for querying data
kcsb_query = (
KustoConnectionStringBuilder.with_aad_application_key_authentication(
data[CONF_ADX_CLUSTER_INGEST_URI].replace("ingest-", ""),
data[CONF_APP_REG_ID],
data[CONF_APP_REG_SECRET],
data[CONF_AUTHORITY_ID],
)
)
if data[CONF_USE_QUEUED_CLIENT] is True:
# Queded is the only option supported on free tear of ADX
self.write_client = QueuedIngestClient(kcsb)
self.write_client = QueuedIngestClient(kcsb_ingest)
else:
self.write_client = ManagedStreamingIngestClient.from_dm_kcsb(kcsb)
self.write_client = ManagedStreamingIngestClient.from_dm_kcsb(kcsb_ingest)
self.query_client = KustoClient(kcsb)
self.query_client = KustoClient(kcsb_query)
def test_connection(self) -> None:
"""Test connection, will throw Exception when it cannot connect."""
"""Test connection, will throw Exception if it cannot connect."""
query = f"{self._table} | take 1"

View file

@ -10,6 +10,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.helpers.selector import BooleanSelector
from . import AzureDataExplorerClient
from .const import (
@ -19,7 +20,7 @@ from .const import (
CONF_APP_REG_ID,
CONF_APP_REG_SECRET,
CONF_AUTHORITY_ID,
CONF_USE_FREE,
CONF_USE_QUEUED_CLIENT,
DEFAULT_OPTIONS,
DOMAIN,
)
@ -34,7 +35,7 @@ STEP_USER_DATA_SCHEMA = vol.Schema(
vol.Required(CONF_APP_REG_ID): str,
vol.Required(CONF_APP_REG_SECRET): str,
vol.Required(CONF_AUTHORITY_ID): str,
vol.Optional(CONF_USE_FREE, default=False): bool,
vol.Required(CONF_USE_QUEUED_CLIENT, default=False): BooleanSelector(),
}
)

View file

@ -17,7 +17,7 @@ CONF_AUTHORITY_ID = "authority_id"
CONF_SEND_INTERVAL = "send_interval"
CONF_MAX_DELAY = "max_delay"
CONF_FILTER = DATA_FILTER = "filter"
CONF_USE_FREE = "use_queued_ingestion"
CONF_USE_QUEUED_CLIENT = "use_queued_ingestion"
DATA_HUB = "hub"
STEP_USER = "user"

View file

@ -3,15 +3,19 @@
"step": {
"user": {
"title": "Setup your Azure Data Explorer integration",
"description": "Enter connection details.",
"description": "Enter connection details",
"data": {
"cluster_ingest_uri": "Cluster ingest URI",
"database": "Database name",
"table": "Table name",
"cluster_ingest_uri": "Cluster Ingest URI",
"authority_id": "Authority ID",
"client_id": "Client ID",
"client_secret": "Client secret",
"authority_id": "Authority ID",
"database": "Database name",
"table": "Table name",
"use_queued_ingestion": "Use queued ingestion"
},
"data_description": {
"cluster_ingest_uri": "Ingest-URI of the cluster",
"use_queued_ingestion": "Must be enabled when using ADX free cluster"
}
}
},

View file

@ -8,7 +8,7 @@ from homeassistant.components.azure_data_explorer.const import (
CONF_APP_REG_SECRET,
CONF_AUTHORITY_ID,
CONF_SEND_INTERVAL,
CONF_USE_FREE,
CONF_USE_QUEUED_CLIENT,
)
AZURE_DATA_EXPLORER_PATH = "homeassistant.components.azure_data_explorer"
@ -29,7 +29,7 @@ BASE_CONFIG_URI = {
}
BASIC_OPTIONS = {
CONF_USE_FREE: False,
CONF_USE_QUEUED_CLIENT: False,
CONF_SEND_INTERVAL: 5,
}
@ -39,10 +39,10 @@ BASE_CONFIG_FULL = BASE_CONFIG | BASIC_OPTIONS | BASE_CONFIG_URI
BASE_CONFIG_IMPORT = {
CONF_ADX_CLUSTER_INGEST_URI: "https://cluster.region.kusto.windows.net",
CONF_USE_FREE: False,
CONF_USE_QUEUED_CLIENT: False,
CONF_SEND_INTERVAL: 5,
}
FREE_OPTIONS = {CONF_USE_FREE: True, CONF_SEND_INTERVAL: 5}
FREE_OPTIONS = {CONF_USE_QUEUED_CLIENT: True, CONF_SEND_INTERVAL: 5}
BASE_CONFIG_FREE = BASE_CONFIG | FREE_OPTIONS