Template platform tweaks (#32037)

* Remove unnecessary boolean return from platform setup

* Fix template cover validation
pull/32041/head
Paulus Schoutsen 2020-02-20 12:14:31 -08:00 committed by GitHub
parent 2e35190aff
commit ce710f1e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 64 deletions

View File

@ -103,12 +103,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
attribute_templates,
)
)
if not sensors:
_LOGGER.error("No sensors added")
return False
async_add_entities(sensors)
return True
class BinarySensorTemplate(BinarySensorDevice):

View File

@ -65,30 +65,33 @@ TILT_FEATURES = (
| SUPPORT_SET_TILT_POSITION
)
COVER_SCHEMA = vol.Schema(
{
vol.Inclusive(OPEN_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
vol.Inclusive(CLOSE_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
vol.Optional(STOP_ACTION): cv.SCRIPT_SCHEMA,
vol.Exclusive(
CONF_POSITION_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
): cv.template,
vol.Exclusive(
CONF_VALUE_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Optional(CONF_POSITION_TEMPLATE): cv.template,
vol.Optional(CONF_TILT_TEMPLATE): cv.template,
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_TILT_OPTIMISTIC): cv.boolean,
vol.Optional(POSITION_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(TILT_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
}
COVER_SCHEMA = vol.All(
vol.Schema(
{
vol.Inclusive(OPEN_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
vol.Inclusive(CLOSE_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
vol.Optional(STOP_ACTION): cv.SCRIPT_SCHEMA,
vol.Exclusive(
CONF_POSITION_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
): cv.template,
vol.Exclusive(
CONF_VALUE_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Optional(CONF_POSITION_TEMPLATE): cv.template,
vol.Optional(CONF_TILT_TEMPLATE): cv.template,
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
vol.Optional(CONF_TILT_OPTIMISTIC): cv.boolean,
vol.Optional(POSITION_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(TILT_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
}
),
cv.has_at_least_one_key(OPEN_ACTION, POSITION_ACTION),
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -118,12 +121,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
optimistic = device_config.get(CONF_OPTIMISTIC)
tilt_optimistic = device_config.get(CONF_TILT_OPTIMISTIC)
if position_action is None and open_action is None:
_LOGGER.error(
"Must specify at least one of %s" or "%s", OPEN_ACTION, POSITION_ACTION
)
continue
templates = {
CONF_VALUE_TEMPLATE: state_template,
CONF_POSITION_TEMPLATE: position_template,
@ -160,12 +157,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
entity_ids,
)
)
if not covers:
_LOGGER.error("No covers added")
return False
async_add_entities(covers)
return True
class CoverTemplate(CoverDevice):

View File

@ -131,12 +131,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
)
)
if not lights:
_LOGGER.error("No lights added")
return False
async_add_entities(lights)
return True
class LightTemplate(Light):

View File

@ -93,12 +93,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
)
)
if not switches:
_LOGGER.error("No switches added")
return False
async_add_entities(switches)
return True
class SwitchTemplate(SwitchDevice):

View File

@ -270,26 +270,22 @@ async def test_template_mutex(hass, calls):
assert hass.states.async_all() == []
async def test_template_open_or_position(hass, calls):
async def test_template_open_or_position(hass, caplog):
"""Test that at least one of open_cover or set_position is used."""
with assert_setup_component(1, "cover"):
assert await setup.async_setup_component(
hass,
"cover",
{
"cover": {
"platform": "template",
"covers": {
"test_template_cover": {"value_template": "{{ 1 == 1 }}"}
},
}
},
)
await hass.async_start()
assert await setup.async_setup_component(
hass,
"cover",
{
"cover": {
"platform": "template",
"covers": {"test_template_cover": {"value_template": "{{ 1 == 1 }}"}},
}
},
)
await hass.async_block_till_done()
assert hass.states.async_all() == []
assert "Invalid config for [cover.template]" in caplog.text
async def test_template_open_and_close(hass, calls):