mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Add tests to Zeversolar integration (#117928)
This commit is contained in:
parent
7554ca9460
commit
ee38099a91
@ -1705,10 +1705,6 @@ omit =
|
||||
homeassistant/components/zeroconf/models.py
|
||||
homeassistant/components/zeroconf/usage.py
|
||||
homeassistant/components/zestimate/sensor.py
|
||||
homeassistant/components/zeversolar/__init__.py
|
||||
homeassistant/components/zeversolar/coordinator.py
|
||||
homeassistant/components/zeversolar/entity.py
|
||||
homeassistant/components/zeversolar/sensor.py
|
||||
homeassistant/components/zha/core/cluster_handlers/*
|
||||
homeassistant/components/zha/core/device.py
|
||||
homeassistant/components/zha/core/gateway.py
|
||||
|
@ -1 +1,51 @@
|
||||
"""Tests for the Zeversolar integration."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from zeversolar import StatusEnum, ZeverSolarData
|
||||
|
||||
from homeassistant.components.zeversolar.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
MOCK_HOST_ZEVERSOLAR = "zeversolar-fake-host"
|
||||
MOCK_PORT_ZEVERSOLAR = 10200
|
||||
|
||||
|
||||
async def init_integration(hass: HomeAssistant) -> MockConfigEntry:
|
||||
"""Mock integration setup."""
|
||||
|
||||
zeverData = ZeverSolarData(
|
||||
wifi_enabled=False,
|
||||
serial_or_registry_id="1223",
|
||||
registry_key="A-2",
|
||||
hardware_version="M10",
|
||||
software_version="123-23",
|
||||
reported_datetime="19900101 23:00",
|
||||
communication_status=StatusEnum.OK,
|
||||
num_inverters=1,
|
||||
serial_number="123456778",
|
||||
pac=1234,
|
||||
energy_today=123,
|
||||
status=StatusEnum.OK,
|
||||
meter_status=StatusEnum.OK,
|
||||
)
|
||||
|
||||
with (
|
||||
patch("zeversolar.ZeverSolarClient.get_data", return_value=zeverData),
|
||||
):
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: MOCK_HOST_ZEVERSOLAR,
|
||||
CONF_PORT: MOCK_PORT_ZEVERSOLAR,
|
||||
},
|
||||
entry_id="my_id",
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
return entry
|
||||
|
123
tests/components/zeversolar/snapshots/test_sensor.ambr
Normal file
123
tests/components/zeversolar/snapshots/test_sensor.ambr
Normal file
@ -0,0 +1,123 @@
|
||||
# serializer version: 1
|
||||
# name: test_sensors
|
||||
ConfigEntrySnapshot({
|
||||
'data': dict({
|
||||
'host': 'zeversolar-fake-host',
|
||||
'port': 10200,
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'domain': 'zeversolar',
|
||||
'entry_id': <ANY>,
|
||||
'minor_version': 1,
|
||||
'options': dict({
|
||||
}),
|
||||
'pref_disable_new_entities': False,
|
||||
'pref_disable_polling': False,
|
||||
'source': 'user',
|
||||
'title': 'Mock Title',
|
||||
'unique_id': None,
|
||||
'version': 1,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.zeversolar_sensor_energy_today-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.zeversolar_sensor_energy_today',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Energy today',
|
||||
'platform': 'zeversolar',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'energy_today',
|
||||
'unique_id': '123456778_energy_today',
|
||||
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.zeversolar_sensor_energy_today-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'energy',
|
||||
'friendly_name': 'Zeversolar Sensor Energy today',
|
||||
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
|
||||
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.zeversolar_sensor_energy_today',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '123',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.zeversolar_sensor_power-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
|
||||
'entity_id': 'sensor.zeversolar_sensor_power',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.POWER: 'power'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Power',
|
||||
'platform': 'zeversolar',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'pac',
|
||||
'unique_id': '123456778_pac',
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.zeversolar_sensor_power-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'power',
|
||||
'friendly_name': 'Zeversolar Sensor Power',
|
||||
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.zeversolar_sensor_power',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1234',
|
||||
})
|
||||
# ---
|
32
tests/components/zeversolar/test_init.py
Normal file
32
tests/components/zeversolar/test_init.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""Test the init file code."""
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.zeversolar.__init__ as init
|
||||
from homeassistant.components.zeversolar.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from tests.common import MockConfigEntry, MockModule, mock_integration
|
||||
|
||||
MOCK_HOST_ZEVERSOLAR = "zeversolar-fake-host"
|
||||
MOCK_PORT_ZEVERSOLAR = 10200
|
||||
|
||||
|
||||
async def test_async_setup_entry_fails(hass: HomeAssistant) -> None:
|
||||
"""Test the sensor setup."""
|
||||
mock_integration(hass, MockModule(DOMAIN))
|
||||
|
||||
config = MockConfigEntry(
|
||||
data={
|
||||
CONF_HOST: MOCK_HOST_ZEVERSOLAR,
|
||||
CONF_PORT: MOCK_PORT_ZEVERSOLAR,
|
||||
},
|
||||
domain=DOMAIN,
|
||||
)
|
||||
|
||||
config.add_to_hass(hass)
|
||||
|
||||
with pytest.raises(ConfigEntryNotReady):
|
||||
await init.async_setup_entry(hass, config)
|
27
tests/components/zeversolar/test_sensor.py
Normal file
27
tests/components/zeversolar/test_sensor.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""Test the sensor classes."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import init_integration
|
||||
|
||||
from tests.common import snapshot_platform
|
||||
|
||||
|
||||
async def test_sensors(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
|
||||
) -> None:
|
||||
"""Test sensors."""
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.zeversolar.PLATFORMS",
|
||||
[Platform.SENSOR],
|
||||
):
|
||||
entry = await init_integration(hass)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
|
Loading…
x
Reference in New Issue
Block a user