Mark Tasmota status sensors as diagnostic sensors (#57958)

* Mark Tasmota status sensors as diagnostic sensors

* Disable IP and firmware version sensors by default
This commit is contained in:
Erik Montnemery 2021-10-19 04:33:26 +02:00 committed by GitHub
parent 1a978662ec
commit 2bae113748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 3 deletions

View File

@ -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

View File

@ -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
)