Add tests of homeworks __init__ (#113581)
This commit is contained in:
parent
398c38b50d
commit
7b20641651
2 changed files with 115 additions and 1 deletions
115
tests/components/homeworks/test_init.py
Normal file
115
tests/components/homeworks/test_init.py
Normal file
|
@ -0,0 +1,115 @@
|
|||
"""Tests for the Lutron Homeworks Series 4 and 8 integration."""
|
||||
from unittest.mock import ANY, MagicMock
|
||||
|
||||
from pyhomeworks.pyhomeworks import HW_BUTTON_PRESSED, HW_BUTTON_RELEASED
|
||||
|
||||
from homeassistant.components.homeworks import EVENT_BUTTON_PRESS, EVENT_BUTTON_RELEASE
|
||||
from homeassistant.components.homeworks.const import CONF_DIMMERS, CONF_KEYPADS, DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, async_capture_events
|
||||
|
||||
|
||||
async def test_import(
|
||||
hass: HomeAssistant,
|
||||
mock_homeworks: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Homeworks YAML import."""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
CONF_HOST: "192.168.0.1",
|
||||
CONF_PORT: 1234,
|
||||
CONF_DIMMERS: [],
|
||||
CONF_KEYPADS: [],
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.flow.async_progress()) == 1
|
||||
assert hass.config_entries.flow.async_progress()[0]["context"]["source"] == "import"
|
||||
|
||||
|
||||
async def test_load_unload_config_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_homeworks: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Homeworks configuration entry loading/unloading."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
mock_homeworks.assert_called_once_with("192.168.0.1", 1234, ANY)
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not hass.data.get(DOMAIN)
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
async def test_config_entry_not_ready(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_homeworks: MagicMock,
|
||||
) -> None:
|
||||
"""Test the Homeworks configuration entry not ready."""
|
||||
mock_homeworks.side_effect = ConnectionError
|
||||
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_homeworks.assert_called_once()
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_keypad_events(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_homeworks: MagicMock,
|
||||
) -> None:
|
||||
"""Test Homeworks keypad events."""
|
||||
release_events = async_capture_events(hass, EVENT_BUTTON_RELEASE)
|
||||
press_events = async_capture_events(hass, EVENT_BUTTON_PRESS)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_homeworks.assert_called_once_with("192.168.0.1", 1234, ANY)
|
||||
hw_callback = mock_homeworks.mock_calls[0][1][2]
|
||||
|
||||
hw_callback(HW_BUTTON_PRESSED, ["[02:08:02:01]", 1])
|
||||
await hass.async_block_till_done()
|
||||
assert len(press_events) == 1
|
||||
assert len(release_events) == 0
|
||||
assert press_events[0].data == {
|
||||
"id": "foyer_keypad",
|
||||
"name": "Foyer Keypad",
|
||||
"button": 1,
|
||||
}
|
||||
assert press_events[0].event_type == "homeworks_button_press"
|
||||
|
||||
hw_callback(HW_BUTTON_RELEASED, ["[02:08:02:01]", 1])
|
||||
await hass.async_block_till_done()
|
||||
assert len(press_events) == 1
|
||||
assert len(release_events) == 1
|
||||
assert release_events[0].data == {
|
||||
"id": "foyer_keypad",
|
||||
"name": "Foyer Keypad",
|
||||
"button": 1,
|
||||
}
|
||||
assert release_events[0].event_type == "homeworks_button_release"
|
||||
|
||||
hw_callback("unsupported", ["[02:08:02:01]", 1])
|
||||
await hass.async_block_till_done()
|
||||
assert len(press_events) == 1
|
||||
assert len(release_events) == 1
|
Loading…
Add table
Add a link
Reference in a new issue