2024-12-04 00:35:50 +01:00
|
|
|
"""Test the sensors provided by the Powerfox integration."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
|
|
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2026-02-28 02:57:02 +01:00
|
|
|
from powerfox import (
|
|
|
|
|
DeviceReport,
|
|
|
|
|
PowerfoxConnectionError,
|
|
|
|
|
PowerfoxNoDataError,
|
|
|
|
|
PowerfoxPrivacyError,
|
|
|
|
|
)
|
2026-01-08 14:53:00 +01:00
|
|
|
import pytest
|
2025-05-19 14:55:48 +03:00
|
|
|
from syrupy.assertion import SnapshotAssertion
|
2024-12-04 00:35:50 +01:00
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
|
|
|
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
|
|
|
|
|
|
from . import setup_integration
|
|
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
|
|
|
|
|
|
|
|
|
|
2026-01-08 14:53:00 +01:00
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
2024-12-04 00:35:50 +01:00
|
|
|
async def test_all_sensors(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
mock_powerfox_client: AsyncMock,
|
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
|
entity_registry: er.EntityRegistry,
|
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test the Powerfox sensors."""
|
|
|
|
|
with patch("homeassistant.components.powerfox.PLATFORMS", [Platform.SENSOR]):
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
|
|
|
|
|
|
|
|
|
|
2026-02-28 02:57:02 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"exception",
|
|
|
|
|
[PowerfoxConnectionError, PowerfoxNoDataError, PowerfoxPrivacyError],
|
|
|
|
|
)
|
2024-12-04 00:35:50 +01:00
|
|
|
async def test_update_failed(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
mock_powerfox_client: AsyncMock,
|
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
|
freezer: FrozenDateTimeFactory,
|
2026-02-28 02:57:02 +01:00
|
|
|
exception: Exception,
|
2024-12-04 00:35:50 +01:00
|
|
|
) -> None:
|
|
|
|
|
"""Test entities become unavailable after failed update."""
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
assert mock_config_entry.state is ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.poweropti_energy_usage").state is not None
|
|
|
|
|
|
2026-02-28 02:57:02 +01:00
|
|
|
mock_powerfox_client.device.side_effect = exception
|
2024-12-04 00:35:50 +01:00
|
|
|
freezer.tick(timedelta(minutes=5))
|
|
|
|
|
async_fire_time_changed(hass)
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.poweropti_energy_usage").state == STATE_UNAVAILABLE
|
2026-01-08 14:53:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_skips_gas_sensors_when_report_missing(
|
|
|
|
|
hass: HomeAssistant,
|
|
|
|
|
mock_powerfox_client: AsyncMock,
|
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test gas sensors are not created when report lacks gas data."""
|
|
|
|
|
mock_powerfox_client.report.return_value = DeviceReport(gas=None)
|
|
|
|
|
|
|
|
|
|
with patch("homeassistant.components.powerfox.PLATFORMS", [Platform.SENSOR]):
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
|
|
assert hass.states.get("sensor.gasopti_gas_consumption_today") is None
|