Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 108296 - nfandroidtv service notify disappears when restarting home assistant #128958

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions homeassistant/components/nfandroidtv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""The NFAndroidTV integration."""

from notifications_android_tv.notifications import ConnectError, Notifications

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
Expand All @@ -26,14 +23,8 @@

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""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[DOMAIN][entry.entry_id] = entry.data[CONF_HOST]

Check warning on line 27 in homeassistant/components/nfandroidtv/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/__init__.py#L27

Added line #L27 was not covered by tests

hass.async_create_task(
discovery.async_load_platform(
Expand Down
48 changes: 30 additions & 18 deletions homeassistant/components/nfandroidtv/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from typing import Any

from notifications_android_tv import Notifications
from notifications_android_tv.notifications import ConnectError, Notifications

Check warning on line 9 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L9

Added line #L9 was not covered by tests
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import voluptuous as vol
Expand Down Expand Up @@ -59,9 +59,9 @@
"""Get the NFAndroidTV notification service."""
if discovery_info is None:
return None
notify = await hass.async_add_executor_job(Notifications, discovery_info[CONF_HOST])

return NFAndroidTVNotificationService(
notify,
discovery_info[CONF_HOST],
hass.config.is_allowed_path,
)

Expand All @@ -71,15 +71,23 @@

def __init__(
self,
notify: Notifications,
host: str,
is_allowed_path: Any,
) -> None:
"""Initialize the service."""
self.notify = notify
self.host = host

Check warning on line 78 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L78

Added line #L78 was not covered by tests
self.is_allowed_path = is_allowed_path
self.notify: Notifications | None = None

Check warning on line 80 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L80

Added line #L80 was not covered by tests

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:
_LOGGER.warning("Failed to connect to host: %s", self.host)
return

Check warning on line 89 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L84-L89

Added lines #L84 - L89 were not covered by tests

data: dict | None = kwargs.get(ATTR_DATA)
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
duration = None
Expand Down Expand Up @@ -178,18 +186,22 @@
translation_key="invalid_notification_icon",
translation_placeholders={"type": type(icondata).__name__},
)
self.notify.send(
message,
title=title,
duration=duration,
fontsize=fontsize,
position=position,
bkgcolor=bkgcolor,
transparency=transparency,
interrupt=interrupt,
icon=icon,
image_file=image_file,
)

try:
self.notify.send(

Check warning on line 191 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L190-L191

Added lines #L190 - L191 were not covered by tests
message,
title=title,
duration=duration,
fontsize=fontsize,
position=position,
bkgcolor=bkgcolor,
transparency=transparency,
interrupt=interrupt,
icon=icon,
image_file=image_file,
)
except ConnectError:
_LOGGER.warning("Failed to connect to host: %s", self.host)

Check warning on line 204 in homeassistant/components/nfandroidtv/notify.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/nfandroidtv/notify.py#L203-L204

Added lines #L203 - L204 were not covered by tests

def load_file(
self,
Expand Down
Loading