From eb4d1e3071aabc36395bfefe428c8e23fbbf0b57 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:23:31 +0100 Subject: [PATCH] Add action enums to bring (#190053) --- homeassistant/components/bring/services.py | 48 ++++++++++++++------- tests/components/bring/test_notification.py | 21 +++++---- tests/components/bring/test_services.py | 37 ++++++++-------- 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/homeassistant/components/bring/services.py b/homeassistant/components/bring/services.py index 4427d810eba9..34b6805ab6a8 100644 --- a/homeassistant/components/bring/services.py +++ b/homeassistant/components/bring/services.py @@ -1,5 +1,7 @@ """Actions for Bring! integration.""" +from enum import StrEnum + from bring_api import ( ActivityType, BringAuthException, @@ -11,7 +13,7 @@ import probatio from homeassistant.components.event import EventEntityStateAttribute from homeassistant.components.todo import DOMAIN as TODO_DOMAIN -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import CommonServiceArgument from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import ( @@ -24,18 +26,28 @@ from .const import DOMAIN from .coordinator import BringConfigEntry ATTR_ACTIVITY = "uuid" -ATTR_ITEM_NAME = "item" -ATTR_NOTIFICATION_TYPE = "message" -ATTR_REACTION = "reaction" ATTR_RECEIVER = "publicUserUuid" -SERVICE_PUSH_NOTIFICATION = "send_message" -SERVICE_ACTIVITY_STREAM_REACTION = "send_reaction" + +class BringService(StrEnum): + """Store keys for Bring! services.""" + + SEND_MESSAGE = "send_message" + SEND_REACTION = "send_reaction" + + +class BringServiceArgument(StrEnum): + """Store keys for Bring! service arguments.""" + + MESSAGE = "message" + ITEM = "item" + REACTION = "reaction" + SERVICE_ACTIVITY_STREAM_REACTION_SCHEMA = probatio.Schema( { - probatio.Required(ATTR_ENTITY_ID): cv.entity_id, - probatio.Required(ATTR_REACTION): probatio.All( + probatio.Required(CommonServiceArgument.ENTITY_ID): cv.entity_id, + probatio.Required(BringServiceArgument.REACTION): probatio.All( probatio.Upper, probatio.Coerce(ReactionType), ), @@ -51,15 +63,19 @@ def async_setup_services(hass: HomeAssistant) -> None: """Send a reaction in response to recent activity of a list member.""" if ( - not (state := hass.states.get(call.data[ATTR_ENTITY_ID])) - or not (entity := er.async_get(hass).async_get(call.data[ATTR_ENTITY_ID])) + not (state := hass.states.get(call.data[CommonServiceArgument.ENTITY_ID])) + or not ( + entity := er.async_get(hass).async_get( + call.data[CommonServiceArgument.ENTITY_ID] + ) + ) or not entity.config_entry_id ): raise ServiceValidationError( translation_domain=DOMAIN, translation_key="entity_not_found", translation_placeholders={ - ATTR_ENTITY_ID: call.data[ATTR_ENTITY_ID], + "entity_id": call.data[CommonServiceArgument.ENTITY_ID], }, ) config_entry: BringConfigEntry = service.async_get_config_entry( @@ -72,7 +88,7 @@ def async_setup_services(hass: HomeAssistant) -> None: activity = state.attributes[EventEntityStateAttribute.EVENT_TYPE] - reaction: ReactionType = call.data[ATTR_REACTION] + reaction: ReactionType = call.data[BringServiceArgument.REACTION] if not activity: raise ServiceValidationError( @@ -96,7 +112,7 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, async_send_activity_stream_reaction, SERVICE_ACTIVITY_STREAM_REACTION_SCHEMA, ) @@ -104,13 +120,13 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_PUSH_NOTIFICATION, + BringService.SEND_MESSAGE, entity_domain=TODO_DOMAIN, schema={ - probatio.Required(ATTR_NOTIFICATION_TYPE): probatio.All( + probatio.Required(BringServiceArgument.MESSAGE): probatio.All( probatio.Upper, probatio.Coerce(BringNotificationType) ), - probatio.Optional(ATTR_ITEM_NAME): cv.string, + probatio.Optional(BringServiceArgument.ITEM): cv.string, }, func="async_send_message", ) diff --git a/tests/components/bring/test_notification.py b/tests/components/bring/test_notification.py index 8cb526fe4797..6e65ce422e6f 100644 --- a/tests/components/bring/test_notification.py +++ b/tests/components/bring/test_notification.py @@ -7,11 +7,7 @@ import pytest from pytest_homeassistant.common import MockConfigEntry from homeassistant.components.bring.const import DOMAIN -from homeassistant.components.bring.services import ( - ATTR_ITEM_NAME, - ATTR_NOTIFICATION_TYPE, - SERVICE_PUSH_NOTIFICATION, -) +from homeassistant.components.bring.services import BringService, BringServiceArgument from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant @@ -33,9 +29,9 @@ async def test_send_notification( await hass.services.async_call( DOMAIN, - SERVICE_PUSH_NOTIFICATION, + BringService.SEND_MESSAGE, service_data={ - ATTR_NOTIFICATION_TYPE: "GOING_SHOPPING", + BringServiceArgument.MESSAGE: "GOING_SHOPPING", }, target={ATTR_ENTITY_ID: "todo.einkauf"}, blocking=True, @@ -64,9 +60,9 @@ async def test_send_notification_exception( with pytest.raises(HomeAssistantError) as err: await hass.services.async_call( DOMAIN, - SERVICE_PUSH_NOTIFICATION, + BringService.SEND_MESSAGE, service_data={ - ATTR_NOTIFICATION_TYPE: "GOING_SHOPPING", + BringServiceArgument.MESSAGE: "GOING_SHOPPING", }, target={ATTR_ENTITY_ID: "todo.einkauf"}, blocking=True, @@ -90,8 +86,11 @@ async def test_send_notification_service_validation_error( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_PUSH_NOTIFICATION, - service_data={ATTR_NOTIFICATION_TYPE: "URGENT_MESSAGE", ATTR_ITEM_NAME: ""}, + BringService.SEND_MESSAGE, + service_data={ + BringServiceArgument.MESSAGE: "URGENT_MESSAGE", + BringServiceArgument.ITEM: "", + }, target={ATTR_ENTITY_ID: "todo.einkauf"}, blocking=True, ) diff --git a/tests/components/bring/test_services.py b/tests/components/bring/test_services.py index b6aefc1cb25e..38f01c9b3f52 100644 --- a/tests/components/bring/test_services.py +++ b/tests/components/bring/test_services.py @@ -13,12 +13,9 @@ import pytest from pytest_homeassistant.common import MockConfigEntry from homeassistant.components.bring.const import DOMAIN -from homeassistant.components.bring.services import ( - ATTR_REACTION, - SERVICE_ACTIVITY_STREAM_REACTION, -) +from homeassistant.components.bring.services import BringService, BringServiceArgument from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import CommonServiceArgument from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import entity_registry as er @@ -50,10 +47,10 @@ async def test_send_reaction( await hass.services.async_call( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, service_data={ - ATTR_ENTITY_ID: "event.einkauf_activities", - ATTR_REACTION: reaction, + CommonServiceArgument.ENTITY_ID: "event.einkauf_activities", + BringServiceArgument.REACTION: reaction, }, blocking=True, ) @@ -84,10 +81,10 @@ async def test_send_reaction_exception( with pytest.raises(HomeAssistantError) as err: await hass.services.async_call( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, service_data={ - ATTR_ENTITY_ID: "event.einkauf_activities", - ATTR_REACTION: "heart", + CommonServiceArgument.ENTITY_ID: "event.einkauf_activities", + BringServiceArgument.REACTION: "heart", }, blocking=True, ) @@ -112,10 +109,10 @@ async def test_send_reaction_config_entry_not_loaded( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, service_data={ - ATTR_ENTITY_ID: "event.einkauf_activities", - ATTR_REACTION: "heart", + CommonServiceArgument.ENTITY_ID: "event.einkauf_activities", + BringServiceArgument.REACTION: "heart", }, blocking=True, ) @@ -143,10 +140,10 @@ async def test_send_reaction_unknown_entity( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, service_data={ - ATTR_ENTITY_ID: "event.einkauf_activities", - ATTR_REACTION: "heart", + CommonServiceArgument.ENTITY_ID: "event.einkauf_activities", + BringServiceArgument.REACTION: "heart", }, blocking=True, ) @@ -176,10 +173,10 @@ async def test_send_reaction_not_found( with pytest.raises(HomeAssistantError) as err: await hass.services.async_call( DOMAIN, - SERVICE_ACTIVITY_STREAM_REACTION, + BringService.SEND_REACTION, service_data={ - ATTR_ENTITY_ID: "event.einkauf_activities", - ATTR_REACTION: "heart", + CommonServiceArgument.ENTITY_ID: "event.einkauf_activities", + BringServiceArgument.REACTION: "heart", }, blocking=True, )