Skip to content

Commit 8d396b5

Browse files
committed
rework object selector schema
1 parent 3ac62da commit 8d396b5

File tree

3 files changed

+80
-138
lines changed

3 files changed

+80
-138
lines changed

homeassistant/components/ntfy/notify.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ATTR_MARKDOWN = "markdown"
4444
ATTR_PRIORITY = "priority"
4545
ATTR_TAGS = "tags"
46+
ATTR_ACTIONS = "actions"
4647
ATTR_ACTION = "action"
4748
ATTR_VIEW = "view"
4849
ATTR_BROADCAST = "broadcast"
@@ -66,25 +67,24 @@
6667
{
6768
vol.Required(ATTR_LABEL): cv.string,
6869
vol.Optional(ATTR_CLEAR, default=False): cv.boolean,
69-
vol.Optional(ATTR_POSITION): vol.All(vol.Coerce(int), vol.Range(1, 3)),
7070
}
7171
)
7272
VIEW_SCHEMA = ACTION_SCHEMA.extend(
7373
{
74-
vol.Optional(ATTR_ACTION, default="view"): str,
74+
vol.Optional(ATTR_ACTION): vol.All(str, "view"),
7575
vol.Required(ATTR_URL): cv.url,
7676
}
7777
)
7878
BROADCAST_SCHEMA = ACTION_SCHEMA.extend(
7979
{
80-
vol.Optional(ATTR_ACTION, default="broadcast"): str,
80+
vol.Optional(ATTR_ACTION): vol.All(str, "broadcast"),
8181
vol.Optional(ATTR_INTENT): cv.string,
8282
vol.Optional(ATTR_EXTRAS): dict[str, str],
8383
}
8484
)
8585
HTTP_SCHEMA = VIEW_SCHEMA.extend(
8686
{
87-
vol.Optional(ATTR_ACTION, default="http"): str,
87+
vol.Optional(ATTR_ACTION): vol.All(str, "http"),
8888
vol.Optional(ATTR_METHOD): cv.string,
8989
vol.Optional(ATTR_HEADERS): dict[str, str],
9090
vol.Optional(ATTR_BODY): cv.string,
@@ -106,9 +106,9 @@
106106
vol.Optional(ATTR_EMAIL): vol.Email(),
107107
vol.Optional(ATTR_CALL): cv.string,
108108
vol.Optional(ATTR_ICON): vol.All(vol.Url(), vol.Coerce(URL)),
109-
vol.Optional(ATTR_VIEW): vol.All(cv.ensure_list, [VIEW_SCHEMA]),
110-
vol.Optional(ATTR_BROADCAST): vol.All(cv.ensure_list, [BROADCAST_SCHEMA]),
111-
vol.Optional(ATTR_HTTP): vol.All(cv.ensure_list, [HTTP_SCHEMA]),
109+
vol.Optional(ATTR_ACTIONS): vol.All(
110+
cv.ensure_list, [vol.Any(VIEW_SCHEMA, BROADCAST_SCHEMA, HTTP_SCHEMA)]
111+
),
112112
}
113113
)
114114

@@ -164,13 +164,8 @@ async def publish(self, **kwargs: Any) -> None:
164164
translation_domain=DOMAIN,
165165
translation_key="delay_no_call",
166166
)
167-
actions: list[dict[str, Any]] = (
168-
params.pop(ATTR_VIEW, [])
169-
+ params.pop(ATTR_BROADCAST, [])
170-
+ params.pop(ATTR_HTTP, [])
171-
)
172-
actions.sort(key=lambda a: a.pop(ATTR_POSITION, float("inf")))
173167

