Add tests for kitchen_sink sensor platform (#94724)

* Add tests for kitchen_sink sensor platform

* Address review comments
This commit is contained in:
Erik Montnemery 2023-06-19 14:16:18 +02:00 committed by GitHub
parent e49c2fde14
commit 3ee63ba2c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 12 deletions

View file

@ -7,7 +7,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_BATTERY_LEVEL, UnitOfPower
from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -31,7 +31,6 @@ async def async_setup_entry(
None,
SensorStateClass.MEASUREMENT,
UnitOfPower.WATT, # Not a volume unit
None,
),
DemoSensor(
"statistics_issue_2",
@ -40,7 +39,6 @@ async def async_setup_entry(
None,
SensorStateClass.MEASUREMENT,
"dogs", # Can't be converted to cats
None,
),
DemoSensor(
"statistics_issue_3",
@ -49,7 +47,6 @@ async def async_setup_entry(
None,
None, # Wrong state class
UnitOfPower.WATT,
None,
),
]
)
@ -68,9 +65,6 @@ class DemoSensor(SensorEntity):
device_class: SensorDeviceClass | None,
state_class: SensorStateClass | None,
unit_of_measurement: str | None,
battery: StateType,
options: list[str] | None = None,
translation_key: str | None = None,
) -> None:
"""Initialize the sensor."""
self._attr_device_class = device_class
@ -79,13 +73,8 @@ class DemoSensor(SensorEntity):
self._attr_native_value = state
self._attr_state_class = state_class
self._attr_unique_id = unique_id
self._attr_options = options
self._attr_translation_key = translation_key
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=name,
)
if battery:
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}

View file

@ -0,0 +1,40 @@
# serializer version: 1
# name: test_states
set({
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Statistics issue 1',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
'context': <ANY>,
'entity_id': 'sensor.statistics_issue_1',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '100',
}),
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Statistics issue 2',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': 'dogs',
}),
'context': <ANY>,
'entity_id': 'sensor.statistics_issue_2',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '100',
}),
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Statistics issue 3',
'unit_of_measurement': <UnitOfPower.WATT: 'W'>,
}),
'context': <ANY>,
'entity_id': 'sensor.statistics_issue_3',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '100',
}),
})
# ---

View file

@ -0,0 +1,33 @@
"""The tests for the kitchen_sink sensor platform."""
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.kitchen_sink import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@pytest.fixture
async def sensor_only() -> None:
"""Enable only the sensor platform."""
with patch(
"homeassistant.components.kitchen_sink.COMPONENTS_WITH_DEMO_PLATFORM",
[Platform.SENSOR],
):
yield
@pytest.fixture(autouse=True)
async def setup_comp(hass: HomeAssistant, sensor_only):
"""Set up demo component."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()
async def test_states(hass: HomeAssistant, snapshot: SnapshotAssertion) -> None:
"""Test the expected sensor entities are added."""
states = hass.states.async_all()
assert set(states) == snapshot