"""Tests for SolarEdge optimizer sensors.""" from __future__ import annotations from unittest.mock import AsyncMock, patch from homeassistant.const import CONF_URL from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.helpers import entity_registry as er from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.solaredge_optimizers.api import parse_snapshot from custom_components.solaredge_optimizers.const import CONF_SCAN_INTERVAL, DOMAIN from .sample_data import SAMPLE_PAYLOAD async def test_optimizer_sensors(hass: HomeAssistant) -> None: """Create energy and power sensors for every optimizer.""" entry = MockConfigEntry( domain=DOMAIN, title="SolarEdge Site 4886699", unique_id="4886699", data={CONF_URL: "http://app:8099", CONF_SCAN_INTERVAL: 600}, ) entry.add_to_hass(hass) with patch( "custom_components.solaredge_optimizers.api." "SolarEdgeOptimizerApiClient.async_get_optimizers", AsyncMock(return_value=parse_snapshot(SAMPLE_PAYLOAD)), ): assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() entity_registry = er.async_get(hass) entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id) assert len(entities) == 4 energy_entity = next( entity for entity in entities if entity.unique_id.endswith("_daily_energy") ) power_entity = next( entity for entity in entities if entity.unique_id.endswith("_current_power") ) assert hass.states.get(energy_entity.entity_id).state == "1234.5" assert hass.states.get(power_entity.entity_id).state == "160.7" device_registry = dr.async_get(hass) devices = dr.async_entries_for_config_entry(device_registry, entry.entry_id) assert len(devices) == 2 assert {device.serial_number for device in devices} == { "14F28854-E2", "14F28855-E2", }