Skip to content
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

Flume: Add flume.list_notifications service #100621

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
50 changes: 49 additions & 1 deletion homeassistant/components/flume/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -10,17 +11,33 @@
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,
DOMAIN,
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):
Expand Down Expand Up @@ -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,
frenck marked this conversation as resolved.
Show resolved Hide resolved
}

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await async_setup_service(hass)

return True

Expand All @@ -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:
tronikos marked this conversation as resolved.
Show resolved Hide resolved
"""Add the services for the flume integration."""

async def 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,
notifications,
schema=LIST_NOTIFICATIONS_SERVICE_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
7 changes: 2 additions & 5 deletions homeassistant/components/flume/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from .const import (
DOMAIN,
FLUME_AUTH,
FLUME_DEVICES,
FLUME_NOTIFICATIONS_COORDINATOR,
FLUME_TYPE_BRIDGE,
FLUME_TYPE_SENSOR,
KEY_DEVICE_ID,
Expand Down Expand Up @@ -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[
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/flume/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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"
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/flume/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
list_notifications:
fields:
config_entry:
required: true
selector:
config_entry:
integration: flume
12 changes: 12 additions & 0 deletions homeassistant/components/flume/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
}
}