168+
actions: list[dict[str, Any]] | None = params.get(ATTR_ACTIONS)
174169
if actions:
175170
if len(actions) > MAX_ACTIONS_ALLOWED:
176171
raise ServiceValidationError(

homeassistant/components/ntfy/services.yaml

Lines changed: 54 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -89,101 +89,57 @@ publish:
8989
autocomplete: url
9090
example: https://example.org/logo.png
9191
actions:
92-
collapsed: true
93-
fields:
94-
view:
95-
selector:
96-
object:
97-
label_field: "label"
98-
description_field: "url"
99-
multiple: true
100-
translation_key: view
101-
fields:
102-
label:
103-
selector:
104-
text:
105-
url:
106-
selector:
107-
text:
108-
type: url
109-
clear:
110-
selector:
111-
boolean:
112-
position:
113-
selector:
114-
select:
115-
options:
116-
- 1
117-
- 2
118-
- 3
119-
mode: dropdown
120-
broadcast:
121-
selector:
122-
object:
123-
label_field: "label"
124-
description_field: "intent"
125-
multiple: true
126-
translation_key: broadcast
127-
fields:
128-
label:
129-
selector:
130-
text:
131-
intent:
132-
selector:
133-
text:
134-
extras:
135-
selector:
136-
object:
137-
clear:
138-
selector:
139-
boolean:
140-
position:
141-
selector:
142-
select:
143-
options:
144-
- 1
145-
- 2
146-
- 3
147-
mode: dropdown
148-
http:
149-
selector:
150-
object:
151-
label_field: "label"
152-
description_field: "url"
153-
multiple: true
154-
translation_key: http
155-
fields:
156-
label:
157-
selector:
158-
text:
159-
url:
160-
selector:
161-
text:
162-
type: url
163-
method:
164-
selector:
165-
select:
166-
options:
167-
- GET
168-
- POST
169-
- PUT
170-
- DELETE
171-
custom_value: true
172-
headers:
173-
selector:
174-
object:
175-
body:
176-
selector:
177-
text:
178-
multiline: true
179-
clear:
180-
selector:
181-
boolean:
182-
position:
183-
selector:
184-
select:
185-
options:
186-
- 1
187-
- 2
188-
- 3
189-
mode: dropdown
92+
selector:
93+
object:
94+
label_field: "label"
95+
description_field: "url"
96+
multiple: true
97+
translation_key: actions
98+
fields:
99+
action:
100+
selector:
101+
select:
102+
options:
103+
- value: view
104+
label: Open website/app
105+
- value: broadcast
106+
label: Send Android broadcast
107+
- value: http
108+
label: Send HTTP request
109+
translation_key: action_type
110+
mode: dropdown
111+
label:
112+
selector:
113+
text:
114+
required: true
115+
url:
116+
selector:
117+
text:
118+
type: url
119+
required: true
120+
intent:
121+
selector:
122+
text:
123+
required: true
124+
extras:
125+
selector:
126+
object:
127+
method:
128+
selector:
129+
select:
130+
options:
131+
- GET
132+
- POST
133+
- PUT
134+
- DELETE
135+
custom_value: true
136+
headers:
137+
selector:
138+
object:
139+
body:
140+
selector:
141+
text:
142+
multiline: true
143+
clear:
144+
selector:
145+
boolean:

homeassistant/components/ntfy/strings.json

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,11 @@
338338
"http": {
339339
"name": "Send HTTP request",
340340
"description": "Adds an 'http' button action that sends an HTTP request when the action button is tapped. See the documentation for details: https://docs.ntfy.sh/publish/#send-http-request"
341-
}
342-
},
343-
"sections": {
341+
},
342+
344343
"actions": {
345344
"name": "Action buttons",
346-
"description": "Up to three actions ('view', 'broadcast', or 'http') can be defined for a notification. These appear as buttons below the notification content. Actions are executed when the corresponding button is tapped or clicked."
345+
"description": "Up to three actions ('view', 'broadcast', or 'http') can be added as buttons below the notification. Actions are executed when the corresponding button is tapped or clicked."
347346
}
348347
}
349348
}
@@ -358,32 +357,24 @@
358357
"5": "Maximum"
359358
}
360359
},
361-
"view": {
362-
"fields": {
363-
"label": "Label of the action button",
364-
"url": "URL to open when action is tapped",
365-
"clear": "Clear notification after action button is tapped",
366-
"position": "Position of the action button"
367-
}
368-
},
369-
"broadcast": {
370-
"fields": {
371-
"label": "[%key:component::ntfy::selector::view::fields::label%]",
372-
"intent": "Android intent to send when action is tapped",
373-
"extras": "Extras to include in the intent (key-value pairs)",
374-
"clear": "[%key:component::ntfy::selector::view::fields::clear%]",
375-
"position": "[%key:component::ntfy::selector::view::fields::position%]"
360+
"action_type": {
361+
"options": {
362+
"view": "Open website/app",
363+
"broadcast": "Send Android broadcast",
364+
"http": "Send HTTP request"
376365
}
377366
},
378-
"http": {
367+
"actions": {
379368
"fields": {
380-
"label": "[%key:component::ntfy::selector::view::fields::label%]",
381-
"url": "URL to which the HTTP request will be sent",
382-
"method": "HTTP method to use for request",
383-
"headers": "Additional HTTP headers (key-value pairs)",
384-
"body": "Body of the HTTP request",
385-
"clear": "[%key:component::ntfy::selector::view::fields::clear%]",
386-
"position": "[%key:component::ntfy::selector::view::fields::position%]"
369+
"label": "Label of the action button",
370+
"action": "Action type",
371+
"url": "URL to open for the 'view' action or to request for the 'http' action",
372+
"intent": "Android intent to send when the 'broadcast' action is triggered",
373+
"extras": "Extras to include in the intent as key-value pairs ('broadcast' action only)",
374+
"method": "HTTP method to use for the 'http' action",
375+
"headers": "Additional HTTP headers as key-value pairs ('http' action only)",
376+
"body": "Body of the HTTP request ('http' action only)",
377+
"clear": "Clear notification after action button is tapped"
387378
}
388379
}
389380
}

0 commit comments

Comments
 (0)