Add timeout / debounce (for brightness and others) (#13534)
* Add async timeout feature * Decorator for setter methods to limit service calls to HA * Changed to async * Use async_call_later * Use lastargs, async_add_job * Use dict for lastargs * Updated tests to stop patch
This commit is contained in:
1 parent
c77d013f43
commit
262ea14e5a
8 files changed
+151
-16
No files matched your search
@@ -2,21 +2,67 @@
|
||||
|
||||
This includes tests for all mock object types.
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
import unittest
|
||||
from unittest.mock import call, patch, Mock
|
||||
|
||||
from homeassistant.components.homekit.accessories import (
|
||||
add_preload_service, set_accessory_info,
|
||||
HomeAccessory, HomeBridge, HomeDriver)
|
||||
debounce, HomeAccessory, HomeBridge, HomeDriver)
|
||||
from homeassistant.components.homekit.const import (
|
||||
ACCESSORY_MODEL, ACCESSORY_NAME, BRIDGE_MODEL, BRIDGE_NAME,
|
||||
SERV_ACCESSORY_INFO, CHAR_MANUFACTURER, CHAR_MODEL,
|
||||
CHAR_NAME, CHAR_SERIAL_NUMBER)
|
||||
from homeassistant.const import ATTR_NOW, EVENT_TIME_CHANGED
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
def patch_debounce():
|
||||
"""Return patch for debounce method."""
|
||||
return patch('homeassistant.components.homekit.accessories.debounce',
|
||||
lambda f: lambda *args, **kwargs: f(*args, **kwargs))
|
||||
|
||||
|
||||
class TestAccessories(unittest.TestCase):
|
||||
"""Test pyhap adapter methods."""
|
||||
|
||||
def test_debounce(self):
|
||||
"""Test add_timeout decorator function."""
|
||||
def demo_func(*args):
|
||||
nonlocal arguments, counter
|
||||
counter += 1
|
||||
arguments = args
|
||||
|
||||
arguments = None
|
||||
counter = 0
|
||||
hass = get_test_home_assistant()
|
||||
mock = Mock(hass=hass)
|
||||
|
||||
debounce_demo = debounce(demo_func)
|
||||
self.assertEqual(debounce_demo.__name__, 'demo_func')
|
||||
now = datetime(2018, 1, 1, 20, 0, 0, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=now):
|
||||
debounce_demo(mock, 'value')
|
||||
hass.bus.fire(
|
||||
EVENT_TIME_CHANGED, {ATTR_NOW: now + timedelta(seconds=3)})
|
||||
hass.block_till_done()
|
||||
assert counter == 1
|
||||
assert len(arguments) == 2
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=now):
|
||||
debounce_demo(mock, 'value')
|
||||
debounce_demo(mock, 'value')
|
||||
|
||||
hass.bus.fire(
|
||||
EVENT_TIME_CHANGED, {ATTR_NOW: now + timedelta(seconds=3)})
|
||||
hass.block_till_done()
|
||||
assert counter == 2
|
||||
|
||||
hass.stop()
|
||||
|
||||
def test_add_preload_service(self):
|
||||
"""Test add_preload_service without additional characteristics."""
|
||||
acc = Mock()
|
||||
|
||||
@@ -14,6 +14,7 @@ from homeassistant.const import (
|
||||
CONF_PORT, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.homekit.test_accessories import patch_debounce
|
||||
|
||||
IP_ADDRESS = '127.0.0.1'
|
||||
PATH_HOMEKIT = 'homeassistant.components.homekit'
|
||||
@@ -22,6 +23,17 @@ PATH_HOMEKIT = 'homeassistant.components.homekit'
|
||||
class TestHomeKit(unittest.TestCase):
|
||||
"""Test setup of HomeKit component and HomeKit class."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Setup debounce patcher."""
|
||||
cls.patcher = patch_debounce()
|
||||
cls.patcher.start()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""Stop debounce patcher."""
|
||||
cls.patcher.stop()
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.homekit.type_lights import Light
|
||||
from homeassistant.components.light import (
|
||||
DOMAIN, ATTR_BRIGHTNESS, ATTR_BRIGHTNESS_PCT, ATTR_COLOR_TEMP,
|
||||
ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_COLOR)
|
||||
@@ -12,11 +11,26 @@ from homeassistant.const import (
|
||||
SERVICE_TURN_OFF, STATE_ON, STATE_OFF, STATE_UNKNOWN)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.homekit.test_accessories import patch_debounce
|
||||
|
||||
|
||||
class TestHomekitLights(unittest.TestCase):
|
||||
"""Test class for all accessory types regarding lights."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Setup Light class import and debounce patcher."""
|
||||
cls.patcher = patch_debounce()
|
||||
cls.patcher.start()
|
||||
_import = __import__('homeassistant.components.homekit.type_lights',
|
||||
fromlist=['Light'])
|
||||
cls.light_cls = _import.Light
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""Stop debounce patcher."""
|
||||
cls.patcher.stop()
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -38,7 +52,7 @@ class TestHomekitLights(unittest.TestCase):
|
||||
entity_id = 'light.demo'
|
||||
self.hass.states.set(entity_id, STATE_ON,
|
||||
{ATTR_SUPPORTED_FEATURES: 0})
|
||||
acc = Light(self.hass, entity_id, 'Light', aid=2)
|
||||
acc = self.light_cls(self.hass, entity_id, 'Light', aid=2)
|
||||
self.assertEqual(acc.aid, 2)
|
||||
self.assertEqual(acc.category, 5) # Lightbulb
|
||||
self.assertEqual(acc.char_on.value, 0)
|
||||
@@ -82,7 +96,7 @@ class TestHomekitLights(unittest.TestCase):
|
||||
entity_id = 'light.demo'
|
||||
self.hass.states.set(entity_id, STATE_ON, {
|
||||
ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS: 255})
|
||||
acc = Light(self.hass, entity_id, 'Light', aid=2)
|
||||
acc = self.light_cls(self.hass, entity_id, 'Light', aid=2)
|
||||
self.assertEqual(acc.char_brightness.value, 0)
|
||||
|
||||
acc.run()
|
||||
@@ -124,7 +138,7 @@ class TestHomekitLights(unittest.TestCase):
|
||||
self.hass.states.set(entity_id, STATE_ON, {
|
||||
ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR_TEMP,
|
||||
ATTR_COLOR_TEMP: 190})
|
||||
acc = Light(self.hass, entity_id, 'Light', aid=2)
|
||||
acc = self.light_cls(self.hass, entity_id, 'Light', aid=2)
|
||||
self.assertEqual(acc.char_color_temperature.value, 153)
|
||||
|
||||
acc.run()
|
||||
@@ -146,7 +160,7 @@ class TestHomekitLights(unittest.TestCase):
|
||||
self.hass.states.set(entity_id, STATE_ON, {
|
||||
ATTR_SUPPORTED_FEATURES: SUPPORT_COLOR,
|
||||
ATTR_HS_COLOR: (260, 90)})
|
||||
acc = Light(self.hass, entity_id, 'Light', aid=2)
|
||||
acc = self.light_cls(self.hass, entity_id, 'Light', aid=2)
|
||||
self.assertEqual(acc.char_hue.value, 0)
|
||||
self.assertEqual(acc.char_saturation.value, 75)
|
||||
|
||||
|
||||
@@ -6,17 +6,32 @@ from homeassistant.components.climate import (
|
||||
ATTR_CURRENT_TEMPERATURE, ATTR_TEMPERATURE,
|
||||
ATTR_TARGET_TEMP_LOW, ATTR_TARGET_TEMP_HIGH, ATTR_OPERATION_MODE,
|
||||
ATTR_OPERATION_LIST, STATE_COOL, STATE_HEAT, STATE_AUTO)
|
||||
from homeassistant.components.homekit.type_thermostats import Thermostat
|
||||
from homeassistant.const import (
|
||||
ATTR_SERVICE, EVENT_CALL_SERVICE, ATTR_SERVICE_DATA,
|
||||
ATTR_UNIT_OF_MEASUREMENT, STATE_OFF, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.homekit.test_accessories import patch_debounce
|
||||
|
||||
|
||||
class TestHomekitThermostats(unittest.TestCase):
|
||||
"""Test class for all accessory types regarding thermostats."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Setup Thermostat class import and debounce patcher."""
|
||||
cls.patcher = patch_debounce()
|
||||
cls.patcher.start()
|
||||
_import = __import__(
|
||||
'homeassistant.components.homekit.type_thermostats',
|
||||
fromlist=['Thermostat'])
|
||||
cls.thermostat_cls = _import.Thermostat
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""Stop debounce patcher."""
|
||||
cls.patcher.stop()
|
||||
|
||||
def setUp(self):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -37,7 +52,7 @@ class TestHomekitThermostats(unittest.TestCase):
|
||||
"""Test if accessory and HA are updated accordingly."""
|
||||
climate = 'climate.test'
|
||||
|
||||
acc = Thermostat(self.hass, climate, 'Climate', False, aid=2)
|
||||
acc = self.thermostat_cls(self.hass, climate, 'Climate', False, aid=2)
|
||||
acc.run()
|
||||
|
||||
self.assertEqual(acc.aid, 2)
|
||||
@@ -172,7 +187,7 @@ class TestHomekitThermostats(unittest.TestCase):
|
||||
"""Test if accessory and HA are updated accordingly."""
|
||||
climate = 'climate.test'
|
||||
|
||||
acc = Thermostat(self.hass, climate, 'Climate', True)
|
||||
acc = self.thermostat_cls(self.hass, climate, 'Climate', True)
|
||||
acc.run()
|
||||
|
||||
self.assertEqual(acc.char_cooling_thresh_temp.value, 23.0)
|
||||
@@ -242,7 +257,7 @@ class TestHomekitThermostats(unittest.TestCase):
|
||||
"""Test if accessory and HA are updated accordingly."""
|
||||
climate = 'climate.test'
|
||||
|
||||
acc = Thermostat(self.hass, climate, 'Climate', True)
|
||||
acc = self.thermostat_cls(self.hass, climate, 'Climate', True)
|
||||
acc.run()
|
||||
|
||||
self.hass.states.set(climate, STATE_AUTO,
|
||||
|
||||
Reference in new issue
Block a user