core/homeassistant/components/analytics_insights/config_flow.py

187 lines
6.5 KiB
Python
Raw Normal View History

Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
"""Config flow for Homeassistant Analytics integration."""
from __future__ import annotations
from typing import Any
from python_homeassistant_analytics import (
HomeassistantAnalyticsClient,
HomeassistantAnalyticsConnectionError,
)
from python_homeassistant_analytics.models import IntegrationType
import voluptuous as vol
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
OptionsFlow,
OptionsFlowWithConfigEntry,
)
from homeassistant.core import callback
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import (
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
)
from .const import (
CONF_TRACKED_CUSTOM_INTEGRATIONS,
CONF_TRACKED_INTEGRATIONS,
DOMAIN,
LOGGER,
)
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
INTEGRATION_TYPES_WITHOUT_ANALYTICS = (
IntegrationType.BRAND,
IntegrationType.ENTITY,
IntegrationType.VIRTUAL,
)
class HomeassistantAnalyticsConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Homeassistant Analytics."""
@staticmethod
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Get the options flow for this handler."""
return HomeassistantAnalyticsOptionsFlowHandler(config_entry)
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
self._async_abort_entries_match()
errors: dict[str, str] = {}
if user_input is not None:
if not user_input.get(CONF_TRACKED_INTEGRATIONS) and not user_input.get(
CONF_TRACKED_CUSTOM_INTEGRATIONS
):
errors["base"] = "no_integrations_selected"
else:
return self.async_create_entry(
title="Home Assistant Analytics Insights",
data={},
options={
CONF_TRACKED_INTEGRATIONS: user_input.get(
CONF_TRACKED_INTEGRATIONS, []
),
CONF_TRACKED_CUSTOM_INTEGRATIONS: user_input.get(
CONF_TRACKED_CUSTOM_INTEGRATIONS, []
),
},
)
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
client = HomeassistantAnalyticsClient(
session=async_get_clientsession(self.hass)
)
try:
integrations = await client.get_integrations()
custom_integrations = await client.get_custom_integrations()
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
except HomeassistantAnalyticsConnectionError:
LOGGER.exception("Error connecting to Home Assistant analytics")
return self.async_abort(reason="cannot_connect")
options = [
SelectOptionDict(
value=domain,
label=integration.title,
)
for domain, integration in integrations.items()
if integration.integration_type not in INTEGRATION_TYPES_WITHOUT_ANALYTICS
]
return self.async_show_form(
step_id="user",
errors=errors,
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
data_schema=vol.Schema(
{
vol.Optional(CONF_TRACKED_INTEGRATIONS): SelectSelector(
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
SelectSelectorConfig(
options=options,
multiple=True,
sort=True,
)
),
vol.Optional(CONF_TRACKED_CUSTOM_INTEGRATIONS): SelectSelector(
SelectSelectorConfig(
options=list(custom_integrations),
multiple=True,
sort=True,
)
),
Add Homeassistant Analytics Insights integration (#107634) * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Add Homeassistant Analytics integration * Fix feedback * Fix test * Update conftest.py * Add some testcases * Make code clear * log exception * Bump python-homeassistant-analytics to 0.2.1 * Bump python-homeassistant-analytics to 0.3.0 * Change domain to homeassistant_analytics_consumer * Add integration name to config flow selector * Update homeassistant/components/homeassistant_analytics_consumer/manifest.json Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> * Fix hassfest * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Bump python-homeassistant-analytics to 0.4.0 * Rename to Home Assistant Analytics Insights * Update homeassistant/components/analytics_insights/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/analytics_insights/manifest.json Co-authored-by: Robert Resch <robert@resch.dev> * Rename to Home Assistant Analytics Insights * add test * Fallback to 0 when there is no data found * Allow to select any integration * Fix tests * Fix tests * Update tests/components/analytics_insights/conftest.py Co-authored-by: Robert Resch <robert@resch.dev> * Update tests/components/analytics_insights/test_sensor.py Co-authored-by: Robert Resch <robert@resch.dev> * Fix format * Fix tests --------- Co-authored-by: Sid <27780930+autinerd@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
2024-01-23 09:32:31 +00:00
}
),
)
class HomeassistantAnalyticsOptionsFlowHandler(OptionsFlowWithConfigEntry):
"""Handle Homeassistant Analytics options."""
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
errors: dict[str, str] = {}
if user_input is not None:
if not user_input.get(CONF_TRACKED_INTEGRATIONS) and not user_input.get(
CONF_TRACKED_CUSTOM_INTEGRATIONS
):
errors["base"] = "no_integrations_selected"
else:
return self.async_create_entry(
title="",
data={
CONF_TRACKED_INTEGRATIONS: user_input.get(
CONF_TRACKED_INTEGRATIONS, []
),
CONF_TRACKED_CUSTOM_INTEGRATIONS: user_input.get(
CONF_TRACKED_CUSTOM_INTEGRATIONS, []
),
},
)
client = HomeassistantAnalyticsClient(
session=async_get_clientsession(self.hass)
)
try:
integrations = await client.get_integrations()
custom_integrations = await client.get_custom_integrations()
except HomeassistantAnalyticsConnectionError:
LOGGER.exception("Error connecting to Home Assistant analytics")
return self.async_abort(reason="cannot_connect")
options = [
SelectOptionDict(
value=domain,
label=integration.title,
)
for domain, integration in integrations.items()
if integration.integration_type not in INTEGRATION_TYPES_WITHOUT_ANALYTICS
]
return self.async_show_form(
step_id="init",
errors=errors,
data_schema=self.add_suggested_values_to_schema(
vol.Schema(
{
vol.Optional(CONF_TRACKED_INTEGRATIONS): SelectSelector(
SelectSelectorConfig(
options=options,
multiple=True,
sort=True,
)
),
vol.Optional(CONF_TRACKED_CUSTOM_INTEGRATIONS): SelectSelector(
SelectSelectorConfig(
options=list(custom_integrations),
multiple=True,
sort=True,
)
),
},
),
self.options,
),
)