mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Improve ecobee service schemas (#26955)
* Validate date and time in create vaction Improve validation with utility functions. * Improve validate ATTR_VACATION_NAME * Add tests for ecobee.util functions * Revise tests as standalone functions
This commit is contained in:
parent
1c72a246a0
commit
2dfdc5f6f8
@ -37,6 +37,7 @@ from homeassistant.util.temperature import convert
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import DOMAIN, _LOGGER
|
||||
from .util import ecobee_date, ecobee_time
|
||||
|
||||
ATTR_COOL_TEMP = "cool_temp"
|
||||
ATTR_END_DATE = "end_date"
|
||||
@ -106,13 +107,17 @@ DTGROUP_INCLUSIVE_MSG = (
|
||||
CREATE_VACATION_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(ATTR_VACATION_NAME): cv.string,
|
||||
vol.Required(ATTR_VACATION_NAME): vol.All(cv.string, vol.Length(max=12)),
|
||||
vol.Required(ATTR_COOL_TEMP): vol.Coerce(float),
|
||||
vol.Required(ATTR_HEAT_TEMP): vol.Coerce(float),
|
||||
vol.Inclusive(ATTR_START_DATE, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): cv.string,
|
||||
vol.Inclusive(ATTR_START_TIME, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): cv.string,
|
||||
vol.Inclusive(ATTR_END_DATE, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): cv.string,
|
||||
vol.Inclusive(ATTR_END_TIME, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): cv.string,
|
||||
vol.Inclusive(
|
||||
ATTR_START_DATE, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG
|
||||
): ecobee_date,
|
||||
vol.Inclusive(
|
||||
ATTR_START_TIME, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG
|
||||
): ecobee_time,
|
||||
vol.Inclusive(ATTR_END_DATE, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): ecobee_date,
|
||||
vol.Inclusive(ATTR_END_TIME, "dtgroup", msg=DTGROUP_INCLUSIVE_MSG): ecobee_time,
|
||||
vol.Optional(ATTR_FAN_MODE, default="auto"): vol.Any("auto", "on"),
|
||||
vol.Optional(ATTR_FAN_MIN_ON_TIME, default=0): vol.All(
|
||||
int, vol.Range(min=0, max=60)
|
||||
@ -123,7 +128,7 @@ CREATE_VACATION_SCHEMA = vol.Schema(
|
||||
DELETE_VACATION_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(ATTR_VACATION_NAME): cv.string,
|
||||
vol.Required(ATTR_VACATION_NAME): vol.All(cv.string, vol.Length(max=12)),
|
||||
}
|
||||
)
|
||||
|
||||
|
21
homeassistant/components/ecobee/util.py
Normal file
21
homeassistant/components/ecobee/util.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""Validation utility functions for ecobee services."""
|
||||
from datetime import datetime
|
||||
import voluptuous as vol
|
||||
|
||||
|
||||
def ecobee_date(date_string):
|
||||
"""Validate a date_string as valid for the ecobee API."""
|
||||
try:
|
||||
datetime.strptime(date_string, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
raise vol.Invalid("Date does not match ecobee date format YYYY-MM-DD")
|
||||
return date_string
|
||||
|
||||
|
||||
def ecobee_time(time_string):
|
||||
"""Validate a time_string as valid for the ecobee API."""
|
||||
try:
|
||||
datetime.strptime(time_string, "%H:%M:%S")
|
||||
except ValueError:
|
||||
raise vol.Invalid("Time does not match ecobee 24-hour time format HH:MM:SS")
|
||||
return time_string
|
35
tests/components/ecobee/test_util.py
Normal file
35
tests/components/ecobee/test_util.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""Tests for the ecobee.util module."""
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.ecobee.util import ecobee_date, ecobee_time
|
||||
|
||||
|
||||
def test_ecobee_date_with_valid_input():
|
||||
"""Test that the date function returns the expected result."""
|
||||
test_input = "2019-09-27"
|
||||
|
||||
assert ecobee_date(test_input) == test_input
|
||||
|
||||
|
||||
def test_ecobee_date_with_invalid_input():
|
||||
"""Test that the date function raises the expected exception."""
|
||||
test_input = "20190927"
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
ecobee_date(test_input)
|
||||
|
||||
|
||||
def test_ecobee_time_with_valid_input():
|
||||
"""Test that the time function returns the expected result."""
|
||||
test_input = "20:55:15"
|
||||
|
||||
assert ecobee_time(test_input) == test_input
|
||||
|
||||
|
||||
def test_ecobee_time_with_invalid_input():
|
||||
"""Test that the time function raises the expected exception."""
|
||||
test_input = "20:55"
|
||||
|
||||
with pytest.raises(vol.Invalid):
|
||||
ecobee_time(test_input)
|
Loading…
x
Reference in New Issue
Block a user