mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Fix nfandroidtv service notify disappears when restarting 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 <erik@montnemery.com>
This commit is contained in:
parent
e8a534be9c
commit
4d9843172b
@ -1,11 +1,8 @@
|
|||||||
"""The NFAndroidTV integration."""
|
"""The NFAndroidTV integration."""
|
||||||
|
|
||||||
from notifications_android_tv.notifications import ConnectError, Notifications
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, Platform
|
from homeassistant.const import CONF_HOST, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
from homeassistant.helpers import config_validation as cv, discovery
|
from homeassistant.helpers import config_validation as cv, discovery
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
@ -25,14 +22,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up NFAndroidTV from a config entry."""
|
"""Set up NFAndroidTV from a config entry."""
|
||||||
try:
|
|
||||||
await hass.async_add_executor_job(Notifications, entry.data[CONF_HOST])
|
|
||||||
except ConnectError as ex:
|
|
||||||
raise ConfigEntryNotReady(
|
|
||||||
f"Failed to connect to host: {entry.data[CONF_HOST]}"
|
|
||||||
) from ex
|
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
hass.data[DOMAIN][entry.entry_id] = entry.data[CONF_HOST]
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
discovery.async_load_platform(
|
discovery.async_load_platform(
|
||||||
|
@ -6,7 +6,7 @@ from io import BufferedReader
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from notifications_android_tv import Notifications
|
from notifications_android_tv.notifications import ConnectError, Notifications
|
||||||
import requests
|
import requests
|
||||||
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -19,7 +19,7 @@ from homeassistant.components.notify import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ServiceValidationError
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
@ -59,9 +59,9 @@ async def async_get_service(
|
|||||||
"""Get the NFAndroidTV notification service."""
|
"""Get the NFAndroidTV notification service."""
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
return None
|
return None
|
||||||
notify = await hass.async_add_executor_job(Notifications, discovery_info[CONF_HOST])
|
|
||||||
return NFAndroidTVNotificationService(
|
return NFAndroidTVNotificationService(
|
||||||
notify,
|
discovery_info[CONF_HOST],
|
||||||
hass.config.is_allowed_path,
|
hass.config.is_allowed_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -71,15 +71,24 @@ class NFAndroidTVNotificationService(BaseNotificationService):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
notify: Notifications,
|
host: str,
|
||||||
is_allowed_path: Any,
|
is_allowed_path: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
self.notify = notify
|
self.host = host
|
||||||
self.is_allowed_path = is_allowed_path
|
self.is_allowed_path = is_allowed_path
|
||||||
|
self.notify: Notifications | None = None
|
||||||
|
|
||||||
def send_message(self, message: str, **kwargs: Any) -> None:
|
def send_message(self, message: str, **kwargs: Any) -> None:
|
||||||
"""Send a message to a Android TV device."""
|
"""Send a message to an Android TV device."""
|
||||||
|
if self.notify is None:
|
||||||
|
try:
|
||||||
|
self.notify = Notifications(self.host)
|
||||||
|
except ConnectError as err:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"Failed to connect to host: {self.host}"
|
||||||
|
) from err
|
||||||
|
|
||||||
data: dict | None = kwargs.get(ATTR_DATA)
|
data: dict | None = kwargs.get(ATTR_DATA)
|
||||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||||
duration = None
|
duration = None
|
||||||
@ -178,18 +187,22 @@ class NFAndroidTVNotificationService(BaseNotificationService):
|
|||||||
translation_key="invalid_notification_icon",
|
translation_key="invalid_notification_icon",
|
||||||
translation_placeholders={"type": type(icondata).__name__},
|
translation_placeholders={"type": type(icondata).__name__},
|
||||||
)
|
)
|
||||||
self.notify.send(
|
|
||||||
message,
|
try:
|
||||||
title=title,
|
self.notify.send(
|
||||||
duration=duration,
|
message,
|
||||||
fontsize=fontsize,
|
title=title,
|
||||||
position=position,
|
duration=duration,
|
||||||
bkgcolor=bkgcolor,
|
fontsize=fontsize,
|
||||||
transparency=transparency,
|
position=position,
|
||||||
interrupt=interrupt,
|
bkgcolor=bkgcolor,
|
||||||
icon=icon,
|
transparency=transparency,
|
||||||
image_file=image_file,
|
interrupt=interrupt,
|
||||||
)
|
icon=icon,
|
||||||
|
image_file=image_file,
|
||||||
|
)
|
||||||
|
except ConnectError as err:
|
||||||
|
raise HomeAssistantError(f"Failed to connect to host: {self.host}") from err
|
||||||
|
|
||||||
def load_file(
|
def load_file(
|
||||||
self,
|
self,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user