From 2dfdc5f6f884d3bbb5729bcdaf811a30ae46b8f9 Mon Sep 17 00:00:00 2001 From: Mark Coombes Date: Sat, 28 Sep 2019 05:32:22 -0400 Subject: [PATCH] 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 --- homeassistant/components/ecobee/climate.py | 17 +++++++---- homeassistant/components/ecobee/util.py | 21 +++++++++++++ tests/components/ecobee/test_util.py | 35 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 homeassistant/components/ecobee/util.py create mode 100644 tests/components/ecobee/test_util.py diff --git a/homeassistant/components/ecobee/climate.py b/homeassistant/components/ecobee/climate.py index 460bd2bb4a4..6eccdccf8c6 100644 --- a/homeassistant/components/ecobee/climate.py +++ b/homeassistant/components/ecobee/climate.py @@ -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)), } ) diff --git a/homeassistant/components/ecobee/util.py b/homeassistant/components/ecobee/util.py new file mode 100644 index 00000000000..3acc3e5676d --- /dev/null +++ b/homeassistant/components/ecobee/util.py @@ -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 diff --git a/tests/components/ecobee/test_util.py b/tests/components/ecobee/test_util.py new file mode 100644 index 00000000000..ee02f2a33aa --- /dev/null +++ b/tests/components/ecobee/test_util.py @@ -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)