mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Add tests for the ADS sensor platform (#194098)
This commit is contained in:
@@ -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': <ANY>,
|
||||
'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({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'ADS sensor',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.ads_sensor',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '42',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[temperature][entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'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': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Temperature',
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 1,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.TEMPERATURE: 'temperature'>,
|
||||
'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': <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensor[temperature][state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'temperature',
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Temperature',
|
||||
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
<EntityStateAttribute.UNIT_OF_MEASUREMENT: 'unit_of_measurement'>: <UnitOfTemperature.CELSIUS: '°C'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.temperature',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '21.5',
|
||||
})
|
||||
# ---
|
||||
@@ -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("<h", 215),
|
||||
ENTITY_ID,
|
||||
id="temperature",
|
||||
),
|
||||
pytest.param(
|
||||
{"adsvar": ADS_VAR},
|
||||
struct.pack("<h", 42),
|
||||
"sensor.ads_sensor",
|
||||
id="defaults",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_sensor(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_ads_device: MockAdsDevice,
|
||||
snapshot: SnapshotAssertion,
|
||||
platform_config: dict[str, Any],
|
||||
payload: bytes,
|
||||
entity_id: str,
|
||||
) -> 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("<B", 200), "200", id="byte"),
|
||||
pytest.param("usint", struct.pack("<B", 200), "200", id="usint"),
|
||||
pytest.param("sint", struct.pack("<b", -100), "-100", id="sint"),
|
||||
pytest.param("int", struct.pack("<h", -1000), "-1000", id="int"),
|
||||
pytest.param("uint", struct.pack("<H", 60000), "60000", id="uint"),
|
||||
pytest.param("word", struct.pack("<H", 60000), "60000", id="word"),
|
||||
pytest.param("dint", struct.pack("<i", -100000), "-100000", id="dint"),
|
||||
pytest.param("udint", struct.pack("<I", 100000), "100000", id="udint"),
|
||||
# Decoded as signed, so values from 2**31 up come out negative.
|
||||
pytest.param(
|
||||
"udint", struct.pack("<I", 3_000_000_000), "-1294967296", id="udint_high"
|
||||
),
|
||||
pytest.param("dword", struct.pack("<I", 100000), "100000", id="dword"),
|
||||
pytest.param(
|
||||
"dword", struct.pack("<I", 3_000_000_000), "-1294967296", id="dword_high"
|
||||
),
|
||||
pytest.param("real", struct.pack("<f", 21.5), "21.5", id="real"),
|
||||
pytest.param("lreal", struct.pack("<d", -0.125), "-0.125", id="lreal"),
|
||||
],
|
||||
)
|
||||
async def test_decodes_the_plc_value(
|
||||
hass: HomeAssistant,
|
||||
mock_ads_device: MockAdsDevice,
|
||||
adstype: str,
|
||||
payload: bytes,
|
||||
expected: str,
|
||||
) -> 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("<h", 215)
|
||||
assert await setup_ads_platform(
|
||||
hass, SENSOR_DOMAIN, {"adsvar": ADS_VAR, "name": "Temperature", "factor": 10}
|
||||
)
|
||||
assert hass.states.get(ENTITY_ID).state == "21.5"
|
||||
|
||||
await mock_ads_device.async_push(hass, ADS_VAR, struct.pack("<h", -40))
|
||||
assert hass.states.get(ENTITY_ID).state == "-4.0"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"adstype",
|
||||
[
|
||||
pytest.param("string", id="not_for_sensors"),
|
||||
pytest.param("nonsense", id="unknown"),
|
||||
],
|
||||
)
|
||||
async def test_invalid_config(
|
||||
hass: HomeAssistant,
|
||||
mock_ads_device: MockAdsDevice,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
adstype: str,
|
||||
) -> 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("<h", 215)
|
||||
|
||||
assert await async_run_past_first_update(
|
||||
hass,
|
||||
mock_ads_device,
|
||||
setup_ads_platform(
|
||||
hass,
|
||||
SENSOR_DOMAIN,
|
||||
{"adsvar": ADS_VAR, "name": "Temperature", "factor": 0},
|
||||
),
|
||||
)
|
||||
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
|
||||
Reference in New Issue
Block a user