UniFi integration move to push messaging (#31086)

* Rewrite UniFi integration to use push messaging

* Add signalling for new clients/devices

* Update list of known wireless clients when we get events of them connecting

* Reconnection logic for websocket

* Fix failing tests

* Bump requirement to v12

* Add new tests

* Update homeassistant/components/unifi/controller.py

Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Robert SvenssonandMartin Hjelmare authored and GitHub committed 2020-01-31 20:23:25 +01:00
1 parent 06c8e53323
commit 958a867c11
14 files changed
+296 -377

No files matched your search

+9 -84
View File
@@ -4,6 +4,8 @@ from unittest.mock import Mock, patch
from homeassistant.components import unifi
from homeassistant.setup import async_setup_component
from .test_controller import setup_unifi_integration
from tests.common import MockConfigEntry, mock_coro
@@ -42,67 +44,15 @@ async def test_setup_with_config(hass):
async def test_successful_config_entry(hass):
"""Test that configured options for a host are loaded via config entry."""
entry = MockConfigEntry(
domain=unifi.DOMAIN,
data={
"controller": {
"host": "0.0.0.0",
"username": "user",
"password": "pass",
"port": 80,
"site": "default",
"verify_ssl": True,
},
"poe_control": True,
},
)
entry.add_to_hass(hass)
mock_registry = Mock()
with patch.object(unifi, "UniFiController") as mock_controller, patch(
"homeassistant.helpers.device_registry.async_get_registry",
return_value=mock_coro(mock_registry),
):
mock_controller.return_value.async_setup.return_value = mock_coro(True)
mock_controller.return_value.mac = "00:11:22:33:44:55"
assert await unifi.async_setup_entry(hass, entry) is True
assert len(mock_controller.mock_calls) == 2
p_hass, p_entry = mock_controller.mock_calls[0][1]
assert p_hass is hass
assert p_entry is entry
assert len(mock_registry.mock_calls) == 1
assert mock_registry.mock_calls[0][2] == {
"config_entry_id": entry.entry_id,
"connections": {("mac", "00:11:22:33:44:55")},
"manufacturer": unifi.ATTR_MANUFACTURER,
"model": "UniFi Controller",
"name": "UniFi Controller",
}
await setup_unifi_integration(hass)
assert hass.data[unifi.DOMAIN]
async def test_controller_fail_setup(hass):
"""Test that a failed setup still stores controller."""
entry = MockConfigEntry(
domain=unifi.DOMAIN,
data={
"controller": {
"host": "0.0.0.0",
"username": "user",
"password": "pass",
"port": 80,
"site": "default",
"verify_ssl": True,
},
"poe_control": True,
},
)
entry.add_to_hass(hass)
with patch.object(unifi, "UniFiController") as mock_cntrlr:
mock_cntrlr.return_value.async_setup.return_value = mock_coro(False)
assert await unifi.async_setup_entry(hass, entry) is False
await setup_unifi_integration(hass)
assert hass.data[unifi.DOMAIN] == {}
@@ -140,33 +90,8 @@ async def test_controller_no_mac(hass):
async def test_unload_entry(hass):
"""Test being able to unload an entry."""
entry = MockConfigEntry(
domain=unifi.DOMAIN,
data={
"controller": {
"host": "0.0.0.0",
"username": "user",
"password": "pass",
"port": 80,
"site": "default",
"verify_ssl": True,
},
"poe_control": True,
},
)
entry.add_to_hass(hass)
controller = await setup_unifi_integration(hass)
assert hass.data[unifi.DOMAIN]
with patch.object(unifi, "UniFiController") as mock_controller, patch(
"homeassistant.helpers.device_registry.async_get_registry",
return_value=mock_coro(Mock()),
):
mock_controller.return_value.async_setup.return_value = mock_coro(True)
mock_controller.return_value.mac = "00:11:22:33:44:55"
assert await unifi.async_setup_entry(hass, entry) is True
assert len(mock_controller.return_value.mock_calls) == 1
mock_controller.return_value.async_reset.return_value = mock_coro(True)
assert await unifi.async_unload_entry(hass, entry)
assert len(mock_controller.return_value.async_reset.mock_calls) == 1
assert hass.data[unifi.DOMAIN] == {}
assert await unifi.async_unload_entry(hass, controller.config_entry)
assert not hass.data[unifi.DOMAIN]