Skip to content

Add sensors to ntfy integration #145262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
11 changes: 5 additions & 6 deletions homeassistant/components/ntfy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@
NtfyUnauthorizedAuthenticationError,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import DOMAIN
from .coordinator import NtfyConfigEntry, NtfyDataUpdateCoordinator

_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.NOTIFY]


type NtfyConfigEntry = ConfigEntry[Ntfy]
PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool:
Expand Down Expand Up @@ -59,7 +56,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool
translation_key="timeout_error",
) from e

entry.runtime_data = ntfy
coordinator = NtfyDataUpdateCoordinator(hass, entry, ntfy)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
74 changes: 74 additions & 0 deletions homeassistant/components/ntfy/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""DataUpdateCoordinator for ntfy integration."""

from __future__ import annotations

from datetime import timedelta
import logging

from aiontfy import Account as NtfyAccount, Ntfy
from aiontfy.exceptions import (
NtfyConnectionError,
NtfyHTTPError,
NtfyTimeoutError,
NtfyUnauthorizedAuthenticationError,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

type NtfyConfigEntry = ConfigEntry[NtfyDataUpdateCoordinator]


class NtfyDataUpdateCoordinator(DataUpdateCoordinator[NtfyAccount]):
"""Ntfy data update coordinator."""

config_entry: NtfyConfigEntry

def __init__(
self, hass: HomeAssistant, config_entry: NtfyConfigEntry, ntfy: Ntfy
) -> None:
"""Initialize the ntfy data update coordinator."""
super().__init__(
hass,
_LOGGER,
config_entry=config_entry,
name=DOMAIN,
update_interval=timedelta(minutes=15),
)

self.ntfy = ntfy

async def _async_update_data(self) -> NtfyAccount:
"""Fetch account data from ntfy."""

try:
return await self.ntfy.account()
except NtfyUnauthorizedAuthenticationError as e:
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="authentication_error",
) from e
except NtfyHTTPError as e:
_LOGGER.debug("Error %s: %s [%s]", e.code, e.error, e.link)
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="server_error",
translation_placeholders={"error_msg": str(e.error)},
) from e
except NtfyConnectionError as e:
_LOGGER.debug("Error", exc_info=True)
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="connection_error",
) from e
except NtfyTimeoutError as e:
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="timeout_error",
) from e
62 changes: 62 additions & 0 deletions homeassistant/components/ntfy/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,68 @@
"publish": {
"default": "mdi:console-line"
}
},
"sensor": {
"messages": {
"default": "mdi:message-arrow-right-outline"
},
"messages_remaining": {
"default": "mdi:message-plus-outline"
},
"messages_limit": {
"default": "mdi:message-alert-outline"
},
"messages_expiry_duration": {
"default": "mdi:message-text-clock"
},
"emails": {
"default": "mdi:email-arrow-right-outline"
},
"emails_remaining": {
"default": "mdi:email-plus-outline"
},
"emails_limit": {
"default": "mdi:email-alert-outline"
},
"calls": {
"default": "mdi:phone-outgoing"
},
"calls_remaining": {
"default": "mdi:phone-plus"
},
"calls_limit": {
"default": "mdi:phone-alert"
},
"reservations": {
"default": "mdi:lock"
},
"reservations_remaining": {
"default": "mdi:lock-plus"
},
"reservations_limit": {
"default": "mdi:lock-alert"
},
"attachment_total_size": {
"default": "mdi:database-arrow-right"
},
"attachment_total_size_remaining": {
"default": "mdi:database-plus"
},
"attachment_total_size_limit": {
"default": "mdi:database-alert"
},
"attachment_expiry_duration": {
"default": "mdi:cloud-clock"
},
"attachment_file_size": {
"default": "mdi:file-alert"
},
"attachment_bandwidth": {
"default": "mdi:cloud-upload"
},
"tier": {
"default": "mdi:star"
}
}
}
}
5 changes: 3 additions & 2 deletions homeassistant/components/ntfy/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import NtfyConfigEntry
from .const import CONF_TOPIC, DOMAIN
from .coordinator import NtfyConfigEntry

PARALLEL_UPDATES = 0

Expand Down Expand Up @@ -69,9 +69,10 @@ def __init__(
name=subentry.data.get(CONF_NAME, self.topic),
configuration_url=URL(config_entry.data[CONF_URL]) / self.topic,
identifiers={(DOMAIN, f"{config_entry.entry_id}_{subentry.subentry_id}")},
via_device=(DOMAIN, config_entry.entry_id),
)
self.config_entry = config_entry
self.ntfy = config_entry.runtime_data
self.ntfy = config_entry.runtime_data.ntfy

async def async_send_message(self, message: str, title: str | None = None) -> None:
"""Publish a message to a topic."""
Expand Down
Loading