Skip to content

Commit

Permalink
Address review comments, do not validate schemas twice
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Oct 2, 2019
1 parent 835c845 commit 79d70b1
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 35 deletions.
9 changes: 7 additions & 2 deletions homeassistant/components/automation/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
TRIGGER_BASE_SCHEMA,
async_get_device_automation_platform,
)
from homeassistant.const import CONF_DOMAIN


# mypy: allow-untyped-defs, no-check-untyped-defs
Expand All @@ -14,11 +15,15 @@

async def async_validate_trigger_config(hass, config):
"""Validate config."""
platform = await async_get_device_automation_platform(hass, config, "trigger")
platform = await async_get_device_automation_platform(
hass, config[CONF_DOMAIN], "trigger"
)
return platform.TRIGGER_SCHEMA(config)


async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for trigger."""
platform = await async_get_device_automation_platform(hass, config, "trigger")
platform = await async_get_device_automation_platform(
hass, config[CONF_DOMAIN], "trigger"
)
return await platform.async_attach_trigger(hass, config, action, automation_info)
1 change: 0 additions & 1 deletion homeassistant/components/binary_sensor/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@

async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
trigger_type = config[CONF_TYPE]
if trigger_type in TURNED_ON:
from_state = "off"
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/deconz/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ def _get_deconz_event_from_device_id(hass, device_id):

async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)

device_registry = await hass.helpers.device_registry.async_get_registry()
device = device_registry.async_get(config[CONF_DEVICE_ID])

Expand Down
20 changes: 4 additions & 16 deletions homeassistant/components/device_automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

import voluptuous as vol
import voluptuous_serialize

from homeassistant.const import CONF_PLATFORM, CONF_DOMAIN, CONF_DEVICE_ID
from homeassistant.components import websocket_api
Expand Down Expand Up @@ -58,7 +59,7 @@ async def async_setup(hass, config):
return True


async def _async_get_device_automation_platform(hass, domain, automation_type):
async def async_get_device_automation_platform(hass, domain, automation_type):
"""Load device automation platform for integration.
Throws InvalidDeviceAutomationConfig if the integration is not found or does not support device automation.
Expand All @@ -77,22 +78,12 @@ async def _async_get_device_automation_platform(hass, domain, automation_type):
return platform


async def async_get_device_automation_platform(hass, config, automation_type):
"""Load device automation platform for integration.
Throws InvalidDeviceAutomationConfig if the integration is not found or does not support device automation.
"""
return await _async_get_device_automation_platform(
hass, config[CONF_DOMAIN], automation_type
)


async def _async_get_device_automations_from_domain(
hass, domain, automation_type, device_id
):
"""List device automations."""
try:
platform = await _async_get_device_automation_platform(
platform = await async_get_device_automation_platform(
hass, domain, automation_type
)
except InvalidDeviceAutomationConfig:
Expand Down Expand Up @@ -139,7 +130,7 @@ async def _async_get_device_automations(hass, automation_type, device_id):
async def _async_get_device_automation_capabilities(hass, automation_type, automation):
"""List device automations."""
try:
platform = await _async_get_device_automation_platform(
platform = await async_get_device_automation_platform(
hass, automation[CONF_DOMAIN], automation_type
)
except InvalidDeviceAutomationConfig:
Expand All @@ -152,9 +143,6 @@ async def _async_get_device_automation_capabilities(hass, automation_type, autom
return {}

capabilities = await getattr(platform, function_name)(hass, automation)

import voluptuous_serialize

capabilities = capabilities.copy()

extra_fields = capabilities.get("extra_fields")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ async def async_call_action_from_config(
domain: str,
):
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
action_type = config[CONF_TYPE]
if action_type == CONF_TURN_ON:
action = "turn_on"
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/light/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async def async_call_action_from_config(
context: Context,
) -> None:
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
await toggle_entity.async_call_action_from_config(
hass, config, variables, context, DOMAIN
)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/light/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def async_attach_trigger(
automation_info: dict,
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
return await toggle_entity.async_attach_trigger(
hass, config, action, automation_info
)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/switch/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async def async_call_action_from_config(
context: Context,
) -> None:
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
await toggle_entity.async_call_action_from_config(
hass, config, variables, context, DOMAIN
)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/switch/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def async_attach_trigger(
automation_info: dict,
) -> CALLBACK_TYPE:
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
return await toggle_entity.async_attach_trigger(
hass, config, action, automation_info
)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/zha/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async def async_call_action_from_config(
context: Context,
) -> None:
"""Perform an action based on configuration."""
config = ACTION_SCHEMA(config)
await ZHA_ACTION_TYPES[DEVICE_ACTION_TYPES[config[CONF_TYPE]]](
hass, config, variables, context
)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/zha/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
config = TRIGGER_SCHEMA(config)
trigger = (config[CONF_TYPE], config[CONF_SUBTYPE])
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])

Expand Down
11 changes: 4 additions & 7 deletions homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from urllib.parse import urlparse
from uuid import UUID

import voluptuous as vol
from pkg_resources import parse_version
import voluptuous as vol
import voluptuous_serialize

import homeassistant.util.dt as dt_util
from homeassistant.const import (
Expand Down Expand Up @@ -374,9 +375,7 @@ def positive_timedelta(value: timedelta) -> timedelta:
return value


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


def remove_falsy(value: List[T]) -> List[T]:
Expand Down Expand Up @@ -697,9 +696,7 @@ def validator(value):

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

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

return voluptuous_serialize.UNSUPPORTED
Expand Down

0 comments on commit 79d70b1

Please sign in to comment.