mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Full test coverage for Vodafone Station sensor platform (#133285)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
parent
6c9c17f129
commit
277ee03145
@ -5,6 +5,7 @@ import logging
|
|||||||
_LOGGER = logging.getLogger(__package__)
|
_LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
DOMAIN = "vodafone_station"
|
DOMAIN = "vodafone_station"
|
||||||
|
SCAN_INTERVAL = 30
|
||||||
|
|
||||||
DEFAULT_DEVICE_NAME = "Unknown device"
|
DEFAULT_DEVICE_NAME = "Unknown device"
|
||||||
DEFAULT_HOST = "192.168.1.1"
|
DEFAULT_HOST = "192.168.1.1"
|
||||||
|
@ -14,7 +14,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
|||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import _LOGGER, DOMAIN
|
from .const import _LOGGER, DOMAIN, SCAN_INTERVAL
|
||||||
|
|
||||||
CONSIDER_HOME_SECONDS = DEFAULT_CONSIDER_HOME.total_seconds()
|
CONSIDER_HOME_SECONDS = DEFAULT_CONSIDER_HOME.total_seconds()
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ class VodafoneStationRouter(DataUpdateCoordinator[UpdateCoordinatorDataType]):
|
|||||||
hass=hass,
|
hass=hass,
|
||||||
logger=_LOGGER,
|
logger=_LOGGER,
|
||||||
name=f"{DOMAIN}-{host}-coordinator",
|
name=f"{DOMAIN}-{host}-coordinator",
|
||||||
update_interval=timedelta(seconds=30),
|
update_interval=timedelta(seconds=SCAN_INTERVAL),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _calculate_update_time_and_consider_home(
|
def _calculate_update_time_and_consider_home(
|
||||||
|
28
tests/components/vodafone_station/conftest.py
Normal file
28
tests/components/vodafone_station/conftest.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Configure tests for Vodafone Station."""
|
||||||
|
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from .const import DEVICE_DATA_QUERY, SENSOR_DATA_QUERY
|
||||||
|
|
||||||
|
from tests.common import AsyncMock, Generator, patch
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_vodafone_station_router() -> Generator[AsyncMock]:
|
||||||
|
"""Mock a Vodafone Station router."""
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"homeassistant.components.vodafone_station.coordinator.VodafoneStationSercommApi",
|
||||||
|
autospec=True,
|
||||||
|
) as mock_router,
|
||||||
|
):
|
||||||
|
router = mock_router.return_value
|
||||||
|
router.get_devices_data.return_value = DEVICE_DATA_QUERY
|
||||||
|
router.get_sensor_data.return_value = SENSOR_DATA_QUERY
|
||||||
|
router.convert_uptime.return_value = datetime(
|
||||||
|
2024, 11, 19, 20, 19, 0, tzinfo=UTC
|
||||||
|
)
|
||||||
|
router.base_url = "https://fake_host"
|
||||||
|
yield router
|
94
tests/components/vodafone_station/test_sensor.py
Normal file
94
tests/components/vodafone_station/test_sensor.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
"""Tests for Vodafone Station sensor platform."""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.vodafone_station.const import (
|
||||||
|
DOMAIN,
|
||||||
|
LINE_TYPES,
|
||||||
|
SCAN_INTERVAL,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import MOCK_USER_DATA, SENSOR_DATA_QUERY
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("connection_type", "index"),
|
||||||
|
[
|
||||||
|
("dsl_ipaddr", 0),
|
||||||
|
("fiber_ipaddr", 1),
|
||||||
|
("vf_internet_key_ip_addr", 2),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_active_connection_type(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_vodafone_station_router: AsyncMock,
|
||||||
|
connection_type,
|
||||||
|
index,
|
||||||
|
) -> None:
|
||||||
|
"""Test device connection type."""
|
||||||
|
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
active_connection_entity = f"sensor.vodafone_station_{SENSOR_DATA_QUERY['sys_serial_number']}_active_connection"
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(active_connection_entity)
|
||||||
|
assert state
|
||||||
|
assert state.state == "unknown"
|
||||||
|
|
||||||
|
sensor_data = deepcopy(SENSOR_DATA_QUERY)
|
||||||
|
sensor_data[connection_type] = "1.1.1.1"
|
||||||
|
mock_vodafone_station_router.get_sensor_data.return_value = sensor_data
|
||||||
|
|
||||||
|
freezer.tick(SCAN_INTERVAL)
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
state = hass.states.get(active_connection_entity)
|
||||||
|
assert state
|
||||||
|
assert state.state == LINE_TYPES[index]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.freeze_time("2023-12-02T13:00:00+00:00")
|
||||||
|
async def test_uptime(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_vodafone_station_router: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test device uptime shift."""
|
||||||
|
|
||||||
|
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA)
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
uptime = "2024-11-19T20:19:00+00:00"
|
||||||
|
uptime_entity = (
|
||||||
|
f"sensor.vodafone_station_{SENSOR_DATA_QUERY['sys_serial_number']}_uptime"
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get(uptime_entity)
|
||||||
|
assert state
|
||||||
|
assert state.state == uptime
|
||||||
|
|
||||||
|
mock_vodafone_station_router.get_sensor_data.return_value["sys_uptime"] = "12:17:23"
|
||||||
|
|
||||||
|
freezer.tick(SCAN_INTERVAL)
|
||||||
|
async_fire_time_changed(hass)
|
||||||
|
await hass.async_block_till_done(wait_background_tasks=True)
|
||||||
|
|
||||||
|
state = hass.states.get(uptime_entity)
|
||||||
|
assert state
|
||||||
|
assert state.state == uptime
|
Loading…
x
Reference in New Issue
Block a user