Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion custom_components/prometheus_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
from __future__ import annotations

from dataclasses import dataclass
import logging
from typing import Final, Optional
from typing import TYPE_CHECKING, Final, Optional
from urllib.parse import urljoin

import aiohttp

from homeassistant.const import STATE_PROBLEM, STATE_UNKNOWN
from homeassistant.helpers.reload import async_setup_reload_service

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType

from .const import DOMAIN, PLATFORMS

_LOGGER: Final = logging.getLogger(__name__)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the prometheus-sensor integration."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here and not in async_setup_platform for each individual platform?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_setup_reload_service will clear the set of known entities and then trigger the setup workflow. Calling this multiple times from within an integration introduces a race condition that could lead to a reload only exposing one platform of entities.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair.

return True


@dataclass(frozen=True)
class QueryResult:
value: Optional[float] = None
Expand Down
3 changes: 3 additions & 0 deletions custom_components/prometheus_sensor/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import timedelta
from typing import Final

DOMAIN = "prometheus_sensor"
PLATFORMS = ["binary_sensor", "sensor"]

# Match the default scrape_interval in Prometheus
SCAN_INTERVAL: Final = timedelta(seconds=15)

Expand Down
Loading