mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Add tests for kitchen_sink sensor platform (#94724)
* Add tests for kitchen_sink sensor platform * Address review comments
This commit is contained in:
parent
e49c2fde14
commit
3ee63ba2c2
@ -7,7 +7,7 @@ from homeassistant.components.sensor import (
|
|||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -31,7 +31,6 @@ async def async_setup_entry(
|
|||||||
None,
|
None,
|
||||||
SensorStateClass.MEASUREMENT,
|
SensorStateClass.MEASUREMENT,
|
||||||
UnitOfPower.WATT, # Not a volume unit
|
UnitOfPower.WATT, # Not a volume unit
|
||||||
None,
|
|
||||||
),
|
),
|
||||||
DemoSensor(
|
DemoSensor(
|
||||||
"statistics_issue_2",
|
"statistics_issue_2",
|
||||||
@ -40,7 +39,6 @@ async def async_setup_entry(
|
|||||||
None,
|
None,
|
||||||
SensorStateClass.MEASUREMENT,
|
SensorStateClass.MEASUREMENT,
|
||||||
"dogs", # Can't be converted to cats
|
"dogs", # Can't be converted to cats
|
||||||
None,
|
|
||||||
),
|
),
|
||||||
DemoSensor(
|
DemoSensor(
|
||||||
"statistics_issue_3",
|
"statistics_issue_3",
|
||||||
@ -49,7 +47,6 @@ async def async_setup_entry(
|
|||||||
None,
|
None,
|
||||||
None, # Wrong state class
|
None, # Wrong state class
|
||||||
UnitOfPower.WATT,
|
UnitOfPower.WATT,
|
||||||
None,
|
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -68,9 +65,6 @@ class DemoSensor(SensorEntity):
|
|||||||
device_class: SensorDeviceClass | None,
|
device_class: SensorDeviceClass | None,
|
||||||
state_class: SensorStateClass | None,
|
state_class: SensorStateClass | None,
|
||||||
unit_of_measurement: str | None,
|
unit_of_measurement: str | None,
|
||||||
battery: StateType,
|
|
||||||
options: list[str] | None = None,
|
|
||||||
translation_key: str | None = None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._attr_device_class = device_class
|
self._attr_device_class = device_class
|
||||||
@ -79,13 +73,8 @@ class DemoSensor(SensorEntity):
|
|||||||
self._attr_native_value = state
|
self._attr_native_value = state
|
||||||
self._attr_state_class = state_class
|
self._attr_state_class = state_class
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._attr_options = options
|
|
||||||
self._attr_translation_key = translation_key
|
|
||||||
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, unique_id)},
|
identifiers={(DOMAIN, unique_id)},
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
|
||||||
if battery:
|
|
||||||
self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: battery}
|
|
||||||
|
40
tests/components/kitchen_sink/snapshots/test_sensor.ambr
Normal file
40
tests/components/kitchen_sink/snapshots/test_sensor.ambr
Normal 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',
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
# ---
|
33
tests/components/kitchen_sink/test_sensor.py
Normal file
33
tests/components/kitchen_sink/test_sensor.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user