Implement minor Colorfix alterations (#42521)
* Added logic to ensure one of ATTR_URL or ATTR_PATH is provided using cv, updated tests to reflect this * Added missing explicit None returns
This commit is contained in:
parent
0860ce4099
commit
6e43d489ca
2 changed files with 18 additions and 15 deletions
|
@ -27,12 +27,15 @@ import homeassistant.helpers.config_validation as cv
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Extend the existing light.turn_on service schema
|
# Extend the existing light.turn_on service schema
|
||||||
SERVICE_SCHEMA = cv.make_entity_service_schema(
|
SERVICE_SCHEMA = vol.All(
|
||||||
|
cv.has_at_least_one_key(ATTR_URL, ATTR_PATH),
|
||||||
|
cv.make_entity_service_schema(
|
||||||
{
|
{
|
||||||
**LIGHT_TURN_ON_SCHEMA,
|
**LIGHT_TURN_ON_SCHEMA,
|
||||||
vol.Exclusive(ATTR_PATH, "color_extractor"): cv.isfile,
|
vol.Exclusive(ATTR_PATH, "color_extractor"): cv.isfile,
|
||||||
vol.Exclusive(ATTR_URL, "color_extractor"): cv.url,
|
vol.Exclusive(ATTR_URL, "color_extractor"): cv.url,
|
||||||
}
|
}
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,10 +66,6 @@ async def async_setup(hass, hass_config):
|
||||||
"""Decide which color_extractor method to call based on service."""
|
"""Decide which color_extractor method to call based on service."""
|
||||||
service_data = dict(service_call.data)
|
service_data = dict(service_call.data)
|
||||||
|
|
||||||
if ATTR_URL not in service_data and ATTR_PATH not in service_data:
|
|
||||||
_LOGGER.error("Missing either required %s or %s key", ATTR_URL, ATTR_PATH)
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if ATTR_URL in service_data:
|
if ATTR_URL in service_data:
|
||||||
image_type = "URL"
|
image_type = "URL"
|
||||||
|
@ -110,7 +109,7 @@ async def async_setup(hass, hass_config):
|
||||||
"External URL '%s' is not allowed, please add to 'allowlist_external_urls'",
|
"External URL '%s' is not allowed, please add to 'allowlist_external_urls'",
|
||||||
url,
|
url,
|
||||||
)
|
)
|
||||||
return
|
return None
|
||||||
|
|
||||||
_LOGGER.debug("Getting predominant RGB from image URL '%s'", url)
|
_LOGGER.debug("Getting predominant RGB from image URL '%s'", url)
|
||||||
|
|
||||||
|
@ -123,7 +122,7 @@ async def async_setup(hass, hass_config):
|
||||||
|
|
||||||
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
|
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
|
||||||
_LOGGER.error("Failed to get ColorThief image due to HTTPError: %s", err)
|
_LOGGER.error("Failed to get ColorThief image due to HTTPError: %s", err)
|
||||||
return
|
return None
|
||||||
|
|
||||||
content = await response.content.read()
|
content = await response.content.read()
|
||||||
|
|
||||||
|
@ -140,7 +139,7 @@ async def async_setup(hass, hass_config):
|
||||||
"File path '%s' is not allowed, please add to 'allowlist_external_dirs'",
|
"File path '%s' is not allowed, please add to 'allowlist_external_dirs'",
|
||||||
file_path,
|
file_path,
|
||||||
)
|
)
|
||||||
return
|
return None
|
||||||
|
|
||||||
_LOGGER.debug("Getting predominant RGB from file path '%s'", file_path)
|
_LOGGER.debug("Getting predominant RGB from file path '%s'", file_path)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import io
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import pytest
|
import pytest
|
||||||
|
from voluptuous.error import MultipleInvalid
|
||||||
|
|
||||||
from homeassistant.components.color_extractor import (
|
from homeassistant.components.color_extractor import (
|
||||||
ATTR_PATH,
|
ATTR_PATH,
|
||||||
|
@ -103,7 +104,10 @@ async def test_missing_url_and_path(hass):
|
||||||
ATTR_ENTITY_ID: LIGHT_ENTITY,
|
ATTR_ENTITY_ID: LIGHT_ENTITY,
|
||||||
}
|
}
|
||||||
|
|
||||||
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, service_data, blocking=True)
|
with pytest.raises(MultipleInvalid):
|
||||||
|
await hass.services.async_call(
|
||||||
|
DOMAIN, SERVICE_TURN_ON, service_data, blocking=True
|
||||||
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# check light is still off, unchanged due to bad parameters on service call
|
# check light is still off, unchanged due to bad parameters on service call
|
||||||
|
|
Loading…
Add table
Reference in a new issue