66import logging
77from typing import Any
88
9- from notifications_android_tv import Notifications
9+ from notifications_android_tv . notifications import ConnectError , Notifications
1010import requests
1111from requests .auth import HTTPBasicAuth , HTTPDigestAuth
1212import voluptuous as vol
1919)
2020from homeassistant .const import CONF_HOST
2121from homeassistant .core import HomeAssistant
22- from homeassistant .exceptions import ServiceValidationError
22+ from homeassistant .exceptions import HomeAssistantError , ServiceValidationError
2323from homeassistant .helpers import config_validation as cv
2424from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType
2525
@@ -59,9 +59,9 @@ async def async_get_service(
5959 """Get the NFAndroidTV notification service."""
6060 if discovery_info is None :
6161 return None
62- notify = await hass . async_add_executor_job ( Notifications , discovery_info [ CONF_HOST ])
62+
6363 return NFAndroidTVNotificationService (
64- notify ,
64+ discovery_info [ CONF_HOST ] ,
6565 hass .config .is_allowed_path ,
6666 )
6767
@@ -71,15 +71,24 @@ class NFAndroidTVNotificationService(BaseNotificationService):
7171
7272 def __init__ (
7373 self ,
74- notify : Notifications ,
74+ host : str ,
7575 is_allowed_path : Any ,
7676 ) -> None :
7777 """Initialize the service."""
78- self .notify = notify
78+ self .host = host
7979 self .is_allowed_path = is_allowed_path
80+ self .notify : Notifications | None = None
8081
8182 def send_message (self , message : str , ** kwargs : Any ) -> None :
82- """Send a message to a Android TV device."""
83+ """Send a message to an Android TV device."""
84+ if self .notify is None :
85+ try :
86+ self .notify = Notifications (self .host )
87+ except ConnectError as err :
88+ raise HomeAssistantError (
89+ f"Failed to connect to host: { self .host } "
90+ ) from err
91+
8392 data : dict | None = kwargs .get (ATTR_DATA )
8493 title = kwargs .get (ATTR_TITLE , ATTR_TITLE_DEFAULT )
8594 duration = None
@@ -178,18 +187,22 @@ def send_message(self, message: str, **kwargs: Any) -> None:
178187 translation_key = "invalid_notification_icon" ,
179188 translation_placeholders = {"type" : type (icondata ).__name__ },
180189 )
181- self .notify .send (
182- message ,
183- title = title ,
184- duration = duration ,
185- fontsize = fontsize ,
186- position = position ,
187- bkgcolor = bkgcolor ,
188- transparency = transparency ,
189- interrupt = interrupt ,
190- icon = icon ,
191- image_file = image_file ,
192- )
190+
191+ try :
192+ self .notify .send (
193+ message ,
194+ title = title ,
195+ duration = duration ,
196+ fontsize = fontsize ,
197+ position = position ,
198+ bkgcolor = bkgcolor ,
199+ transparency = transparency ,
200+ interrupt = interrupt ,
201+ icon = icon ,
202+ image_file = image_file ,
203+ )
204+ except ConnectError as err :
205+ raise HomeAssistantError (f"Failed to connect to host: { self .host } " ) from err
193206
194207 def load_file (
195208 self ,
0 commit comments