Add notification service for Bring component (#109222)

* Add notification service for Bring component

* change to async

* update to new library and raise for urgent message without item name

* add icons.json and replace string with reference in strings.json

* Incorporate proposed changes from https://github.com/home-assistant/core/pull/115510

* Remove unnecessary exception, rewrite translations strings

* remove unused constants
This commit is contained in:
Mr. Bubbles 2024-04-24 19:41:46 +02:00 committed by GitHub
parent bc7fa8cf9e
commit 67021be274
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 119 additions and 3 deletions

View File

@ -1,3 +1,11 @@
"""Constants for the Bring! integration."""
from typing import Final
DOMAIN = "bring"
ATTR_SENDER: Final = "sender"
ATTR_ITEM_NAME: Final = "item"
ATTR_NOTIFICATION_TYPE: Final = "message"
SERVICE_PUSH_NOTIFICATION = "send_message"

View File

@ -5,5 +5,8 @@
"default": "mdi:cart"
}
}
},
"services": {
"send_message": "mdi:cellphone-message"
}
}

View File

@ -0,0 +1,23 @@
send_message:
target:
entity:
domain: todo
integration: bring
fields:
message:
example: urgent_message
required: true
default: "going_shopping"
selector:
select:
translation_key: "notification_type_selector"
options:
- "going_shopping"
- "changed_list"
- "shopping_done"
- "urgent_message"
item:
example: Cilantro
required: false
selector:
text:

View File

@ -38,6 +38,42 @@
},
"setup_authentication_exception": {
"message": "Authentication failed for {email}, check your email and password"
},
"notify_missing_argument_item": {
"message": "Failed to call service {service}. 'URGENT_MESSAGE' requires a value @ data['item']. Got None"
},
"notify_request_failed": {
"message": "Failed to send push notification for bring due to a connection error, try again later"
}
},
"services": {
"send_message": {
"name": "[%key:component::notify::services::notify::name%]",
"description": "Send a mobile push notification to members of a shared Bring! list.",
"fields": {
"entity_id": {
"name": "List",
"description": "Bring! list whose members (except sender) will be notified."
},
"message": {
"name": "Notification type",
"description": "Type of push notification to send to list members."
},
"item": {
"name": "Item (Required if message type `Breaking news` selected)",
"description": "Item name to include in a breaking news message e.g. `Breaking news - Please get cilantro!`"
}
}
}
},
"selector": {
"notification_type_selector": {
"options": {
"going_shopping": "I'm going shopping! - Last chance for adjustments",
"changed_list": "List changed - Check it out",
"shopping_done": "Shopping done - you can relax",
"urgent_message": "Breaking news - Please get `item`!"
}
}
}
}

View File

@ -6,7 +6,8 @@ from typing import TYPE_CHECKING
import uuid
from bring_api.exceptions import BringRequestException
from bring_api.types import BringItem, BringItemOperation
from bring_api.types import BringItem, BringItemOperation, BringNotificationType
import voluptuous as vol
from homeassistant.components.todo import (
TodoItem,
@ -16,11 +17,18 @@ from homeassistant.components.todo import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.config_validation import make_entity_service_schema
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .const import (
ATTR_ITEM_NAME,
ATTR_NOTIFICATION_TYPE,
DOMAIN,
SERVICE_PUSH_NOTIFICATION,
)
from .coordinator import BringData, BringDataUpdateCoordinator
@ -46,6 +54,21 @@ async def async_setup_entry(
for bring_list in coordinator.data.values()
)
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_PUSH_NOTIFICATION,
make_entity_service_schema(
{
vol.Required(ATTR_NOTIFICATION_TYPE): vol.All(
vol.Upper, cv.enum(BringNotificationType)
),
vol.Optional(ATTR_ITEM_NAME): cv.string,
}
),
"async_send_message",
)
class BringTodoListEntity(
CoordinatorEntity[BringDataUpdateCoordinator], TodoListEntity
@ -231,3 +254,26 @@ class BringTodoListEntity(
) from e
await self.coordinator.async_refresh()
async def async_send_message(
self,
message: BringNotificationType,
item: str | None = None,
) -> None:
"""Send a push notification to members of a shared bring list."""
try:
await self.coordinator.bring.notify(self._list_uuid, message, item or None)
except BringRequestException as e:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="notify_request_failed",
) from e
except ValueError as e:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="notify_missing_argument_item",
translation_placeholders={
"service": f"{DOMAIN}.{SERVICE_PUSH_NOTIFICATION}",
},
) from e