Skip to content

Commit

Permalink
Simplify coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Dec 31, 2024
1 parent 516c6dd commit 4e4be70
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
5 changes: 5 additions & 0 deletions custom_components/integration_blueprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

from __future__ import annotations

from datetime import timedelta
from typing import TYPE_CHECKING

from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.loader import async_get_loaded_integration

from .api import IntegrationBlueprintApiClient
from .const import DOMAIN, LOGGER
from .coordinator import BlueprintDataUpdateCoordinator
from .data import IntegrationBlueprintData

Expand All @@ -37,6 +39,9 @@ async def async_setup_entry(
"""Set up this integration using UI."""
coordinator = BlueprintDataUpdateCoordinator(
hass=hass,
logger=LOGGER,
name=DOMAIN,
update_interval=timedelta(hours=1),
)
entry.runtime_data = IntegrationBlueprintData(
client=IntegrationBlueprintApiClient(
Expand Down
12 changes: 10 additions & 2 deletions custom_components/integration_blueprint/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from __future__ import annotations

import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from slugify import slugify

from .api import (
IntegrationBlueprintApiClient,
Expand All @@ -25,7 +26,7 @@ class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(
self,
user_input: dict | None = None,
) -> data_entry_flow.FlowResult:
) -> config_entries.ConfigFlowResult:
"""Handle a flow initialized by the user."""
_errors = {}
if user_input is not None:
Expand All @@ -44,6 +45,13 @@ async def async_step_user(
LOGGER.exception(exception)
_errors["base"] = "unknown"
else:
await self.async_set_unique_id(
## Do NOT use this in production code
## The unique_id should never be something that can change
## https://developers.home-assistant.io/docs/config_entries_config_flow_handler#unique-ids
unique_id=slugify(user_input[CONF_USERNAME])
)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=user_input[CONF_USERNAME],
data=user_input,
Expand Down
16 changes: 0 additions & 16 deletions custom_components/integration_blueprint/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from datetime import timedelta
from typing import TYPE_CHECKING, Any

from homeassistant.exceptions import ConfigEntryAuthFailed
Expand All @@ -12,11 +11,8 @@
IntegrationBlueprintApiClientAuthenticationError,
IntegrationBlueprintApiClientError,
)
from .const import DOMAIN, LOGGER

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant

from .data import IntegrationBlueprintConfigEntry


Expand All @@ -26,18 +22,6 @@ class BlueprintDataUpdateCoordinator(DataUpdateCoordinator):

config_entry: IntegrationBlueprintConfigEntry

def __init__(
self,
hass: HomeAssistant,
) -> None:
"""Initialize."""
super().__init__(
hass=hass,
logger=LOGGER,
name=DOMAIN,
update_interval=timedelta(hours=1),
)

async def _async_update_data(self) -> Any:
"""Update data via library."""
try:
Expand Down
3 changes: 3 additions & 0 deletions custom_components/integration_blueprint/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"auth": "Username/Password is wrong.",
"connection": "Unable to connect to the server.",
"unknown": "Unknown error occurred."
},
"abort": {
"already_configured": "This entry is already configured."
}
}
}

0 comments on commit 4e4be70

Please sign in to comment.