Add sensors tests for Peblar Rocksolid EV Chargers (#133710)

This commit is contained in:
Franck Nijhof 2024-12-21 12:20:03 +01:00 committed by GitHub
parent aad1d6a25d
commit dbe04f17ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 102 additions and 2 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations
from collections.abc import Generator
from contextlib import nullcontext
from unittest.mock import MagicMock, patch
from peblar import PeblarMeter, PeblarSystemInformation, PeblarVersions
@ -67,11 +68,17 @@ async def init_integration(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_peblar: MagicMock,
request: pytest.FixtureRequest,
) -> MockConfigEntry:
"""Set up the Peblar integration for testing."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
context = nullcontext()
if platform := getattr(request, "param", None):
context = patch("homeassistant.components.peblar.PLATFORMS", [platform])
with context:
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry

View File

@ -0,0 +1,58 @@
# serializer version: 1
# name: test_entities[sensor][sensor.peblar_ev_charger_energy-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.peblar_ev_charger_energy',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None,
'original_name': 'Energy',
'platform': 'peblar',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '23-45-A4O-MOF_energy_total',
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
})
# ---
# name: test_entities[sensor][sensor.peblar_ev_charger_energy-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'energy',
'friendly_name': 'Peblar EV Charger Energy',
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
'context': <ANY>,
'entity_id': 'sensor.peblar_ev_charger_energy',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '880.321',
})
# ---

View File

@ -0,0 +1,35 @@
"""Tests for the Peblar sensor platform."""
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.peblar.const import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize("init_integration", [Platform.SENSOR], indirect=True)
@pytest.mark.usefixtures("init_integration")
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the sensor entities."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
# Ensure all entities are correctly assigned to the Peblar device
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, "23-45-A4O-MOF")}
)
assert device_entry
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
for entity_entry in entity_entries:
assert entity_entry.device_id == device_entry.id