Skip to content

Commit

Permalink
Add sensor state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus committed Feb 23, 2024
1 parent 7ea1344 commit 016520f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ def evcharger_factory(*args, **kwargs):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
yield evcharger


@pytest.fixture(name="channel_values")
def get_channel_values():
"""Get a dirctionary with the measurment and parameter channel values."""
channel_values = {}
for channel in MEASUREMENTS:
channel_values[channel["channelId"]] = channel["values"][0].get("value")

for channel in PARAMETERS[0]["values"]:
channel_values[channel["channelId"]] = channel.get("value")

return channel_values
66 changes: 59 additions & 7 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
"""Test for the SMA EV Charger sensor platform."""
from datetime import timedelta

from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow
from pytest_homeassistant_custom_component.common import async_fire_time_changed

from custom_components.smaev import generate_smaev_entity_id
from custom_components.smaev.const import DEFAULT_SCAN_INTERVAL
from custom_components.smaev.sensor import ENTITY_ID_FORMAT, SENSOR_DESCRIPTIONS


async def test_setup_smaev_sensor(hass: HomeAssistant, entry, evcharger):
"""Test the setup of sensor."""
for description in SENSOR_DESCRIPTIONS:
if not description.entity_registry_enabled_default:
continue
entity_id = generate_smaev_entity_id(
hass, entry, ENTITY_ID_FORMAT, description, suffix=False
def get_entity_ids_and_descriptions(hass, entry) -> tuple:
"""Return a list with (entity_id, entity_description)."""
items = [
(
generate_smaev_entity_id(
hass, entry, ENTITY_ID_FORMAT, description, suffix=False
),
description,
)
for description in SENSOR_DESCRIPTIONS
if description.entity_registry_enabled_default
]
return items


async def test_setup_smaev_sensor(hass: HomeAssistant, entry, evcharger) -> None:
"""Test the setup of sensor."""
for entity_id, description in get_entity_ids_and_descriptions(hass, entry):
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_UNKNOWN
Expand All @@ -28,3 +43,40 @@ async def test_setup_smaev_sensor(hass: HomeAssistant, entry, evcharger):
== description.native_unit_of_measurement
)
assert state.attributes.get(ATTR_DEVICE_CLASS) == description.device_class


async def test_entity_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, entry, evcharger
) -> None:
"""Test tje attributes of an entity."""
for entity_id, description in get_entity_ids_and_descriptions(hass, entry):
entity = entity_registry.async_get(entity_id)
assert entity
assert entity.unique_id == f"{entry.unique_id}-{description.key}"


async def test_status_change(
hass: HomeAssistant, entry, evcharger, channel_values
) -> None:
"""Test sensor changes its state on coordinator update."""
# Make the coordinator refresh data.
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=DEFAULT_SCAN_INTERVAL + 1)
)
await hass.async_block_till_done()

for entity_id, description in get_entity_ids_and_descriptions(hass, entry):
value = channel_values[description.channel]
value = description.value_mapping.get(value, value)

state = hass.states.get(entity_id)
assert state.state == str(value)


async def test_unload_config_entry(hass: HomeAssistant, entry, evcharger) -> None:
"""Test the sensor is removed when the config entry is unloaded."""
items = get_entity_ids_and_descriptions(hass, entry)
await hass.config_entries.async_unload(entry.entry_id)

for entity_id, _ in items:
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE

0 comments on commit 016520f

Please sign in to comment.