mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add additional tests for solarlog (#119928)
This commit is contained in:
parent
904cf26d31
commit
af9f4f310b
@ -1259,9 +1259,6 @@ omit =
|
||||
homeassistant/components/solaredge/__init__.py
|
||||
homeassistant/components/solaredge/coordinator.py
|
||||
homeassistant/components/solaredge_local/sensor.py
|
||||
homeassistant/components/solarlog/__init__.py
|
||||
homeassistant/components/solarlog/coordinator.py
|
||||
homeassistant/components/solarlog/sensor.py
|
||||
homeassistant/components/solax/__init__.py
|
||||
homeassistant/components/solax/sensor.py
|
||||
homeassistant/components/soma/__init__.py
|
||||
|
@ -1 +1,19 @@
|
||||
"""Tests for the solarlog integration."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_platform(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, platforms: list[Platform]
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the SolarLog platform."""
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.solarlog.PLATFORMS", platforms):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
@ -5,21 +5,57 @@ from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.solarlog.const import DOMAIN as SOLARLOG_DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import mock_device_registry, mock_registry
|
||||
from .const import HOST, NAME
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
load_json_object_fixture,
|
||||
mock_device_registry,
|
||||
mock_registry,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_solarlog():
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock a config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=SOLARLOG_DOMAIN,
|
||||
title="solarlog",
|
||||
data={
|
||||
CONF_HOST: HOST,
|
||||
CONF_NAME: NAME,
|
||||
"extended_data": True,
|
||||
},
|
||||
minor_version=2,
|
||||
entry_id="ce5f5431554d101905d31797e1232da8",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_solarlog_connector():
|
||||
"""Build a fixture for the SolarLog API that connects successfully and returns one device."""
|
||||
|
||||
mock_solarlog_api = AsyncMock()
|
||||
with patch(
|
||||
"homeassistant.components.solarlog.config_flow.SolarLogConnector",
|
||||
return_value=mock_solarlog_api,
|
||||
) as mock_solarlog_api:
|
||||
mock_solarlog_api.return_value.test_connection.return_value = True
|
||||
mock_solarlog_api.test_connection = AsyncMock(return_value=True)
|
||||
mock_solarlog_api.update_data.return_value = load_json_object_fixture(
|
||||
"solarlog_data.json", SOLARLOG_DOMAIN
|
||||
)
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.solarlog.coordinator.SolarLogConnector",
|
||||
autospec=True,
|
||||
return_value=mock_solarlog_api,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.solarlog.config_flow.SolarLogConnector",
|
||||
autospec=True,
|
||||
return_value=mock_solarlog_api,
|
||||
),
|
||||
):
|
||||
yield mock_solarlog_api
|
||||
|
||||
|
||||
|
4
tests/components/solarlog/const.py
Normal file
4
tests/components/solarlog/const.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""Common const used across tests for SolarLog."""
|
||||
|
||||
NAME = "Solarlog test 1 2 3"
|
||||
HOST = "http://1.1.1.1"
|
24
tests/components/solarlog/fixtures/solarlog_data.json
Normal file
24
tests/components/solarlog/fixtures/solarlog_data.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"power_ac": 100,
|
||||
"power_dc": 102,
|
||||
"voltage_ac": 100,
|
||||
"voltage_dc": 100,
|
||||
"yield_day": 4.21,
|
||||
"yield_yesterday": 5.21,
|
||||
"yield_month": 515,
|
||||
"yield_year": 1023,
|
||||
"yield_total": 56513,
|
||||
"consumption_ac": 54.87,
|
||||
"consumption_day": 5.31,
|
||||
"consumption_yesterday": 7.34,
|
||||
"consumption_month": 758,
|
||||
"consumption_year": 4587,
|
||||
"consumption_total": 354687,
|
||||
"total_power": 120,
|
||||
"self_consumption_year": 545,
|
||||
"alternator_loss": 2,
|
||||
"efficiency": 0.9804,
|
||||
"usage": 0.5487,
|
||||
"power_available": 45.13,
|
||||
"capacity": 0.85
|
||||
}
|
2183
tests/components/solarlog/snapshots/test_sensor.ambr
Normal file
2183
tests/components/solarlog/snapshots/test_sensor.ambr
Normal file
File diff suppressed because it is too large
Load Diff
@ -12,10 +12,9 @@ from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from .const import HOST, NAME
|
||||
|
||||
NAME = "Solarlog test 1 2 3"
|
||||
HOST = "http://1.1.1.1"
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
@ -56,7 +55,7 @@ def init_config_flow(hass):
|
||||
@pytest.mark.usefixtures("test_connect")
|
||||
async def test_user(
|
||||
hass: HomeAssistant,
|
||||
mock_solarlog: AsyncMock,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test user config."""
|
||||
@ -89,7 +88,7 @@ async def test_form_exceptions(
|
||||
hass: HomeAssistant,
|
||||
exception: Exception,
|
||||
error: dict[str, str],
|
||||
mock_solarlog: AsyncMock,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
) -> None:
|
||||
"""Test we can handle Form exceptions."""
|
||||
flow = init_config_flow(hass)
|
||||
@ -98,7 +97,7 @@ async def test_form_exceptions(
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
mock_solarlog.return_value.test_connection.side_effect = exception
|
||||
mock_solarlog_connector.test_connection.side_effect = exception
|
||||
|
||||
# tests with connection error
|
||||
result = await flow.async_step_user(
|
||||
@ -110,7 +109,7 @@ async def test_form_exceptions(
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == error
|
||||
|
||||
mock_solarlog.return_value.test_connection.side_effect = None
|
||||
mock_solarlog_connector.test_connection.side_effect = None
|
||||
|
||||
# tests with all provided
|
||||
result = await flow.async_step_user(
|
||||
|
@ -1,16 +1,54 @@
|
||||
"""Test the initialization."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from solarlog_cli.solarlog_exceptions import SolarLogConnectionError
|
||||
|
||||
from homeassistant.components.solarlog.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceRegistry
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
|
||||
from .test_config_flow import HOST, NAME
|
||||
from . import setup_platform
|
||||
from .const import HOST, NAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_load_unload(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
) -> None:
|
||||
"""Test load and unload."""
|
||||
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
async def test_raise_config_entry_not_ready_when_offline(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
) -> None:
|
||||
"""Config entry state is SETUP_RETRY when Solarlog is offline."""
|
||||
|
||||
mock_solarlog_connector.update_data.side_effect = SolarLogConnectionError
|
||||
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
assert len(hass.config_entries.flow.async_progress()) == 0
|
||||
|
||||
|
||||
async def test_migrate_config_entry(
|
||||
hass: HomeAssistant, device_reg: DeviceRegistry, entity_reg: EntityRegistry
|
||||
) -> None:
|
||||
|
59
tests/components/solarlog/test_sensor.py
Normal file
59
tests/components/solarlog/test_sensor.py
Normal file
@ -0,0 +1,59 @@
|
||||
"""Test the Home Assistant solarlog sensor module."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from solarlog_cli.solarlog_exceptions import (
|
||||
SolarLogConnectionError,
|
||||
SolarLogUpdateError,
|
||||
)
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import STATE_UNAVAILABLE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_platform
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception",
|
||||
[
|
||||
SolarLogConnectionError,
|
||||
SolarLogUpdateError,
|
||||
],
|
||||
)
|
||||
async def test_connection_error(
|
||||
hass: HomeAssistant,
|
||||
exception: Exception,
|
||||
mock_solarlog_connector: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test connection error."""
|
||||
await setup_platform(hass, mock_config_entry, [Platform.SENSOR])
|
||||
|
||||
mock_solarlog_connector.update_data.side_effect = exception
|
||||
|
||||
freezer.tick(delta=timedelta(hours=12))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("sensor.solarlog_power_ac").state == STATE_UNAVAILABLE
|
Loading…
x
Reference in New Issue
Block a user