|
|
|
@ -24,19 +24,27 @@ from homeassistant.const import (
|
|
|
|
|
)
|
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
|
|
|
|
from tests.async_mock import patch
|
|
|
|
|
from tests.async_mock import MagicMock, patch
|
|
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
async def mock_light(hass):
|
|
|
|
|
async def mock_entry(hass):
|
|
|
|
|
"""Create a mock light entity."""
|
|
|
|
|
return MockConfigEntry(domain=DOMAIN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
async def mock_light(hass, mock_entry):
|
|
|
|
|
"""Create a mock light entity."""
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
|
|
mock_entry = MockConfigEntry(domain=DOMAIN)
|
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
|
|
light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF")
|
|
|
|
|
light = MagicMock(spec=pyzerproc.Light)
|
|
|
|
|
light.address = "AA:BB:CC:DD:EE:FF"
|
|
|
|
|
light.name = "LEDBlue-CCDDEEFF"
|
|
|
|
|
light.connected = False
|
|
|
|
|
|
|
|
|
|
mock_state = pyzerproc.LightState(False, (0, 0, 0))
|
|
|
|
|
|
|
|
|
@ -49,31 +57,36 @@ async def mock_light(hass):
|
|
|
|
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
|
|
light.connected = True
|
|
|
|
|
|
|
|
|
|
return light
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_init(hass):
|
|
|
|
|
async def test_init(hass, mock_entry):
|
|
|
|
|
"""Test platform setup."""
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
|
|
mock_entry = MockConfigEntry(domain=DOMAIN)
|
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
|
|
mock_light_1 = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF")
|
|
|
|
|
mock_light_2 = pyzerproc.Light("11:22:33:44:55:66", "LEDBlue-33445566")
|
|
|
|
|
mock_light_1 = MagicMock(spec=pyzerproc.Light)
|
|
|
|
|
mock_light_1.address = "AA:BB:CC:DD:EE:FF"
|
|
|
|
|
mock_light_1.name = "LEDBlue-CCDDEEFF"
|
|
|
|
|
mock_light_1.connected = True
|
|
|
|
|
|
|
|
|
|
mock_light_2 = MagicMock(spec=pyzerproc.Light)
|
|
|
|
|
mock_light_2.address = "11:22:33:44:55:66"
|
|
|
|
|
mock_light_2.name = "LEDBlue-33445566"
|
|
|
|
|
mock_light_2.connected = True
|
|
|
|
|
|
|
|
|
|
mock_state_1 = pyzerproc.LightState(False, (0, 0, 0))
|
|
|
|
|
mock_state_2 = pyzerproc.LightState(True, (0, 80, 255))
|
|
|
|
|
|
|
|
|
|
mock_light_1.get_state.return_value = mock_state_1
|
|
|
|
|
mock_light_2.get_state.return_value = mock_state_2
|
|
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
|
"homeassistant.components.zerproc.light.pyzerproc.discover",
|
|
|
|
|
return_value=[mock_light_1, mock_light_2],
|
|
|
|
|
), patch.object(mock_light_1, "connect"), patch.object(
|
|
|
|
|
mock_light_2, "connect"
|
|
|
|
|
), patch.object(
|
|
|
|
|
mock_light_1, "get_state", return_value=mock_state_1
|
|
|
|
|
), patch.object(
|
|
|
|
|
mock_light_2, "get_state", return_value=mock_state_2
|
|
|
|
|
):
|
|
|
|
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
@ -98,22 +111,17 @@ async def test_init(hass):
|
|
|
|
|
ATTR_XY_COLOR: (0.138, 0.08),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with patch.object(hass.loop, "stop"), patch.object(
|
|
|
|
|
mock_light_1, "disconnect"
|
|
|
|
|
) as mock_disconnect_1, patch.object(
|
|
|
|
|
mock_light_2, "disconnect"
|
|
|
|
|
) as mock_disconnect_2:
|
|
|
|
|
with patch.object(hass.loop, "stop"):
|
|
|
|
|
await hass.async_stop()
|
|
|
|
|
|
|
|
|
|
assert mock_disconnect_1.called
|
|
|
|
|
assert mock_disconnect_2.called
|
|
|
|
|
assert mock_light_1.disconnect.called
|
|
|
|
|
assert mock_light_2.disconnect.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_exception(hass):
|
|
|
|
|
async def test_discovery_exception(hass, mock_entry):
|
|
|
|
|
"""Test platform setup."""
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
|
|
mock_entry = MockConfigEntry(domain=DOMAIN)
|
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
@ -127,14 +135,16 @@ async def test_discovery_exception(hass):
|
|
|
|
|
assert len(hass.data[DOMAIN]["addresses"]) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_connect_exception(hass):
|
|
|
|
|
async def test_connect_exception(hass, mock_entry):
|
|
|
|
|
"""Test platform setup."""
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
|
|
mock_entry = MockConfigEntry(domain=DOMAIN)
|
|
|
|
|
mock_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
|
|
mock_light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF")
|
|
|
|
|
mock_light = MagicMock(spec=pyzerproc.Light)
|
|
|
|
|
mock_light.address = "AA:BB:CC:DD:EE:FF"
|
|
|
|
|
mock_light.name = "LEDBlue-CCDDEEFF"
|
|
|
|
|
mock_light.connected = False
|
|
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
|
"homeassistant.components.zerproc.light.pyzerproc.discover",
|
|
|
|
@ -149,6 +159,14 @@ async def test_connect_exception(hass):
|
|
|
|
|
assert len(hass.data[DOMAIN]["addresses"]) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_remove_entry(hass, mock_light, mock_entry):
|
|
|
|
|
"""Test platform setup."""
|
|
|
|
|
with patch.object(mock_light, "disconnect") as mock_disconnect:
|
|
|
|
|
await hass.config_entries.async_remove(mock_entry.entry_id)
|
|
|
|
|
|
|
|
|
|
assert mock_disconnect.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_light_turn_on(hass, mock_light):
|
|
|
|
|
"""Test ZerprocLight turn_on."""
|
|
|
|
|
utcnow = dt_util.utcnow()
|
|
|
|
|