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
19 changes: 7 additions & 12 deletions homeassistant/components/broadlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import asyncio
from base64 import b64decode, b64encode
import logging
import re
import socket

from datetime import timedelta
Expand All @@ -19,26 +18,22 @@
DEFAULT_RETRY = 3


def ipv4_address(value):
"""Validate an ipv4 address."""
regex = re.compile(r'^\d+\.\d+\.\d+\.\d+$')
if not regex.match(value):
raise vol.Invalid('Invalid Ipv4 address, expected a.b.c.d')
return value


def data_packet(value):
"""Decode a data packet given for broadlink."""
return b64decode(cv.string(value))
value = cv.string(value)
extra = len(value) % 4
if extra > 0:
value = value + ('=' * (4 - extra))
return b64decode(value)


SERVICE_SEND_SCHEMA = vol.Schema({
vol.Required(CONF_HOST): ipv4_address,
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PACKET): vol.All(cv.ensure_list, [data_packet])
})

SERVICE_LEARN_SCHEMA = vol.Schema({
vol.Required(CONF_HOST): ipv4_address,
vol.Required(CONF_HOST): cv.string,
})


Expand Down
25 changes: 8 additions & 17 deletions tests/components/broadlink/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from unittest.mock import MagicMock, patch, call

import pytest
import voluptuous as vol

from homeassistant.util.dt import utcnow
from homeassistant.components.broadlink import async_setup_service
from homeassistant.components.broadlink import async_setup_service, data_packet
from homeassistant.components.broadlink.const import (
DOMAIN, SERVICE_LEARN, SERVICE_SEND)

Expand All @@ -26,6 +25,13 @@ def dummy_broadlink():
yield broadlink


async def test_padding(hass):
"""Verify that non padding strings are allowed."""
assert data_packet('Jg') == b'&'
assert data_packet('Jg=') == b'&'
assert data_packet('Jg==') == b'&'


async def test_send(hass):
"""Test send service."""
mock_device = MagicMock()
Expand Down Expand Up @@ -100,18 +106,3 @@ async def test_learn_timeout(hass):
assert mock_create.call_args == call(
"No signal was received",
title='Broadlink switch')


async def test_ipv4():
"""Test ipv4 parsing."""
from homeassistant.components.broadlink import ipv4_address

schema = vol.Schema(ipv4_address)

for value in ('invalid', '1', '192', '192.168',
'192.168.0', '192.168.0.A'):
with pytest.raises(vol.MultipleInvalid):
schema(value)

for value in ('192.168.0.1', '10.0.0.1'):
schema(value)