Add nfandroidtv type checking and allow for strings to be passed to the image and icon data (#108652)

* nfandroidtv - add type checking and allow for strings to be passed to the image and icon data

* nfandroidtv - wrong argument name

* nfandroidtv - put the icon in the wrong varible 🙃

* nfandroidtv - raise ServiceValidationError instead of logging

---------

Co-authored-by: nyangogo <7449028+ioangogo@users.noreply.github.com>
This commit is contained in:
nyangogo 2024-04-24 15:19:44 +01:00 committed by GitHub
parent d0f5e40b19
commit c9ff618ef0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 14 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.components.notify import (
)
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -44,6 +45,7 @@ from .const import (
ATTR_POSITION,
ATTR_TRANSPARENCY,
DEFAULT_TIMEOUT,
DOMAIN,
)
_LOGGER = logging.getLogger(__name__)
@ -133,21 +135,49 @@ class NFAndroidTVNotificationService(BaseNotificationService):
"Invalid interrupt-value: %s", data.get(ATTR_INTERRUPT)
)
if imagedata := data.get(ATTR_IMAGE):
image_file = self.load_file(
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),
)
if isinstance(imagedata, str):
image_file = (
self.load_file(url=imagedata)
if imagedata.startswith("http")
else self.load_file(local_path=imagedata)
)
elif isinstance(imagedata, dict):
image_file = self.load_file(
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),
)
else:
raise ServiceValidationError(
"Invalid image provided",
translation_domain=DOMAIN,
translation_key="invalid_notification_image",
translation_placeholders={"type": type(imagedata).__name__},
)
if icondata := data.get(ATTR_ICON):
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),
)
if isinstance(icondata, str):
icondata = (
self.load_file(url=icondata)
if icondata.startswith("http")
else self.load_file(local_path=icondata)
)
elif isinstance(icondata, dict):
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),
)
else:
raise ServiceValidationError(
"Invalid Icon provided",
translation_domain=DOMAIN,
translation_key="invalid_notification_icon",
translation_placeholders={"type": type(icondata).__name__},
)
self.notify.send(
message,
title=title,

View File

@ -1,4 +1,12 @@
{
"exceptions": {
"invalid_notification_icon": {
"message": "Invalid icon data provided. Got {type}"
},
"invalid_notification_image": {
"message": "Invalid image data provided. Got {type}"
}
},
"config": {
"step": {
"user": {