Broadlink fixup unintended breakage from service refactor (#23408)
* Allow host/ipv6 address for broadlink service This matches switch config and is a regression fix * Restore padding of packets for broadlink * Drop unused import * Fix comment on test
This commit is contained in:
parent
7a6acca6bb
commit
7d5c1ede72
2 changed files with 15 additions and 29 deletions
|
@ -2,7 +2,6 @@
|
|||
import asyncio
|
||||
from base64 import b64decode, b64encode
|
||||
import logging
|
||||
import re
|
||||
import socket
|
||||
|
||||
from datetime import timedelta
|
||||
|
@ -19,26 +18,22 @@ _LOGGER = logging.getLogger(__name__)
|
|||
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,
|
||||
})
|
||||
|
||||
|
||||
|
|
|
@ -4,10 +4,9 @@ from base64 import b64decode
|
|||
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)
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue