mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add type hints and code cleanup for mikrotik (#74296)
* Add type hints and code cleanup for mikrotik * update test and increase coverage * move setup_mikrotik_entry to __init__.py
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
"""The tests for the Mikrotik device tracker platform."""
|
||||
from datetime import timedelta
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import mikrotik
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import (
|
||||
DEVICE_2_WIRELESS,
|
||||
@@ -17,12 +18,10 @@ from . import (
|
||||
MOCK_DATA,
|
||||
MOCK_OPTIONS,
|
||||
WIRELESS_DATA,
|
||||
setup_mikrotik_entry,
|
||||
)
|
||||
from .test_hub import setup_mikrotik_entry
|
||||
|
||||
from tests.common import MockConfigEntry, patch
|
||||
|
||||
DEFAULT_DETECTION_TIME = timedelta(seconds=300)
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -56,24 +55,11 @@ def mock_command(self, cmd, params=None):
|
||||
return {}
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that nothing happens when configuring mikrotik through device tracker platform."""
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass,
|
||||
device_tracker.DOMAIN,
|
||||
{device_tracker.DOMAIN: {"platform": "mikrotik"}},
|
||||
)
|
||||
is False
|
||||
)
|
||||
assert mikrotik.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_device_trackers(hass, mock_device_registry_devices):
|
||||
"""Test device_trackers created by mikrotik."""
|
||||
|
||||
# test devices are added from wireless list only
|
||||
hub = await setup_mikrotik_entry(hass)
|
||||
await setup_mikrotik_entry(hass)
|
||||
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1 is not None
|
||||
@@ -90,7 +76,7 @@ async def test_device_trackers(hass, mock_device_registry_devices):
|
||||
# test device_2 is added after connecting to wireless network
|
||||
WIRELESS_DATA.append(DEVICE_2_WIRELESS)
|
||||
|
||||
await hub.async_refresh()
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
@@ -104,26 +90,72 @@ async def test_device_trackers(hass, mock_device_registry_devices):
|
||||
|
||||
# test state remains home if last_seen consider_home_interval
|
||||
del WIRELESS_DATA[1] # device 2 is removed from wireless list
|
||||
hub.api.devices["00:00:00:00:00:02"]._last_seen = dt_util.utcnow() - timedelta(
|
||||
minutes=4
|
||||
)
|
||||
await hub.async_update()
|
||||
await hass.async_block_till_done()
|
||||
with freeze_time(utcnow() + timedelta(minutes=4)):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(minutes=4))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2.state != "not_home"
|
||||
assert device_2.state == "home"
|
||||
|
||||
# test state changes to away if last_seen > consider_home_interval
|
||||
hub.api.devices["00:00:00:00:00:02"]._last_seen = dt_util.utcnow() - timedelta(
|
||||
minutes=5
|
||||
)
|
||||
await hub.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
with freeze_time(utcnow() + timedelta(minutes=6)):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(minutes=6))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2.state == "not_home"
|
||||
|
||||
|
||||
async def test_force_dhcp(hass, mock_device_registry_devices):
|
||||
"""Test updating hub that supports wireless with forced dhcp method."""
|
||||
|
||||
# hub supports wireless by default, force_dhcp is enabled to override
|
||||
await setup_mikrotik_entry(hass, force_dhcp=False)
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1
|
||||
assert device_1.state == "home"
|
||||
# device_2 is not on the wireless list but it is still added from DHCP
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2
|
||||
assert device_2.state == "home"
|
||||
|
||||
|
||||
async def test_hub_not_support_wireless(hass, mock_device_registry_devices):
|
||||
"""Test device_trackers created when hub doesn't support wireless."""
|
||||
|
||||
await setup_mikrotik_entry(hass, support_wireless=False)
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1
|
||||
assert device_1.state == "home"
|
||||
# device_2 is added from DHCP
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2
|
||||
assert device_2.state == "home"
|
||||
|
||||
|
||||
async def test_arp_ping_success(hass, mock_device_registry_devices):
|
||||
"""Test arp ping devices to confirm they are connected."""
|
||||
|
||||
with patch.object(mikrotik.hub.MikrotikData, "do_arp_ping", return_value=True):
|
||||
await setup_mikrotik_entry(hass, arp_ping=True, force_dhcp=True)
|
||||
|
||||
# test wired device_2 show as home if arp ping returns True
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2
|
||||
assert device_2.state == "home"
|
||||
|
||||
|
||||
async def test_arp_ping_timeout(hass, mock_device_registry_devices):
|
||||
"""Test arp ping timeout so devices are shown away."""
|
||||
with patch.object(mikrotik.hub.MikrotikData, "do_arp_ping", return_value=False):
|
||||
await setup_mikrotik_entry(hass, arp_ping=True, force_dhcp=True)
|
||||
|
||||
# test wired device_2 show as not_home if arp ping times out
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2
|
||||
assert device_2.state == "not_home"
|
||||
|
||||
|
||||
async def test_device_trackers_numerical_name(hass, mock_device_registry_devices):
|
||||
"""Test device_trackers created by mikrotik with numerical device name."""
|
||||
|
||||
@@ -164,6 +196,13 @@ async def test_restoring_devices(hass):
|
||||
suggested_object_id="device_2",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
registry.async_get_or_create(
|
||||
device_tracker.DOMAIN,
|
||||
mikrotik.DOMAIN,
|
||||
"00:00:00:00:00:03",
|
||||
suggested_object_id="device_3",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
await setup_mikrotik_entry(hass)
|
||||
|
||||
@@ -174,3 +213,22 @@ async def test_restoring_devices(hass):
|
||||
device_2 = hass.states.get("device_tracker.device_2")
|
||||
assert device_2 is not None
|
||||
assert device_2.state == "not_home"
|
||||
# device_3 is not on the DHCP list or wireless list
|
||||
# so it won't be restored.
|
||||
device_3 = hass.states.get("device_tracker.device_3")
|
||||
assert device_3 is None
|
||||
|
||||
|
||||
async def test_update_failed(hass, mock_device_registry_devices):
|
||||
"""Test failing to connect during update."""
|
||||
|
||||
await setup_mikrotik_entry(hass)
|
||||
|
||||
with patch.object(
|
||||
mikrotik.hub.MikrotikData, "command", side_effect=mikrotik.errors.CannotConnect
|
||||
):
|
||||
async_fire_time_changed(hass, utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device_1 = hass.states.get("device_tracker.device_1")
|
||||
assert device_1.state == STATE_UNAVAILABLE
|
||||
|
||||
Reference in New Issue
Block a user