From 3ee63ba2c29a979213d09f0f0da5867b829d49df Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 19 Jun 2023 14:16:18 +0200 Subject: [PATCH] Add tests for kitchen_sink sensor platform (#94724) * Add tests for kitchen_sink sensor platform * Address review comments --- .../components/kitchen_sink/sensor.py | 13 +----- .../kitchen_sink/snapshots/test_sensor.ambr | 40 +++++++++++++++++++ tests/components/kitchen_sink/test_sensor.py | 33 +++++++++++++++ 3 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 tests/components/kitchen_sink/snapshots/test_sensor.ambr create mode 100644 tests/components/kitchen_sink/test_sensor.py diff --git a/homeassistant/components/kitchen_sink/sensor.py b/homeassistant/components/kitchen_sink/sensor.py index 6692f53810b..6912c940482 100644 --- a/homeassistant/components/kitchen_sink/sensor.py +++ b/homeassistant/components/kitchen_sink/sensor.py @@ -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} diff --git a/tests/components/kitchen_sink/snapshots/test_sensor.ambr b/tests/components/kitchen_sink/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..de3297b7fd8 --- /dev/null +++ b/tests/components/kitchen_sink/snapshots/test_sensor.ambr @@ -0,0 +1,40 @@ +# serializer version: 1 +# name: test_states + set({ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Statistics issue 1', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.statistics_issue_1', + 'last_changed': , + 'last_updated': , + 'state': '100', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Statistics issue 2', + 'state_class': , + 'unit_of_measurement': 'dogs', + }), + 'context': , + 'entity_id': 'sensor.statistics_issue_2', + 'last_changed': , + 'last_updated': , + 'state': '100', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Statistics issue 3', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.statistics_issue_3', + 'last_changed': , + 'last_updated': , + 'state': '100', + }), + }) +# --- diff --git a/tests/components/kitchen_sink/test_sensor.py b/tests/components/kitchen_sink/test_sensor.py new file mode 100644 index 00000000000..8d3f611f15d --- /dev/null +++ b/tests/components/kitchen_sink/test_sensor.py @@ -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