1414
1515import logging
1616from typing import Any
17-
18- from functools import partial
17+ from contextlib import suppress
1918
2019import requests
2120import voluptuous as vol
21+ import re
2222
2323from homeassistant .components .notify import (
2424 ATTR_TITLE_DEFAULT ,
3030
3131from homeassistant .components import media_source
3232
33+ from homeassistant .helpers .network import NoURLAvailableError , get_url
34+
3335from http import HTTPStatus
3436
3537from homeassistant .const import (
4143
4244_LOGGER = logging .getLogger (__name__ )
4345
46+ CAMERA_PROXY_REGEX = re .compile (r"\/api\/camera_proxy\/camera\.(.*)" )
47+
4448
4549def get_service (hass , config , discovery_info = None ):
4650 """Get the HASS Agent notification service."""
@@ -59,37 +63,57 @@ def __init__(self, hass, resource):
5963 self ._resource = resource
6064 self ._hass = hass
6165
62- def send (self , url , data ):
66+ def send_request (self , url , data ):
67+ """Sends the json request"""
6368 return requests .post (url , json = data , timeout = 10 )
6469
6570 async def async_send_message (self , message : str , ** kwargs : Any ):
6671 """Send the message to the provided resource."""
67- _LOGGER .debug ("Preparing notification .. " )
72+ _LOGGER .debug ("Preparing notification" )
6873
6974 title = kwargs .get (ATTR_TITLE , ATTR_TITLE_DEFAULT )
7075 data = kwargs .get (ATTR_DATA , None )
7176
7277 image = data .get ("image" , None )
78+
7379 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 ):
7597 sourced_media = await media_source .async_resolve_media (self .hass , image )
7698 sourced_media = media_source .async_process_play_media_url (
7799 self .hass , sourced_media .url
78100 )
101+ new_url = sourced_media
79102
80- data .update ({"image" : sourced_media })
103+ if new_url is not None :
104+ data .update ({"image" : new_url })
81105
82106 payload = {"message" : message , "title" : title , "data" : data }
83107
84- _LOGGER .debug ("Sending notification .. " )
108+ _LOGGER .debug ("Sending notification" )
85109
86110 try :
87111
88112 response = await self .hass .async_add_executor_job (
89- self .send , self ._resource , payload
113+ self .send_request , self ._resource , payload
90114 )
91115
92- _LOGGER .debug ("Checking result .. " )
116+ _LOGGER .debug ("Checking result" )
93117
94118 if response .status_code == HTTPStatus .INTERNAL_SERVER_ERROR :
95119 _LOGGER .error (
@@ -146,5 +170,5 @@ async def async_send_message(self, message: str, **kwargs: Any):
146170 "Unknown response %d: %s" , response .status_code , response .reason
147171 )
148172
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