Flume: Add flume.notifications service (#100621)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
tronikos 2023-10-25 06:34:48 -07:00 committed by GitHub
parent 4447336083
commit 89e2f06304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 7 deletions

View File

@ -2,6 +2,7 @@
from pyflume import FlumeAuth, FlumeDeviceList
from requests import Session
from requests.exceptions import RequestException
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -10,8 +11,14 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import (
HomeAssistant,
ServiceCall,
ServiceResponse,
SupportsResponse,
)
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.selector import ConfigEntrySelector
from .const import (
BASE_TOKEN_FILENAME,
@ -19,8 +26,18 @@ from .const import (
FLUME_AUTH,
FLUME_DEVICES,
FLUME_HTTP_SESSION,
FLUME_NOTIFICATIONS_COORDINATOR,
PLATFORMS,
)
from .coordinator import FlumeNotificationDataUpdateCoordinator
SERVICE_LIST_NOTIFICATIONS = "list_notifications"
CONF_CONFIG_ENTRY = "config_entry"
LIST_NOTIFICATIONS_SERVICE_SCHEMA = vol.All(
{
vol.Required(CONF_CONFIG_ENTRY): ConfigEntrySelector(),
},
)
def _setup_entry(hass: HomeAssistant, entry: ConfigEntry):
@ -59,14 +76,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
flume_auth, flume_devices, http_session = await hass.async_add_executor_job(
_setup_entry, hass, entry
)
notification_coordinator = FlumeNotificationDataUpdateCoordinator(
hass=hass, auth=flume_auth
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
FLUME_DEVICES: flume_devices,
FLUME_AUTH: flume_auth,
FLUME_HTTP_SESSION: http_session,
FLUME_NOTIFICATIONS_COORDINATOR: notification_coordinator,
}
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await async_setup_service(hass)
return True
@ -81,3 +103,29 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
async def async_setup_service(hass: HomeAssistant) -> None:
"""Add the services for the flume integration."""
async def list_notifications(call: ServiceCall) -> ServiceResponse:
"""Return the user notifications."""
entry_id: str = call.data[CONF_CONFIG_ENTRY]
entry: ConfigEntry | None = hass.config_entries.async_get_entry(entry_id)
if not entry:
raise ValueError(f"Invalid config entry: {entry_id}")
if not (flume_domain_data := hass.data[DOMAIN].get(entry_id)):
raise ValueError(f"Config entry not loaded: {entry_id}")
return {
"notifications": flume_domain_data[
FLUME_NOTIFICATIONS_COORDINATOR
].notifications
}
hass.services.async_register(
DOMAIN,
SERVICE_LIST_NOTIFICATIONS,
list_notifications,
schema=LIST_NOTIFICATIONS_SERVICE_SCHEMA,
supports_response=SupportsResponse.ONLY,
)

View File

@ -15,8 +15,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN,
FLUME_AUTH,
FLUME_DEVICES,
FLUME_NOTIFICATIONS_COORDINATOR,
FLUME_TYPE_BRIDGE,
FLUME_TYPE_SENSOR,
KEY_DEVICE_ID,
@ -84,7 +84,6 @@ async def async_setup_entry(
) -> None:
"""Set up a Flume binary sensor.."""
flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]
flume_auth = flume_domain_data[FLUME_AUTH]
flume_devices = flume_domain_data[FLUME_DEVICES]
flume_entity_list: list[
@ -94,9 +93,7 @@ async def async_setup_entry(
connection_coordinator = FlumeDeviceConnectionUpdateCoordinator(
hass=hass, flume_devices=flume_devices
)
notification_coordinator = FlumeNotificationDataUpdateCoordinator(
hass=hass, auth=flume_auth
)
notification_coordinator = flume_domain_data[FLUME_NOTIFICATIONS_COORDINATOR]
flume_devices = get_valid_flume_devices(flume_devices)
for device in flume_devices:
device_id = device[KEY_DEVICE_ID]

View File

@ -29,7 +29,7 @@ FLUME_TYPE_SENSOR = 2
FLUME_AUTH = "flume_auth"
FLUME_HTTP_SESSION = "http_session"
FLUME_DEVICES = "devices"
FLUME_NOTIFICATIONS_COORDINATOR = "notifications_coordinator"
CONF_TOKEN_FILE = "token_filename"
BASE_TOKEN_FILENAME = "FLUME_TOKEN_FILE"

View File

@ -0,0 +1,7 @@
list_notifications:
fields:
config_entry:
required: true
selector:
config_entry:
integration: flume

View File

@ -61,5 +61,17 @@
"name": "30 days"
}
}
},
"services": {
"list_notifications": {
"name": "List notifications",
"description": "Return user notifications.",
"fields": {
"config_entry": {
"name": "Flume",
"description": "The flume config entry for which to return notifications."
}
}
}
}
}