Support reloading Tasmota config entries (#42097)

This commit is contained in:
Erik Montnemery authored and GitHub committed 2020-10-23 01:22:51 +02:00
1 parent 3661035397
commit de35d58fd4
15 files changed
+179 -75

No files matched your search

+1
View File
@@ -72,6 +72,7 @@ async def setup_tasmota_helper(hass):
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert "tasmota" in hass.config.components
+13 -12
View File
@@ -27,40 +27,41 @@ async def test_valid_discovery_message(hass, mqtt_mock, caplog):
config = copy.deepcopy(DEFAULT_CONFIG)
with patch(
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
) as mock_tasmota_has_entities:
"homeassistant.components.tasmota.discovery.tasmota_get_device_config",
return_value={},
) as mock_tasmota_get_device_config:
await setup_tasmota_helper(hass)
async_fire_mqtt_message(
hass, f"{DEFAULT_PREFIX}/00000049A3BC/config", json.dumps(config)
)
await hass.async_block_till_done()
assert mock_tasmota_has_entities.called
assert mock_tasmota_get_device_config.called
async def test_invalid_topic(hass, mqtt_mock):
"""Test receiving discovery message on wrong topic."""
with patch(
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
) as mock_tasmota_has_entities:
"homeassistant.components.tasmota.discovery.tasmota_get_device_config"
) as mock_tasmota_get_device_config:
await setup_tasmota_helper(hass)
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/123456/configuration", "{}")
await hass.async_block_till_done()
assert not mock_tasmota_has_entities.called
assert not mock_tasmota_get_device_config.called
async def test_invalid_message(hass, mqtt_mock, caplog):
"""Test receiving an invalid message."""
with patch(
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
) as mock_tasmota_has_entities:
"homeassistant.components.tasmota.discovery.tasmota_get_device_config"
) as mock_tasmota_get_device_config:
await setup_tasmota_helper(hass)
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/123456/config", "asd")
await hass.async_block_till_done()
assert "Invalid discovery message" in caplog.text
assert not mock_tasmota_has_entities.called
assert not mock_tasmota_get_device_config.called
async def test_invalid_mac(hass, mqtt_mock, caplog):
@@ -68,8 +69,8 @@ async def test_invalid_mac(hass, mqtt_mock, caplog):
config = copy.deepcopy(DEFAULT_CONFIG)
with patch(
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
) as mock_tasmota_has_entities:
"homeassistant.components.tasmota.discovery.tasmota_get_device_config"
) as mock_tasmota_get_device_config:
await setup_tasmota_helper(hass)
async_fire_mqtt_message(
@@ -77,7 +78,7 @@ async def test_invalid_mac(hass, mqtt_mock, caplog):
)
await hass.async_block_till_done()
assert "MAC mismatch" in caplog.text
assert not mock_tasmota_has_entities.called
assert not mock_tasmota_get_device_config.called
async def test_correct_config_discovery(
+62 -1
View File
@@ -1,5 +1,6 @@
"""The tests for the Tasmota sensor platform."""
import copy
from datetime import timedelta
import json
from hatasmota.utils import (
@@ -9,9 +10,11 @@ from hatasmota.utils import (
)
import pytest
from homeassistant import config_entries
from homeassistant.components import sensor
from homeassistant.components.tasmota.const import DEFAULT_PREFIX
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNKNOWN
from homeassistant.util import dt
from .test_common import (
DEFAULT_CONFIG,
@@ -27,7 +30,7 @@ from .test_common import (
)
from tests.async_mock import patch
from tests.common import async_fire_mqtt_message
from tests.common import async_fire_mqtt_message, async_fire_time_changed
DEFAULT_SENSOR_CONFIG = {
"sn": {
@@ -377,6 +380,64 @@ 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])
async def test_enable_status_sensor(hass, mqtt_mock, setup_tasmota):
"""Test enabling status sensor."""
entity_reg = await hass.helpers.entity_registry.async_get_registry()
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("sensor.tasmota_status")
assert state is None
entry = entity_reg.async_get("sensor.tasmota_status")
assert entry.disabled
assert entry.disabled_by == "integration"
# Enable the status sensor
updated_entry = entity_reg.async_update_entity(
"sensor.tasmota_status", disabled_by=None
)
assert updated_entry != entry
assert updated_entry.disabled is False
await hass.async_block_till_done()
async_fire_time_changed(
hass,
dt.utcnow()
+ timedelta(
seconds=config_entries.EntityRegistryDisabledHandler.RELOAD_AFTER_UPDATE_DELAY
+ 1
),
)
await hass.async_block_till_done()
# Fake re-send of retained discovery message
async_fire_mqtt_message(
hass,
f"{DEFAULT_PREFIX}/{mac}/config",
json.dumps(config),
)
await hass.async_block_till_done()
state = hass.states.get("sensor.tasmota_status")
assert state.state == "unavailable"
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online")
state = hass.states.get("sensor.tasmota_status")
assert state.state == STATE_UNKNOWN
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async def test_availability_when_connection_lost(
hass, mqtt_client_mock, mqtt_mock, setup_tasmota
):