1
0
mirror of https://github.com/home-assistant/core synced 2024-07-08 20:17:01 +00:00

Add snapshots to tasmota sensor test (#120647)

This commit is contained in:
Erik Montnemery 2024-06-27 10:43:28 +02:00 committed by GitHub
parent 85629dc31e
commit 06f495dd45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 1533 additions and 211 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,9 +13,9 @@ from hatasmota.utils import (
get_topic_tele_will,
)
import pytest
from syrupy import SnapshotAssertion
from homeassistant import config_entries
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
from homeassistant.components.tasmota.const import DEFAULT_PREFIX
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
@ -175,7 +175,7 @@ TEMPERATURE_SENSOR_CONFIG = {
@pytest.mark.parametrize(
("sensor_config", "entity_ids", "messages", "states"),
("sensor_config", "entity_ids", "messages"),
[
(
DEFAULT_SENSOR_CONFIG,
@ -184,20 +184,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"DHT11":{"Temperature":20.5}}',
'{"StatusSNS":{"DHT11":{"Temperature":20.0}}}',
),
(
{
"sensor.tasmota_dht11_temperature": {
"state": "20.5",
"attributes": {
"device_class": "temperature",
"unit_of_measurement": "°C",
},
},
},
{
"sensor.tasmota_dht11_temperature": {"state": "20.0"},
},
),
),
(
DICT_SENSOR_CONFIG_1,
@ -206,22 +192,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"TX23":{"Speed":{"Act":"12.3"},"Dir": {"Card": "WSW"}}}',
'{"StatusSNS":{"TX23":{"Speed":{"Act":"23.4"},"Dir": {"Card": "ESE"}}}}',
),
(
{
"sensor.tasmota_tx23_speed_act": {
"state": "12.3",
"attributes": {
"device_class": None,
"unit_of_measurement": "km/h",
},
},
"sensor.tasmota_tx23_dir_card": {"state": "WSW"},
},
{
"sensor.tasmota_tx23_speed_act": {"state": "23.4"},
"sensor.tasmota_tx23_dir_card": {"state": "ESE"},
},
),
),
(
LIST_SENSOR_CONFIG,
@ -233,22 +203,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"ENERGY":{"TotalTariff":[1.2,3.4]}}',
'{"StatusSNS":{"ENERGY":{"TotalTariff":[5.6,7.8]}}}',
),
(
{
"sensor.tasmota_energy_totaltariff_0": {
"state": "1.2",
"attributes": {
"device_class": None,
"unit_of_measurement": None,
},
},
"sensor.tasmota_energy_totaltariff_1": {"state": "3.4"},
},
{
"sensor.tasmota_energy_totaltariff_0": {"state": "5.6"},
"sensor.tasmota_energy_totaltariff_1": {"state": "7.8"},
},
),
),
(
TEMPERATURE_SENSOR_CONFIG,
@ -257,22 +211,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"DS18B20":{"Id": "01191ED79190","Temperature": 12.3}}',
'{"StatusSNS":{"DS18B20":{"Id": "meep","Temperature": 23.4}}}',
),
(
{
"sensor.tasmota_ds18b20_temperature": {
"state": "12.3",
"attributes": {
"device_class": "temperature",
"unit_of_measurement": "°C",
},
},
"sensor.tasmota_ds18b20_id": {"state": "01191ED79190"},
},
{
"sensor.tasmota_ds18b20_temperature": {"state": "23.4"},
"sensor.tasmota_ds18b20_id": {"state": "meep"},
},
),
),
# Test simple Total sensor
(
@ -282,21 +220,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"ENERGY":{"Total":1.2,"TotalStartTime":"2018-11-23T15:33:47"}}',
'{"StatusSNS":{"ENERGY":{"Total":5.6,"TotalStartTime":"2018-11-23T16:33:47"}}}',
),
(
{
"sensor.tasmota_energy_total": {
"state": "1.2",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
},
{
"sensor.tasmota_energy_total": {"state": "5.6"},
},
),
),
# Test list Total sensors
(
@ -306,30 +229,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"ENERGY":{"Total":[1.2, 3.4],"TotalStartTime":"2018-11-23T15:33:47"}}',
'{"StatusSNS":{"ENERGY":{"Total":[5.6, 7.8],"TotalStartTime":"2018-11-23T16:33:47"}}}',
),
(
{
"sensor.tasmota_energy_total_0": {
"state": "1.2",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
"sensor.tasmota_energy_total_1": {
"state": "3.4",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
},
{
"sensor.tasmota_energy_total_0": {"state": "5.6"},
"sensor.tasmota_energy_total_1": {"state": "7.8"},
},
),
),
# Test dict Total sensors
(
@ -342,30 +241,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"ENERGY":{"Total":{"Phase1":1.2, "Phase2":3.4},"TotalStartTime":"2018-11-23T15:33:47"}}',
'{"StatusSNS":{"ENERGY":{"Total":{"Phase1":5.6, "Phase2":7.8},"TotalStartTime":"2018-11-23T15:33:47"}}}',
),
(
{
"sensor.tasmota_energy_total_phase1": {
"state": "1.2",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
"sensor.tasmota_energy_total_phase2": {
"state": "3.4",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
},
{
"sensor.tasmota_energy_total_phase1": {"state": "5.6"},
"sensor.tasmota_energy_total_phase2": {"state": "7.8"},
},
),
),
(
NUMBERED_SENSOR_CONFIG,
@ -384,39 +259,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'"Illuminance3":1.2}}}'
),
),
(
{
"sensor.tasmota_analog_temperature1": {
"state": "1.2",
"attributes": {
"device_class": "temperature",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "°C",
},
},
"sensor.tasmota_analog_temperature2": {
"state": "3.4",
"attributes": {
"device_class": "temperature",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "°C",
},
},
"sensor.tasmota_analog_illuminance3": {
"state": "5.6",
"attributes": {
"device_class": "illuminance",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "lx",
},
},
},
{
"sensor.tasmota_analog_temperature1": {"state": "7.8"},
"sensor.tasmota_analog_temperature2": {"state": "9.0"},
"sensor.tasmota_analog_illuminance3": {"state": "1.2"},
},
),
),
(
NUMBERED_SENSOR_CONFIG_2,
@ -436,48 +278,6 @@ TEMPERATURE_SENSOR_CONFIG = {
'{"Energy":1.0,"Power":1150,"Voltage":230,"Current":5}}}}'
),
),
(
{
"sensor.tasmota_analog_ctenergy1_energy": {
"state": "0.5",
"attributes": {
"device_class": "energy",
ATTR_STATE_CLASS: SensorStateClass.TOTAL,
"unit_of_measurement": "kWh",
},
},
"sensor.tasmota_analog_ctenergy1_power": {
"state": "2300",
"attributes": {
"device_class": "power",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "W",
},
},
"sensor.tasmota_analog_ctenergy1_voltage": {
"state": "230",
"attributes": {
"device_class": "voltage",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "V",
},
},
"sensor.tasmota_analog_ctenergy1_current": {
"state": "10",
"attributes": {
"device_class": "current",
ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT,
"unit_of_measurement": "A",
},
},
},
{
"sensor.tasmota_analog_ctenergy1_energy": {"state": "1.0"},
"sensor.tasmota_analog_ctenergy1_power": {"state": "1150"},
"sensor.tasmota_analog_ctenergy1_voltage": {"state": "230"},
"sensor.tasmota_analog_ctenergy1_current": {"state": "5"},
},
),
),
],
)
@ -485,11 +285,11 @@ async def test_controlling_state_via_mqtt(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mqtt_mock: MqttMockHAClient,
snapshot: SnapshotAssertion,
setup_tasmota,
sensor_config,
entity_ids,
messages,
states,
) -> None:
"""Test state update via MQTT."""
config = copy.deepcopy(DEFAULT_CONFIG)
@ -513,11 +313,13 @@ async def test_controlling_state_via_mqtt(
state = hass.states.get(entity_id)
assert state.state == "unavailable"
assert not state.attributes.get(ATTR_ASSUMED_STATE)
assert state == snapshot
entry = entity_registry.async_get(entity_id)
assert entry.disabled is False
assert entry.disabled_by is None
assert entry.entity_category is None
assert entry == snapshot
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online")
await hass.async_block_till_done()
@ -530,19 +332,13 @@ async def test_controlling_state_via_mqtt(
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/SENSOR", messages[0])
for entity_id in entity_ids:
state = hass.states.get(entity_id)
expected_state = states[0][entity_id]
assert state.state == expected_state["state"]
for attribute, expected in expected_state.get("attributes", {}).items():
assert state.attributes.get(attribute) == expected
assert state == snapshot
# Test polled state update
async_fire_mqtt_message(hass, "tasmota_49A3BC/stat/STATUS10", messages[1])
for entity_id in entity_ids:
state = hass.states.get(entity_id)
expected_state = states[1][entity_id]
assert state.state == expected_state["state"]
for attribute, expected in expected_state.get("attributes", {}).items():
assert state.attributes.get(attribute) == expected
assert state == snapshot
@pytest.mark.parametrize(