From 277ee03145a42588f919d38f3a5829d31a33c278 Mon Sep 17 00:00:00 2001
From: Simone Chemelli <simone.chemelli@gmail.com>
Date: Tue, 31 Dec 2024 03:55:54 -0500
Subject: [PATCH] Full test coverage for Vodafone Station sensor platform
 (#133285)

Co-authored-by: Joostlek <joostlek@outlook.com>
---
 .../components/vodafone_station/const.py      |  1 +
 .../vodafone_station/coordinator.py           |  4 +-
 tests/components/vodafone_station/conftest.py | 28 ++++++
 .../vodafone_station/test_sensor.py           | 94 +++++++++++++++++++
 4 files changed, 125 insertions(+), 2 deletions(-)
 create mode 100644 tests/components/vodafone_station/conftest.py
 create mode 100644 tests/components/vodafone_station/test_sensor.py

diff --git a/homeassistant/components/vodafone_station/const.py b/homeassistant/components/vodafone_station/const.py
index 14cfaabdf7a..99f953d50d5 100644
--- a/homeassistant/components/vodafone_station/const.py
+++ b/homeassistant/components/vodafone_station/const.py
@@ -5,6 +5,7 @@ import logging
 _LOGGER = logging.getLogger(__package__)
 
 DOMAIN = "vodafone_station"
+SCAN_INTERVAL = 30
 
 DEFAULT_DEVICE_NAME = "Unknown device"
 DEFAULT_HOST = "192.168.1.1"
diff --git a/homeassistant/components/vodafone_station/coordinator.py b/homeassistant/components/vodafone_station/coordinator.py
index e95ca2b5976..de794488040 100644
--- a/homeassistant/components/vodafone_station/coordinator.py
+++ b/homeassistant/components/vodafone_station/coordinator.py
@@ -14,7 +14,7 @@ from homeassistant.helpers.device_registry import DeviceInfo
 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
 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()
 
@@ -59,7 +59,7 @@ class VodafoneStationRouter(DataUpdateCoordinator[UpdateCoordinatorDataType]):
             hass=hass,
             logger=_LOGGER,
             name=f"{DOMAIN}-{host}-coordinator",
-            update_interval=timedelta(seconds=30),
+            update_interval=timedelta(seconds=SCAN_INTERVAL),
         )
 
     def _calculate_update_time_and_consider_home(
diff --git a/tests/components/vodafone_station/conftest.py b/tests/components/vodafone_station/conftest.py
new file mode 100644
index 00000000000..c36382e4c01
--- /dev/null
+++ b/tests/components/vodafone_station/conftest.py
@@ -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
diff --git a/tests/components/vodafone_station/test_sensor.py b/tests/components/vodafone_station/test_sensor.py
new file mode 100644
index 00000000000..3a63566b5dc
--- /dev/null
+++ b/tests/components/vodafone_station/test_sensor.py
@@ -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
-- 
GitLab