From b62c01a87781f2a6fd5ce4c879e04fef93d2b04c Mon Sep 17 00:00:00 2001 From: Adam Pasztor Date: Tue, 24 Nov 2026 12:44:32 +0100 Subject: [PATCH] Add tests for the ADS sensor platform (#194098) --- .../components/ads/snapshots/test_sensor.ambr | 109 ++++++++++++ tests/components/ads/test_sensor.py | 163 ++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 tests/components/ads/snapshots/test_sensor.ambr create mode 100644 tests/components/ads/test_sensor.py diff --git a/tests/components/ads/snapshots/test_sensor.ambr b/tests/components/ads/snapshots/test_sensor.ambr new file mode 100644 index 000000000000..53b3f8601e98 --- /dev/null +++ b/tests/components/ads/snapshots/test_sensor.ambr @@ -0,0 +1,109 @@ +# serializer version: 1 +# name: test_sensor[defaults][entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': None, + 'config_subentry_id': None, + 'device_class': None, + 'device_id': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.ads_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'ADS sensor', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'ADS sensor', + 'platform': 'ads', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'GVL.temperature', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor[defaults][state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'ADS sensor', + }), + 'context': , + 'entity_id': 'sensor.ads_sensor', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '42', + }) +# --- +# name: test_sensor[temperature][entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': dict({ + : , + }), + 'config_entry_id': None, + 'config_subentry_id': None, + 'device_class': None, + 'device_id': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.temperature', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Temperature', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'ads', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'GVL.temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor[temperature][state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'temperature', + : 'Temperature', + : , + : , + }), + 'context': , + 'entity_id': 'sensor.temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '21.5', + }) +# --- diff --git a/tests/components/ads/test_sensor.py b/tests/components/ads/test_sensor.py new file mode 100644 index 000000000000..535151004052 --- /dev/null +++ b/tests/components/ads/test_sensor.py @@ -0,0 +1,163 @@ +"""Test the ADS sensor platform.""" + +import struct +from typing import Any + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import MockAdsDevice, async_run_past_first_update, setup_ads_platform + +ADS_VAR = "GVL.temperature" +ENTITY_ID = "sensor.temperature" + + +@pytest.mark.parametrize( + ("platform_config", "payload", "entity_id"), + [ + pytest.param( + { + "adsvar": ADS_VAR, + "name": "Temperature", + "adstype": "int", + "factor": 10, + "device_class": "temperature", + "state_class": "measurement", + "unit_of_measurement": "°C", + }, + struct.pack(" None: + """Test the sensor is created from YAML with the PLC's value.""" + mock_ads_device.values[ADS_VAR] = payload + + assert await setup_ads_platform(hass, SENSOR_DOMAIN, platform_config) + + assert entity_registry.async_get(entity_id) == snapshot(name="entry") + assert hass.states.get(entity_id) == snapshot(name="state") + + +@pytest.mark.parametrize( + ("adstype", "payload", "expected"), + [ + # The value is shown as is, so a BOOL sensor reads "True" or "False". + pytest.param("bool", b"\x01", "True", id="bool"), + pytest.param("byte", struct.pack(" None: + """Test the sensor decodes the value as its ADS type.""" + mock_ads_device.values[ADS_VAR] = payload + + assert await setup_ads_platform( + hass, + SENSOR_DOMAIN, + {"adsvar": ADS_VAR, "name": "Temperature", "adstype": adstype}, + ) + + assert hass.states.get(ENTITY_ID).state == expected + + +async def test_state_follows_the_plc( + hass: HomeAssistant, mock_ads_device: MockAdsDevice +) -> None: + """Test the sensor follows the values the PLC pushes, scaled by the factor.""" + mock_ads_device.values[ADS_VAR] = struct.pack(" None: + """Test a sensor with an unsupported ADS type is not created.""" + await setup_ads_platform( + hass, + SENSOR_DOMAIN, + {"adsvar": ADS_VAR, "name": "Temperature", "adstype": adstype}, + ) + + assert hass.states.get(ENTITY_ID) is None + assert "Invalid config for 'sensor' from integration 'ads'" in caplog.text + + +# The division by zero happens in the pyads callback thread. +@pytest.mark.filterwarnings( + "ignore:Exception in thread:pytest.PytestUnhandledThreadExceptionWarning" +) +async def test_zero_factor(hass: HomeAssistant, mock_ads_device: MockAdsDevice) -> None: + """Test a factor of zero is accepted, and the sensor never gets a value.""" + mock_ads_device.values[ADS_VAR] = struct.pack("