Skip to content

Commit 4d98431

Browse files
Fix nfandroidtv service notify disappears when restarting home assistant (home-assistant#128958)
* move connect to android tv host from init to short before sending a message * Don't swallow exceptions * use string literals for exception --------- Co-authored-by: Erik Montnemery <[email protected]>
1 parent e8a534b commit 4d98431

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

homeassistant/components/nfandroidtv/__init__.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
"""The NFAndroidTV integration."""
22

3-
from notifications_android_tv.notifications import ConnectError, Notifications
4-
53
from homeassistant.config_entries import ConfigEntry
64
from homeassistant.const import CONF_HOST, Platform
75
from homeassistant.core import HomeAssistant
8-
from homeassistant.exceptions import ConfigEntryNotReady
96
from homeassistant.helpers import config_validation as cv, discovery
107
from homeassistant.helpers.typing import ConfigType
118

@@ -25,14 +22,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2522

2623
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2724
"""Set up NFAndroidTV from a config entry."""
28-
try:
29-
await hass.async_add_executor_job(Notifications, entry.data[CONF_HOST])
30-
except ConnectError as ex:
31-
raise ConfigEntryNotReady(
32-
f"Failed to connect to host: {entry.data[CONF_HOST]}"
33-
) from ex
34-
3525
hass.data.setdefault(DOMAIN, {})
26+
hass.data[DOMAIN][entry.entry_id] = entry.data[CONF_HOST]
3627

3728
hass.async_create_task(
3829
discovery.async_load_platform(

homeassistant/components/nfandroidtv/notify.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
from typing import Any
88

9-
from notifications_android_tv import Notifications
9+
from notifications_android_tv.notifications import ConnectError, Notifications
1010
import requests
1111
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
1212
import voluptuous as vol
@@ -19,7 +19,7 @@
1919
)
2020
from homeassistant.const import CONF_HOST
2121
from homeassistant.core import HomeAssistant
22-
from homeassistant.exceptions import ServiceValidationError
22+
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
2323
from homeassistant.helpers import config_validation as cv
2424
from 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

Comments
 (0)