14
14
15
15
import logging
16
16
from typing import Any
17
-
18
- from functools import partial
17
+ from contextlib import suppress
19
18
20
19
import requests
21
20
import voluptuous as vol
21
+ import re
22
22
23
23
from homeassistant .components .notify import (
24
24
ATTR_TITLE_DEFAULT ,
30
30
31
31
from homeassistant .components import media_source
32
32
33
+ from homeassistant .helpers .network import NoURLAvailableError , get_url
34
+
33
35
from http import HTTPStatus
34
36
35
37
from homeassistant .const import (
41
43
42
44
_LOGGER = logging .getLogger (__name__ )
43
45
46
+ CAMERA_PROXY_REGEX = re .compile (r"\/api\/camera_proxy\/camera\.(.*)" )
47
+
44
48
45
49
def get_service (hass , config , discovery_info = None ):
46
50
"""Get the HASS Agent notification service."""
@@ -59,37 +63,57 @@ def __init__(self, hass, resource):
59
63
self ._resource = resource
60
64
self ._hass = hass
61
65
62
- def send (self , url , data ):
66
+ def send_request (self , url , data ):
67
+ """Sends the json request"""
63
68
return requests .post (url , json = data , timeout = 10 )
64
69
65
70
async def async_send_message (self , message : str , ** kwargs : Any ):
66
71
"""Send the message to the provided resource."""
67
- _LOGGER .debug ("Preparing notification .. " )
72
+ _LOGGER .debug ("Preparing notification" )
68
73
69
74
title = kwargs .get (ATTR_TITLE , ATTR_TITLE_DEFAULT )
70
75
data = kwargs .get (ATTR_DATA , None )
71
76
72
77
image = data .get ("image" , None )
78
+
73
79
if image is not None :
74
- if media_source .is_media_source_id (image ):
80
+ new_url = None
81
+
82
+ camera_proxy_match = CAMERA_PROXY_REGEX .match (image )
83
+
84
+ if camera_proxy_match is not None :
85
+ camera = self .hass .states .get (f"camera.{ camera_proxy_match .group (1 )} " )
86
+
87
+ if camera is not None :
88
+ external_url = None
89
+ with suppress (NoURLAvailableError ): # external_url not configured
90
+ external_url = get_url (self .hass , allow_internal = False )
91
+
92
+ if external_url is not None :
93
+ access_token = camera .attributes ["access_token" ]
94
+ new_url = f"{ external_url } { image } ?token={ access_token } "
95
+
96
+ elif media_source .is_media_source_id (image ):
75
97
sourced_media = await media_source .async_resolve_media (self .hass , image )
76
98
sourced_media = media_source .async_process_play_media_url (
77
99
self .hass , sourced_media .url
78
100
)
101
+ new_url = sourced_media
79
102
80
- data .update ({"image" : sourced_media })
103
+ if new_url is not None :
104
+ data .update ({"image" : new_url })
81
105
82
106
payload = {"message" : message , "title" : title , "data" : data }
83
107
84
- _LOGGER .debug ("Sending notification .. " )
108
+ _LOGGER .debug ("Sending notification" )
85
109
86
110
try :
87
111
88
112
response = await self .hass .async_add_executor_job (
89
- self .send , self ._resource , payload
113
+ self .send_request , self ._resource , payload
90
114
)
91
115
92
- _LOGGER .debug ("Checking result .. " )
116
+ _LOGGER .debug ("Checking result" )
93
117
94
118
if response .status_code == HTTPStatus .INTERNAL_SERVER_ERROR :
95
119
_LOGGER .error (
@@ -146,5 +170,5 @@ async def async_send_message(self, message: str, **kwargs: Any):
146
170
"Unknown response %d: %s" , response .status_code , response .reason
147
171
)
148
172
149
- except Exception as e :
150
- _LOGGER .debug ("Error sending message: %s" , e )
173
+ except Exception as ex :
174
+ _LOGGER .debug ("Error sending message: %s" , ex )
0 commit comments