Repository: home-assistant/core
File: homeassistant/components/ntfy/notify.py (Python)

Code before:
    _attr_supported_features = NotifyEntityFeature.TITLE

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

    async def publish(self, **kwargs: Any) -> None:
        """Publish a message to a topic."""

        params: dict[str, Any] = kwargs
        delay: timedelta | None = params.get("delay")
        if delay:
            params["delay"] = f"{delay.total_seconds()}s"
            if params.get("email"):
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="delay_no_email",
                )
            if params.get("call"):
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="delay_no_call",
                )
        actions: list[dict[str, Any]] = (
            params.pop(ATTR_VIEW, [])
            + params.pop(ATTR_BROADCAST, [])
            + params.pop(ATTR_HTTP, [])
        )
        actions.sort(key=lambda a: a.pop(ATTR_POSITION, float("inf")))

        if actions:
            if len(actions) > MAX_ACTIONS_ALLOWED:
                raise ServiceValidationError(
                    translation_domain=DOMAIN,
                    translation_key="too_many_actions",
                )
            params["actions"] = [
                ACTIONS_MAP[action.pop(ATTR_ACTION)](**action) for action in actions
            ]

        msg = Message(topic=self.topic, **params)
        try:
            await self.ntfy.publish(msg)
        except NtfyUnauthorizedAuthenticationError as e:
            self.config_entry.async_start_reauth(self.hass)
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="authentication_error",
            ) from e
        except NtfyHTTPError as e:
            raise HomeAssistantError(

Review comment (suggestion):
This currently does not allow reordering the actions, so that e.g. the first action is a `http` action, and the second one is a `view` action.

It would be better to have something like this:
```
data:
  actions:
    - type: view
      url: http://example.org
```

(On a related note, I'm incidentally currently working on proper UI support for exactly this pattern, see home-assistant/home-assistant.io#40847 and the PRs linked there).

Code after:

    async def clear(self, **kwargs: Any) -> None:
        """Clear a message."""

        params: dict[str, Any] = kwargs

        try:
            await self.ntfy.clear(self.topic, params[ATTR_SEQUENCE_ID])
        except NtfyUnauthorizedAuthenticationError as e:
            self.config_entry.async_start_reauth(self.hass)
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="authentication_error",
            ) from e
        except NtfyException as e:
            _LOGGER.debug("Exception:", exc_info=True)
            raise HomeAssistantError(
                translation_key="clear_failed",
                translation_domain=DOMAIN,
            ) from e

    async def delete(self, **kwargs: Any) -> None:
        """Delete a message."""

        params: dict[str, Any] = kwargs

        try:
            await self.ntfy.delete(self.topic, params[ATTR_SEQUENCE_ID])
        except NtfyUnauthorizedAuthenticationError as e:
            self.config_entry.async_start_reauth(self.hass)
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="authentication_error",
            ) from e
        except NtfyException as e:
            _LOGGER.debug("Exception:", exc_info=True)
            raise HomeAssistantError(
                translation_key="delete_failed",
                translation_domain=DOMAIN,
            ) from e