2424 PLATFORM_SCHEMA ,
2525 BaseNotificationService ,
2626)
27- """from homeassistant.const import (
28- CONF_RESOURCE,
29- HTTP_BAD_REQUEST,
30- HTTP_INTERNAL_SERVER_ERROR,
31- HTTP_OK,
32- )"""
27+
28+ from http import HTTPStatus
29+
3330from homeassistant .const import (
3431 CONF_RESOURCE ,
3532)
@@ -51,6 +48,8 @@ def get_service(hass, config, discovery_info=None):
5148 """Get the HASS Agent notification service."""
5249 resource = config .get (CONF_RESOURCE )
5350
51+ _LOGGER .info ("Service created" )
52+
5453 return HassAgentNotificationService (
5554 hass ,
5655 resource
@@ -71,6 +70,8 @@ def __init__(
7170
7271 def send_message (self , message = "" , title = "" , ** kwargs ):
7372 """Send the message to the provided resource."""
73+ _LOGGER .debug ("Preparing notification .." )
74+
7475 data = kwargs .get (ATTR_DATA , None )
7576 image = data .get (ATTR_IMAGE ) if data is not None and ATTR_IMAGE in data else None
7677 duration = data .get (ATTR_DURATION ) if data is not None and ATTR_DURATION in data else 0
@@ -82,17 +83,33 @@ def send_message(self, message="", title="", **kwargs):
8283 'duration' : duration
8384 })
8485
86+ _LOGGER .debug ("Sending notification .." )
87+
8588 response = requests .post (
8689 self ._resource ,
8790 json = payload ,
8891 timeout = 10
8992 )
9093
91- """ if HTTP_INTERNAL_SERVER_ERROR <= response.status_code < 600:
92- _LOGGER.exception("Server error. Response %d: %s:", response.status_code, response.reason)
93- elif HTTP_BAD_REQUEST <= response.status_code < HTTP_INTERNAL_SERVER_ERROR:
94- _LOGGER.exception("Client error. Response %d: %s:", response.status_code, response.reason)
95- elif HTTP_OK <= response.status_code < 300:
96- _LOGGER.debug("Success. Response %d: %s:", response.status_code, response.reason)
94+ _LOGGER .debug ("Checking result .." )
95+
96+ if response .status_code == HTTPStatus .INTERNAL_SERVER_ERROR :
97+ _LOGGER .exception ("Server error. Response %d: %s" , response .status_code , response .reason )
98+ elif response .status_code == HTTPStatus .BAD_REQUEST :
99+ _LOGGER .exception ("Client error (bad request). Response %d: %s" , response .status_code , response .reason )
100+ elif response .status_code == HTTPStatus .NOT_FOUND :
101+ _LOGGER .exception ("Server error (not found). Response %d: %s" , response .status_code , response .reason )
102+ elif response .status_code == HTTPStatus .METHOD_NOT_ALLOWED :
103+ _LOGGER .exception ("Server error (method not allowed). Response %d" , response .status_code )
104+ elif response .status_code == HTTPStatus .REQUEST_TIMEOUT :
105+ _LOGGER .exception ("Server error (request timeout). Response %d: %s" , response .status_code , response .reason )
106+ elif response .status_code == HTTPStatus .NOT_IMPLEMENTED :
107+ _LOGGER .exception ("Server error (not implemented). Response %d: %s" , response .status_code , response .reason )
108+ elif response .status_code == HTTPStatus .SERVICE_UNAVAILABLE :
109+ _LOGGER .exception ("Server error (service unavailable). Response %d" , response .status_code )
110+ elif response .status_code == HTTPStatus .GATEWAY_TIMEOUT :
111+ _LOGGER .exception ("Network error (gateway timeout). Response %d: %s" , response .status_code , response .reason )
112+ elif response .status_code == HTTPStatus .OK :
113+ _LOGGER .debug ("Success. Response %d: %s" , response .status_code , response .reason )
97114 else :
98- _LOGGER.debug("Response %d: %s: ", response.status_code, response.reason)"""
115+ _LOGGER .debug ("Unknown response %d: %s" , response .status_code , response .reason )
0 commit comments