Skip to content

Commit 022546e

Browse files
committed
Add sensors
1 parent a3c0b83 commit 022546e

File tree

8 files changed

+1538
-8
lines changed

8 files changed

+1538
-8
lines changed

homeassistant/components/ntfy/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@
1212
NtfyUnauthorizedAuthenticationError,
1313
)
1414

15-
from homeassistant.config_entries import ConfigEntry
1615
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, Platform
1716
from homeassistant.core import HomeAssistant
1817
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
1918
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2019

2120
from .const import DOMAIN
21+
from .coordinator import NtfyConfigEntry, NtfyDataUpdateCoordinator
2222

2323
_LOGGER = logging.getLogger(__name__)
24-
PLATFORMS: list[Platform] = [Platform.NOTIFY]
25-
26-
27-
type NtfyConfigEntry = ConfigEntry[Ntfy]
24+
PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR]
2825

2926

3027
async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool:
@@ -59,7 +56,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool
5956
translation_key="timeout_error",
6057
) from e
6158

62-
entry.runtime_data = ntfy
59+
coordinator = NtfyDataUpdateCoordinator(hass, entry, ntfy)
60+
await coordinator.async_config_entry_first_refresh()
61+
entry.runtime_data = coordinator
6362

6463
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
6564

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""DataUpdateCoordinator for ntfy integration."""
2+
3+
from __future__ import annotations
4+
5+
from datetime import timedelta
6+
import logging
7+
8+
from aiontfy import Account as NtfyAccount, Ntfy
9+
from aiontfy.exceptions import (
10+
NtfyConnectionError,
11+
NtfyHTTPError,
12+
NtfyTimeoutError,
13+
NtfyUnauthorizedAuthenticationError,
14+
)
15+
16+
from homeassistant.config_entries import ConfigEntry
17+
from homeassistant.core import HomeAssistant
18+
from homeassistant.exceptions import ConfigEntryAuthFailed
19+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
20+
21+
from .const import DOMAIN
22+
23+
_LOGGER = logging.getLogger(__name__)
24+
25+
type NtfyConfigEntry = ConfigEntry[NtfyDataUpdateCoordinator]
26+
27+
28+
class NtfyDataUpdateCoordinator(DataUpdateCoordinator[NtfyAccount]):
29+
"""Ntfy data update coordinator."""
30+
31+
config_entry: NtfyConfigEntry
32+
33+
def __init__(
34+
self, hass: HomeAssistant, config_entry: NtfyConfigEntry, ntfy: Ntfy
35+
) -> None:
36+
"""Initialize the ntfy data update coordinator."""
37+
super().__init__(
38+
hass,
39+
_LOGGER,
40+
config_entry=config_entry,
41+
name=DOMAIN,
42+
update_interval=timedelta(minutes=15),
43+
)
44+
45+
self.ntfy = ntfy
46+
47+
async def _async_update_data(self) -> NtfyAccount:
48+
"""Fetch account data from ntfy."""
49+
50+
try:
51+
return await self.ntfy.account()
52+
except NtfyUnauthorizedAuthenticationError as e:
53+
raise ConfigEntryAuthFailed(
54+
translation_domain=DOMAIN,
55+
translation_key="authentication_error",
56+
) from e
57+
except NtfyHTTPError as e:
58+
_LOGGER.debug("Error %s: %s [%s]", e.code, e.error, e.link)
59+
raise UpdateFailed(
60+
translation_domain=DOMAIN,
61+
translation_key="server_error",
62+
translation_placeholders={"error_msg": str(e.error)},
63+
) from e
64+
except NtfyConnectionError as e:
65+
_LOGGER.debug("Error", exc_info=True)
66+
raise UpdateFailed(
67+
translation_domain=DOMAIN,
68+
translation_key="connection_error",
69+
) from e
70+
except NtfyTimeoutError as e:
71+
raise UpdateFailed(
72+
translation_domain=DOMAIN,
73+
translation_key="timeout_error",
74+
) from e

homeassistant/components/ntfy/icons.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,68 @@
44
"publish": {
55
"default": "mdi:console-line"
66
}
7+
},
8+
"sensor": {
9+
"messages": {
10+
"default": "mdi:message-arrow-right-outline"
11+
},
12+
"messages_remaining": {
13+
"default": "mdi:message-plus-outline"
14+
},
15+
"messages_limit": {
16+
"default": "mdi:message-alert-outline"
17+
},
18+
"messages_expiry_duration": {
19+
"default": "mdi:message-text-clock"
20+
},
21+
"emails": {
22+
"default": "mdi:email-arrow-right-outline"
23+
},
24+
"emails_remaining": {
25+
"default": "mdi:email-plus-outline"
26+
},
27+
"emails_limit": {
28+
"default": "mdi:email-alert-outline"
29+
},
30+
"calls": {
31+
"default": "mdi:phone-outgoing"
32+
},
33+
"calls_remaining": {
34+
"default": "mdi:phone-plus"
35+
},
36+
"calls_limit": {
37+
"default": "mdi:phone-alert"
38+
},
39+
"reservations": {
40+
"default": "mdi:lock"
41+
},
42+
"reservations_remaining": {
43+
"default": "mdi:lock-plus"
44+
},
45+
"reservations_limit": {
46+
"default": "mdi:lock-alert"
47+
},
48+
"attachment_total_size": {
49+
"default": "mdi:database-arrow-right"
50+
},
51+
"attachment_total_size_remaining": {
52+
"default": "mdi:database-plus"
53+
},
54+
"attachment_total_size_limit": {
55+
"default": "mdi:database-alert"
56+
},
57+
"attachment_expiry_duration": {
58+
"default": "mdi:cloud-clock"
59+
},
60+
"attachment_file_size": {
61+
"default": "mdi:file-alert"
62+
},
63+
"attachment_bandwidth": {
64+
"default": "mdi:cloud-upload"
65+
},
66+
"tier": {
67+
"default": "mdi:party-popper"
68+
}
769
}
870
}
971
}

homeassistant/components/ntfy/notify.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
2323
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2424

25-
from . import NtfyConfigEntry
2625
from .const import CONF_TOPIC, DOMAIN
26+
from .coordinator import NtfyConfigEntry
2727

2828
PARALLEL_UPDATES = 0
2929

@@ -69,9 +69,10 @@ def __init__(
6969
name=subentry.data.get(CONF_NAME, self.topic),
7070
configuration_url=URL(config_entry.data[CONF_URL]) / self.topic,
7171
identifiers={(DOMAIN, f"{config_entry.entry_id}_{subentry.subentry_id}")},
72+
via_device=(DOMAIN, config_entry.entry_id),
7273
)
7374
self.config_entry = config_entry
74-
self.ntfy = config_entry.runtime_data
75+
self.ntfy = config_entry.runtime_data.ntfy
7576

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

0 commit comments

Comments
 (0)