Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions homeassistant/components/tankerkoenig/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Ask tankerkoenig.de for petrol price information."""
from datetime import timedelta
import logging
from uuid import UUID

import pytankerkoenig
import voluptuous as vol
Expand All @@ -24,11 +25,26 @@
DEFAULT_RADIUS = 2
DEFAULT_SCAN_INTERVAL = timedelta(minutes=30)


def uuid4_string(value):
Comment thread
bdraco marked this conversation as resolved.
"""Validate a v4 UUID in string format."""
try:
result = UUID(value, version=4)
except (ValueError, AttributeError, TypeError) as error:
raise vol.Invalid("Invalid Version4 UUID", error_message=str(error))

if str(result) != value.lower():
# UUID() will create a uuid4 if input is invalid
raise vol.Invalid("Invalid Version4 UUID")

return str(result)


CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_API_KEY): uuid4_string,
vol.Optional(
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
): cv.time_period,
Expand All @@ -49,7 +65,7 @@
cv.positive_int, vol.Range(min=1)
),
vol.Optional(CONF_STATIONS, default=[]): vol.All(
cv.ensure_list, [cv.string]
cv.ensure_list, [uuid4_string]
),
}
)
Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ pysonos==0.0.24
# homeassistant.components.spc
pyspcwebgw==0.4.0

# homeassistant.components.tankerkoenig
pytankerkoenig==0.0.6

# homeassistant.components.ecobee
python-ecobee-api==0.2.2

Expand Down
1 change: 1 addition & 0 deletions tests/components/tankerkoenig/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the tankerkoenig integration."""
32 changes: 32 additions & 0 deletions tests/components/tankerkoenig/test_tankerkoenig_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""The tests for the custom tankerkoenig validators."""
import unittest
import uuid

import pytest
import voluptuous as vol

from homeassistant.components.tankerkoenig import uuid4_string


class TestUUID4StringValidator(unittest.TestCase):
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All new tests must be standalone pytest test functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #33115

"""Test the UUID4 string custom validator."""

def test_uuid4_string(caplog):
"""Test string uuid validation."""
schema = vol.Schema(uuid4_string)

for value in ["Not a hex string", "0", 0]:
with pytest.raises(vol.Invalid):
schema(value)

with pytest.raises(vol.Invalid):
# the third block should start with 4
schema("a03d31b2-2eee-2acc-bb90-eec40be6ed23")

with pytest.raises(vol.Invalid):
# the fourth block should start with 8-a
schema("a03d31b2-2eee-4acc-1b90-eec40be6ed23")

_str = str(uuid.uuid4())
assert schema(_str) == _str
assert schema(_str.upper()) == _str