1
0
mirror of https://github.com/home-assistant/core.git synced 2026-06-01 05:04:21 +01:00
Files
core/tests/components/sma/test_sensor.py
T

75 lines
2.2 KiB
Python

"""Test the SMA sensor platform."""
from collections.abc import Generator
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
from pysma.exceptions import (
SmaAuthenticationException,
SmaConnectionException,
SmaReadException,
)
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.sma.const import DEFAULT_SCAN_INTERVAL
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 homeassistant.util import dt as dt_util
from . import setup_integration
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_sma_client: Generator,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch(
"homeassistant.components.sma.PLATFORMS",
[Platform.SENSOR],
):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(
hass, entity_registry, snapshot, mock_config_entry.entry_id
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize(
("exception"),
[
(SmaConnectionException),
(SmaAuthenticationException),
(SmaReadException),
(Exception),
],
)
async def test_refresh_exceptions(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_sma_client: Generator,
freezer: FrozenDateTimeFactory,
exception: Exception,
) -> None:
"""Test the coordinator refresh exceptions."""
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_sma_client.read.side_effect = exception
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow())
await hass.async_block_till_done()
state = hass.states.get("sensor.sma_device_name_battery_capacity_a")
assert state.state == STATE_UNAVAILABLE