Add Notifications for Android TV icon support (#60159)

* Add icon support

* Sort imports

* Sort imports correctly

* Satisfy pylint

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Redah 2021-11-25 16:40:26 +01:00 committed by GitHub
parent 6b9c2d8295
commit 3399c90936
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 32 deletions

View file

@ -17,12 +17,20 @@ ATTR_TRANSPARENCY = "transparency"
ATTR_COLOR = "color"
ATTR_BKGCOLOR = "bkgcolor"
ATTR_INTERRUPT = "interrupt"
ATTR_FILE = "file"
# Attributes contained in file
ATTR_FILE_URL = "url"
ATTR_FILE_PATH = "path"
ATTR_FILE_USERNAME = "username"
ATTR_FILE_PASSWORD = "password"
ATTR_FILE_AUTH = "auth"
ATTR_IMAGE = "image"
# Attributes contained in image
ATTR_IMAGE_URL = "url"
ATTR_IMAGE_PATH = "path"
ATTR_IMAGE_USERNAME = "username"
ATTR_IMAGE_PASSWORD = "password"
ATTR_IMAGE_AUTH = "auth"
ATTR_ICON = "icon"
# Attributes contained in icon
ATTR_ICON_URL = "url"
ATTR_ICON_PATH = "path"
ATTR_ICON_USERNAME = "username"
ATTR_ICON_PASSWORD = "password"
ATTR_ICON_AUTH = "auth"
# Any other value or absence of 'auth' lead to basic authentication being used
ATTR_FILE_AUTH_DIGEST = "digest"
ATTR_IMAGE_AUTH_DIGEST = "digest"
ATTR_ICON_AUTH_DIGEST = "digest"

View file

@ -16,7 +16,7 @@ from homeassistant.components.notify import (
PLATFORM_SCHEMA,
BaseNotificationService,
)
from homeassistant.const import ATTR_ICON, CONF_HOST, CONF_TIMEOUT
from homeassistant.const import CONF_HOST, CONF_TIMEOUT
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -24,14 +24,21 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
ATTR_COLOR,
ATTR_DURATION,
ATTR_FILE,
ATTR_FILE_AUTH,
ATTR_FILE_AUTH_DIGEST,
ATTR_FILE_PASSWORD,
ATTR_FILE_PATH,
ATTR_FILE_URL,
ATTR_FILE_USERNAME,
ATTR_FONTSIZE,
ATTR_ICON,
ATTR_ICON_AUTH,
ATTR_ICON_AUTH_DIGEST,
ATTR_ICON_PASSWORD,
ATTR_ICON_PATH,
ATTR_ICON_URL,
ATTR_ICON_USERNAME,
ATTR_IMAGE,
ATTR_IMAGE_AUTH,
ATTR_IMAGE_AUTH_DIGEST,
ATTR_IMAGE_PASSWORD,
ATTR_IMAGE_PATH,
ATTR_IMAGE_URL,
ATTR_IMAGE_USERNAME,
ATTR_INTERRUPT,
ATTR_POSITION,
ATTR_TRANSPARENCY,
@ -158,22 +165,23 @@ class NFAndroidTVNotificationService(BaseNotificationService):
_LOGGER.warning(
"Invalid interrupt-value: %s", str(data.get(ATTR_INTERRUPT))
)
filedata = data.get(ATTR_FILE) if data else None
if filedata is not None:
if ATTR_ICON in filedata:
icon = self.load_file(
url=filedata[ATTR_ICON],
local_path=filedata.get(ATTR_FILE_PATH),
username=filedata.get(ATTR_FILE_USERNAME),
password=filedata.get(ATTR_FILE_PASSWORD),
auth=filedata.get(ATTR_FILE_AUTH),
)
imagedata = data.get(ATTR_IMAGE) if data else None
if imagedata is not None:
image_file = self.load_file(
url=filedata.get(ATTR_FILE_URL),
local_path=filedata.get(ATTR_FILE_PATH),
username=filedata.get(ATTR_FILE_USERNAME),
password=filedata.get(ATTR_FILE_PASSWORD),
auth=filedata.get(ATTR_FILE_AUTH),
url=imagedata.get(ATTR_IMAGE_URL),
local_path=imagedata.get(ATTR_IMAGE_PATH),
username=imagedata.get(ATTR_IMAGE_USERNAME),
password=imagedata.get(ATTR_IMAGE_PASSWORD),
auth=imagedata.get(ATTR_IMAGE_AUTH),
)
icondata = data.get(ATTR_ICON) if data else None
if icondata is not None:
icon = self.load_file(
url=icondata.get(ATTR_ICON_URL),
local_path=icondata.get(ATTR_ICON_PATH),
username=icondata.get(ATTR_ICON_USERNAME),
password=icondata.get(ATTR_ICON_PASSWORD),
auth=icondata.get(ATTR_ICON_AUTH),
)
self.notify.send(
message,
@ -203,7 +211,7 @@ class NFAndroidTVNotificationService(BaseNotificationService):
if username is not None and password is not None:
# Use digest or basic authentication
auth_: HTTPDigestAuth | HTTPBasicAuth
if ATTR_FILE_AUTH_DIGEST == auth:
if auth in (ATTR_IMAGE_AUTH_DIGEST, ATTR_ICON_AUTH_DIGEST):
auth_ = HTTPDigestAuth(username, password)
else:
auth_ = HTTPBasicAuth(username, password)