From 2bae1137480c756b755a0f01122513c02a0e4486 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 19 Oct 2021 04:33:26 +0200 Subject: [PATCH] Mark Tasmota status sensors as diagnostic sensors (#57958) * Mark Tasmota status sensors as diagnostic sensors * Disable IP and firmware version sensors by default --- homeassistant/components/tasmota/sensor.py | 17 +++++++- tests/components/tasmota/test_sensor.py | 47 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/tasmota/sensor.py b/homeassistant/components/tasmota/sensor.py index 39ee97d1648..348ff741c9b 100644 --- a/homeassistant/components/tasmota/sensor.py +++ b/homeassistant/components/tasmota/sensor.py @@ -32,6 +32,7 @@ from homeassistant.const import ( ELECTRIC_CURRENT_AMPERE, ELECTRIC_POTENTIAL_VOLT, ENERGY_KILO_WATT_HOUR, + ENTITY_CATEGORY_DIAGNOSTIC, FREQUENCY_HERTZ, LENGTH_CENTIMETERS, LIGHT_LUX, @@ -229,11 +230,23 @@ class TasmotaSensor(TasmotaAvailability, TasmotaDiscoveryUpdate, SensorEntity): ) return class_or_icon.get(STATE_CLASS) + @property + def entity_category(self) -> str | None: + """Return the category of the entity, if any.""" + if self._tasmota_entity.quantity in status_sensor.SENSORS: + return ENTITY_CATEGORY_DIAGNOSTIC + return None + @property def entity_registry_enabled_default(self) -> bool: """Return if the entity should be enabled when first added to the entity registry.""" - # Hide status sensors to not overwhelm users - if self._tasmota_entity.quantity in status_sensor.SENSORS: + # Hide fast changing status sensors + if self._tasmota_entity.quantity in ( + hc.SENSOR_STATUS_IP, + hc.SENSOR_STATUS_RSSI, + hc.SENSOR_STATUS_SIGNAL, + hc.SENSOR_STATUS_VERSION, + ): return False return True diff --git a/tests/components/tasmota/test_sensor.py b/tests/components/tasmota/test_sensor.py index 26699763a6c..c6f27f0193c 100644 --- a/tests/components/tasmota/test_sensor.py +++ b/tests/components/tasmota/test_sensor.py @@ -148,6 +148,12 @@ async def test_controlling_state_via_mqtt(hass, mqtt_mock, setup_tasmota): assert state.state == "unavailable" assert not state.attributes.get(ATTR_ASSUMED_STATE) + entity_reg = er.async_get(hass) + entry = entity_reg.async_get("sensor.tasmota_dht11_temperature") + assert entry.disabled is False + assert entry.disabled_by is None + assert entry.entity_category is None + async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online") state = hass.states.get("sensor.tasmota_dht11_temperature") assert state.state == STATE_UNKNOWN @@ -769,6 +775,45 @@ async def test_indexed_sensor_attributes(hass, mqtt_mock, setup_tasmota): assert state.attributes.get("unit_of_measurement") == "ppm" +@pytest.mark.parametrize("status_sensor_disabled", [False]) +@pytest.mark.parametrize( + "sensor_name, disabled, disabled_by", + [ + ("tasmota_firmware_version", True, er.DISABLED_INTEGRATION), + ("tasmota_ip", True, er.DISABLED_INTEGRATION), + ("tasmota_last_restart_time", False, None), + ("tasmota_mqtt_connect_count", False, None), + ("tasmota_rssi", True, er.DISABLED_INTEGRATION), + ("tasmota_signal", True, er.DISABLED_INTEGRATION), + ("tasmota_ssid", False, None), + ("tasmota_wifi_connect_count", False, None), + ], +) +async def test_diagnostic_sensors( + hass, mqtt_mock, setup_tasmota, sensor_name, disabled, disabled_by +): + """Test properties of diagnostic sensors.""" + entity_reg = er.async_get(hass) + + config = copy.deepcopy(DEFAULT_CONFIG) + mac = config["mac"] + + async_fire_mqtt_message( + hass, + f"{DEFAULT_PREFIX}/{mac}/config", + json.dumps(config), + ) + await hass.async_block_till_done() + await hass.async_block_till_done() + + state = hass.states.get(f"sensor.{sensor_name}") + assert bool(state) != disabled + entry = entity_reg.async_get(f"sensor.{sensor_name}") + assert entry.disabled == disabled + assert entry.disabled_by == disabled_by + assert entry.entity_category == "diagnostic" + + @pytest.mark.parametrize("status_sensor_disabled", [False]) async def test_enable_status_sensor(hass, mqtt_mock, setup_tasmota): """Test enabling status sensor.""" @@ -791,7 +836,7 @@ async def test_enable_status_sensor(hass, mqtt_mock, setup_tasmota): assert entry.disabled assert entry.disabled_by == er.DISABLED_INTEGRATION - # Enable the status sensor + # Enable the signal level status sensor updated_entry = entity_reg.async_update_entity( "sensor.tasmota_signal", disabled_by=None )