diff --git a/homeassistant/components/flume/__init__.py b/homeassistant/components/flume/__init__.py index 294f50c50e2..9a96233e6a9 100644 --- a/homeassistant/components/flume/__init__.py +++ b/homeassistant/components/flume/__init__.py @@ -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, + ) diff --git a/homeassistant/components/flume/binary_sensor.py b/homeassistant/components/flume/binary_sensor.py index c912c3419d7..2305cd9f23e 100644 --- a/homeassistant/components/flume/binary_sensor.py +++ b/homeassistant/components/flume/binary_sensor.py @@ -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] diff --git a/homeassistant/components/flume/const.py b/homeassistant/components/flume/const.py index 9e932cce4dd..a4e7dba444e 100644 --- a/homeassistant/components/flume/const.py +++ b/homeassistant/components/flume/const.py @@ -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" diff --git a/homeassistant/components/flume/services.yaml b/homeassistant/components/flume/services.yaml new file mode 100644 index 00000000000..e6f3d908a09 --- /dev/null +++ b/homeassistant/components/flume/services.yaml @@ -0,0 +1,7 @@ +list_notifications: + fields: + config_entry: + required: true + selector: + config_entry: + integration: flume diff --git a/homeassistant/components/flume/strings.json b/homeassistant/components/flume/strings.json index 2c1a900c091..5f3021960b5 100644 --- a/homeassistant/components/flume/strings.json +++ b/homeassistant/components/flume/strings.json @@ -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." + } + } + } } }