Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Oct 1, 2019
1 parent b99cd2b commit 835c845
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
11 changes: 5 additions & 6 deletions homeassistant/components/binary_sensor/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import voluptuous as vol

from homeassistant.components.automation import state as state_automation
from homeassistant.components.device_automation import (
POSITIVE_TIME_DELTA_DICT,
TRIGGER_BASE_SCHEMA,
)
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
from homeassistant.components.device_automation.const import (
CONF_TURNED_OFF,
CONF_TURNED_ON,
Expand Down Expand Up @@ -178,7 +175,7 @@
{
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In(TURNED_OFF + TURNED_ON),
vol.Optional(CONF_FOR): POSITIVE_TIME_DELTA_DICT,
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
}
)

Expand Down Expand Up @@ -247,5 +244,7 @@ async def async_get_triggers(hass, device_id):
async def async_get_trigger_capabilities(hass, trigger):
"""List trigger capabilities."""
return {
"extra_fields": vol.Schema({vol.Optional(CONF_FOR): POSITIVE_TIME_DELTA_DICT})
"extra_fields": vol.Schema(
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
)
}
23 changes: 5 additions & 18 deletions homeassistant/components/device_automation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Helpers for device automations."""
import asyncio
from datetime import timedelta
import logging

import voluptuous as vol
Expand All @@ -27,6 +26,7 @@
)

TYPES = {
# platform name, get automations function, get capabilities function
"trigger": (
"device_trigger",
"async_get_triggers",
Expand All @@ -40,10 +40,6 @@
"action": ("device_action", "async_get_actions", "async_get_action_capabilities"),
}

POSITIVE_TIME_DELTA_DICT = vol.All(
vol.Any(timedelta, cv.time_period_dict), cv.positive_timedelta
)


async def async_setup(hass, config):
"""Set up device automation."""
Expand All @@ -67,7 +63,7 @@ async def _async_get_device_automation_platform(hass, domain, automation_type):
Throws InvalidDeviceAutomationConfig if the integration is not found or does not support device automation.
"""
platform_name, _, _ = TYPES[automation_type]
platform_name = TYPES[automation_type][0]
try:
integration = await async_get_integration(hass, domain)
platform = integration.get_platform(platform_name)
Expand Down Expand Up @@ -102,7 +98,7 @@ async def _async_get_device_automations_from_domain(
except InvalidDeviceAutomationConfig:
return None

_, function_name, _ = TYPES[automation_type]
function_name = TYPES[automation_type][1]

return await getattr(platform, function_name)(hass, device_id)

Expand Down Expand Up @@ -140,15 +136,6 @@ async def _async_get_device_automations(hass, automation_type, device_id):
return automations


def _custom_serializer(schema):
import voluptuous_serialize

if schema == POSITIVE_TIME_DELTA_DICT:
return {"type": "positive_time_delta_dict"}

return voluptuous_serialize.UNSUPPORTED


async def _async_get_device_automation_capabilities(hass, automation_type, automation):
"""List device automations."""
try:
Expand All @@ -158,7 +145,7 @@ async def _async_get_device_automation_capabilities(hass, automation_type, autom
except InvalidDeviceAutomationConfig:
return {}

_, _, function_name = TYPES[automation_type]
function_name = TYPES[automation_type][2]

if not hasattr(platform, function_name):
# The device automation has no capabilities
Expand All @@ -175,7 +162,7 @@ async def _async_get_device_automation_capabilities(hass, automation_type, autom
capabilities["extra_fields"] = []
else:
capabilities["extra_fields"] = voluptuous_serialize.convert(
extra_fields, custom_serializer=_custom_serializer
extra_fields, custom_serializer=cv.custom_serializer
)

return capabilities
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/device_automation/toggle_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from homeassistant.core import Context, HomeAssistant, CALLBACK_TYPE
from homeassistant.components.automation import state, AutomationActionType
from homeassistant.components.device_automation import POSITIVE_TIME_DELTA_DICT
from homeassistant.components.device_automation.const import (
CONF_IS_OFF,
CONF_IS_ON,
Expand Down Expand Up @@ -85,7 +84,7 @@
{
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TURNED_OFF, CONF_TURNED_ON]),
vol.Optional(CONF_FOR): POSITIVE_TIME_DELTA_DICT,
vol.Optional(CONF_FOR): cv.positive_time_period_dict,
}
)

Expand Down Expand Up @@ -215,5 +214,7 @@ async def async_get_triggers(
async def async_get_trigger_capabilities(hass: HomeAssistant, trigger: dict) -> dict:
"""List trigger capabilities."""
return {
"extra_fields": vol.Schema({vol.Optional(CONF_FOR): POSITIVE_TIME_DELTA_DICT})
"extra_fields": vol.Schema(
{vol.Optional(CONF_FOR): cv.positive_time_period_dict}
)
}
15 changes: 15 additions & 0 deletions homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ def positive_timedelta(value: timedelta) -> timedelta:
return value


positive_time_period_dict = vol.All(
vol.Any(timedelta, time_period_dict), positive_timedelta
)


def remove_falsy(value: List[T]) -> List[T]:
"""Remove falsy values from a list."""
return [v for v in value if v]
Expand Down Expand Up @@ -690,6 +695,16 @@ def validator(value):
return validator


def custom_serializer(schema):
"""Serialize additional types for voluptuous_serialize."""
import voluptuous_serialize

if schema == positive_time_period_dict:
return {"type": "positive_time_period_dict"}

return voluptuous_serialize.UNSUPPORTED


# Schemas
PLATFORM_SCHEMA = vol.Schema(
{
Expand Down
2 changes: 1 addition & 1 deletion tests/components/binary_sensor/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_delta_dict"}
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/device_automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async def test_websocket_get_trigger_capabilities(
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_delta_dict"}
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}

Expand Down
2 changes: 1 addition & 1 deletion tests/components/light/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_delta_dict"}
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/switch/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_delta_dict"}
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
Expand Down

0 comments on commit 835c845

Please sign in to comment.