mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Black
This commit is contained in:
@@ -9,31 +9,31 @@ import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components import litejet
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
from tests.common import (async_fire_time_changed, async_mock_service)
|
||||
from tests.common import async_fire_time_changed, async_mock_service
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ENTITY_SWITCH = 'switch.mock_switch_1'
|
||||
ENTITY_SWITCH = "switch.mock_switch_1"
|
||||
ENTITY_SWITCH_NUMBER = 1
|
||||
ENTITY_OTHER_SWITCH = 'switch.mock_switch_2'
|
||||
ENTITY_OTHER_SWITCH = "switch.mock_switch_2"
|
||||
ENTITY_OTHER_SWITCH_NUMBER = 2
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock serivce."""
|
||||
return async_mock_service(hass, 'test', 'automation')
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
def get_switch_name(number):
|
||||
"""Get a mock switch name."""
|
||||
return "Mock Switch #"+str(number)
|
||||
return "Mock Switch #" + str(number)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_lj(hass):
|
||||
"""Initialize components."""
|
||||
with mock.patch('pylitejet.LiteJet') as mock_pylitejet:
|
||||
with mock.patch("pylitejet.LiteJet") as mock_pylitejet:
|
||||
mock_lj = mock_pylitejet.return_value
|
||||
|
||||
mock_lj.switch_pressed_callbacks = {}
|
||||
@@ -53,13 +53,10 @@ def mock_lj(hass):
|
||||
mock_lj.on_switch_pressed.side_effect = on_switch_pressed
|
||||
mock_lj.on_switch_released.side_effect = on_switch_released
|
||||
|
||||
config = {
|
||||
'litejet': {
|
||||
'port': '/tmp/this_will_be_mocked'
|
||||
}
|
||||
}
|
||||
assert hass.loop.run_until_complete(setup.async_setup_component(
|
||||
hass, litejet.DOMAIN, config))
|
||||
config = {"litejet": {"port": "/tmp/this_will_be_mocked"}}
|
||||
assert hass.loop.run_until_complete(
|
||||
setup.async_setup_component(hass, litejet.DOMAIN, config)
|
||||
)
|
||||
|
||||
mock_lj.start_time = dt_util.utcnow()
|
||||
mock_lj.last_delta = timedelta(0)
|
||||
@@ -68,10 +65,12 @@ def mock_lj(hass):
|
||||
|
||||
async def simulate_press(hass, mock_lj, number):
|
||||
"""Test to simulate a press."""
|
||||
_LOGGER.info('*** simulate press of %d', number)
|
||||
_LOGGER.info("*** simulate press of %d", number)
|
||||
callback = mock_lj.switch_pressed_callbacks.get(number)
|
||||
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta):
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta,
|
||||
):
|
||||
if callback is not None:
|
||||
await hass.async_add_job(callback)
|
||||
await hass.async_block_till_done()
|
||||
@@ -79,10 +78,12 @@ async def simulate_press(hass, mock_lj, number):
|
||||
|
||||
async def simulate_release(hass, mock_lj, number):
|
||||
"""Test to simulate releasing."""
|
||||
_LOGGER.info('*** simulate release of %d', number)
|
||||
_LOGGER.info("*** simulate release of %d", number)
|
||||
callback = mock_lj.switch_released_callbacks.get(number)
|
||||
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta):
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta,
|
||||
):
|
||||
if callback is not None:
|
||||
await hass.async_add_job(callback)
|
||||
await hass.async_block_till_done()
|
||||
@@ -91,40 +92,42 @@ async def simulate_release(hass, mock_lj, number):
|
||||
async def simulate_time(hass, mock_lj, delta):
|
||||
"""Test to simulate time."""
|
||||
_LOGGER.info(
|
||||
'*** simulate time change by %s: %s',
|
||||
delta,
|
||||
mock_lj.start_time + delta)
|
||||
"*** simulate time change by %s: %s", delta, mock_lj.start_time + delta
|
||||
)
|
||||
mock_lj.last_delta = delta
|
||||
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
|
||||
return_value=mock_lj.start_time + delta):
|
||||
_LOGGER.info('now=%s', dt_util.utcnow())
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + delta,
|
||||
):
|
||||
_LOGGER.info("now=%s", dt_util.utcnow())
|
||||
async_fire_time_changed(hass, mock_lj.start_time + delta)
|
||||
await hass.async_block_till_done()
|
||||
_LOGGER.info('done with now=%s', dt_util.utcnow())
|
||||
_LOGGER.info("done with now=%s", dt_util.utcnow())
|
||||
|
||||
|
||||
async def setup_automation(hass, trigger):
|
||||
"""Test setting up the automation."""
|
||||
assert await setup.async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
'alias': 'My Test',
|
||||
'trigger': trigger,
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"alias": "My Test",
|
||||
"trigger": trigger,
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_simple(hass, calls, mock_lj):
|
||||
"""Test the simplest form of a LiteJet trigger."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER
|
||||
})
|
||||
await setup_automation(
|
||||
hass, {"platform": "litejet", "number": ENTITY_OTHER_SWITCH_NUMBER}
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
@@ -134,13 +137,14 @@ async def test_simple(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_more_than_short(hass, calls, mock_lj):
|
||||
"""Test a too short hold."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_more_than': {
|
||||
'milliseconds': '200'
|
||||
}
|
||||
})
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.1))
|
||||
@@ -150,13 +154,14 @@ async def test_held_more_than_short(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_more_than_long(hass, calls, mock_lj):
|
||||
"""Test a hold that is long enough."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_more_than': {
|
||||
'milliseconds': '200'
|
||||
}
|
||||
})
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
@@ -168,13 +173,14 @@ async def test_held_more_than_long(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_less_than_short(hass, calls, mock_lj):
|
||||
"""Test a hold that is short enough."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_less_than': {
|
||||
'milliseconds': '200'
|
||||
}
|
||||
})
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_less_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.1))
|
||||
@@ -185,13 +191,14 @@ async def test_held_less_than_short(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_less_than_long(hass, calls, mock_lj):
|
||||
"""Test a hold that is too long."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_less_than': {
|
||||
'milliseconds': '200'
|
||||
}
|
||||
})
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_less_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
@@ -203,16 +210,15 @@ async def test_held_less_than_long(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_in_range_short(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a too short hold."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_more_than': {
|
||||
'milliseconds': '100'
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
'held_less_than': {
|
||||
'milliseconds': '300'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.05))
|
||||
@@ -222,16 +228,15 @@ async def test_held_in_range_short(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_in_range_just_right(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a just right hold."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_more_than': {
|
||||
'milliseconds': '100'
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
'held_less_than': {
|
||||
'milliseconds': '300'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
@@ -243,16 +248,15 @@ async def test_held_in_range_just_right(hass, calls, mock_lj):
|
||||
|
||||
async def test_held_in_range_long(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a too long hold."""
|
||||
await setup_automation(hass, {
|
||||
'platform': 'litejet',
|
||||
'number': ENTITY_OTHER_SWITCH_NUMBER,
|
||||
'held_more_than': {
|
||||
'milliseconds': '100'
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
'held_less_than': {
|
||||
'milliseconds': '300'
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
|
||||
Reference in New Issue
Block a user