Add Tasmota integration (#39624)
* Add Tasmota integration * Refactor * Add tests, small improvements * isort * Attempt to fix tests failing with Python 3.8 * Revert "Attempt to fix tests failing with Python 3.8" This reverts commit 11454f8a00136f068ea27204183fa3e62f3cd263. * Fix tests failing with Python 3.8 * Cleanup tests * Address review comments * Address review comments * Address review comments * Use MAC address for device identification * Bump hatasmota * Bump hatasmota * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix indentation * Remove preparation for device remove WS API * Address review comments * Remove useless try-except * Tweak * Improve tests * Tweak * Address review comments * Correct test * Update manifest.json Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
3cf6535011
commit
06e9489fc7
20 files changed
+1569
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for Tasmota component."""
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Test fixtures for Tasmota component."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.tasmota.const import (
|
||||
CONF_DISCOVERY_PREFIX,
|
||||
DEFAULT_PREFIX,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry, mock_device_registry, mock_registry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def device_reg(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_device_registry(hass)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def entity_reg(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_registry(hass)
|
||||
|
||||
|
||||
async def setup_tasmota_helper(hass):
|
||||
"""Set up Tasmota."""
|
||||
hass.config.components.add("tasmota")
|
||||
|
||||
entry = MockConfigEntry(
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
||||
data={CONF_DISCOVERY_PREFIX: DEFAULT_PREFIX},
|
||||
domain=DOMAIN,
|
||||
title="Tasmota",
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
||||
assert "tasmota" in hass.config.components
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def setup_tasmota(hass):
|
||||
"""Set up Tasmota."""
|
||||
await setup_tasmota_helper(hass)
|
||||
@@ -0,0 +1,328 @@
|
||||
"""Common test objects."""
|
||||
import copy
|
||||
import json
|
||||
|
||||
from hatasmota.const import (
|
||||
CONF_MAC,
|
||||
CONF_OFFLINE,
|
||||
CONF_ONLINE,
|
||||
CONF_PREFIX,
|
||||
PREFIX_CMND,
|
||||
PREFIX_TELE,
|
||||
)
|
||||
from hatasmota.utils import (
|
||||
get_state_offline,
|
||||
get_state_online,
|
||||
get_topic_tele_state,
|
||||
get_topic_tele_will,
|
||||
)
|
||||
|
||||
from homeassistant.components.tasmota.const import DEFAULT_PREFIX
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
|
||||
from tests.async_mock import ANY
|
||||
from tests.common import async_fire_mqtt_message
|
||||
|
||||
|
||||
async def help_test_availability_when_connection_lost(
|
||||
hass, mqtt_client_mock, mqtt_mock, domain, config
|
||||
):
|
||||
"""Test availability after MQTT disconnection."""
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
get_topic_tele_will(config),
|
||||
get_state_online(config),
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
mqtt_mock.connected = False
|
||||
await hass.async_add_executor_job(mqtt_client_mock.on_disconnect, None, None, 0)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
mqtt_mock.connected = True
|
||||
await hass.async_add_executor_job(mqtt_client_mock.on_connect, None, None, None, 0)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def help_test_availability(
|
||||
hass,
|
||||
mqtt_mock,
|
||||
domain,
|
||||
config,
|
||||
):
|
||||
"""Test availability.
|
||||
|
||||
This is a test helper for the TasmotaAvailability mixin.
|
||||
"""
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
get_topic_tele_will(config),
|
||||
get_state_online(config),
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
get_topic_tele_will(config),
|
||||
get_state_offline(config),
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def help_test_availability_discovery_update(
|
||||
hass,
|
||||
mqtt_mock,
|
||||
domain,
|
||||
config,
|
||||
):
|
||||
"""Test update of discovered TasmotaAvailability.
|
||||
|
||||
This is a test helper for the TasmotaAvailability mixin.
|
||||
"""
|
||||
# customize availability topic
|
||||
config1 = copy.deepcopy(config)
|
||||
config1[CONF_PREFIX][PREFIX_TELE] = "tele1"
|
||||
config1[CONF_OFFLINE] = "offline1"
|
||||
config1[CONF_ONLINE] = "online1"
|
||||
config2 = copy.deepcopy(config)
|
||||
config2[CONF_PREFIX][PREFIX_TELE] = "tele2"
|
||||
config2[CONF_OFFLINE] = "offline2"
|
||||
config2[CONF_ONLINE] = "online2"
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
|
||||
availability_topic1 = get_topic_tele_will(config1)
|
||||
availability_topic2 = get_topic_tele_will(config2)
|
||||
assert availability_topic1 != availability_topic2
|
||||
offline1 = get_state_offline(config1)
|
||||
offline2 = get_state_offline(config2)
|
||||
assert offline1 != offline2
|
||||
online1 = get_state_online(config1)
|
||||
online2 = get_state_online(config2)
|
||||
assert online1 != online2
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config1[CONF_MAC]}/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, availability_topic1, online1)
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, availability_topic1, offline1)
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
# Change availability settings
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config2[CONF_MAC]}/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify we are no longer subscribing to the old topic or payload
|
||||
async_fire_mqtt_message(hass, availability_topic1, online1)
|
||||
async_fire_mqtt_message(hass, availability_topic1, online2)
|
||||
async_fire_mqtt_message(hass, availability_topic2, online1)
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
# Verify we are subscribing to the new topic
|
||||
async_fire_mqtt_message(hass, availability_topic2, online2)
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def help_test_discovery_removal(
|
||||
hass, mqtt_mock, caplog, domain, config1, config2
|
||||
):
|
||||
"""Test removal of discovered entity."""
|
||||
device_reg = await hass.helpers.device_registry.async_get_registry()
|
||||
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
assert config1[CONF_MAC] == config2[CONF_MAC]
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config1[CONF_MAC]}/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device and entity registry entries are created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", config1[CONF_MAC])})
|
||||
assert device_entry is not None
|
||||
entity_entry = entity_reg.async_get(f"{domain}.test")
|
||||
assert entity_entry is not None
|
||||
|
||||
# Verify state is added
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state is not None
|
||||
assert state.name == "Test"
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config2[CONF_MAC]}/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify entity registry entries are cleared
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", config2[CONF_MAC])})
|
||||
assert device_entry is not None
|
||||
entity_entry = entity_reg.async_get(f"{domain}.test")
|
||||
assert entity_entry is None
|
||||
|
||||
# Verify state is removed
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state is None
|
||||
|
||||
|
||||
async def help_test_discovery_update_unchanged(
|
||||
hass, mqtt_mock, caplog, domain, config, discovery_update
|
||||
):
|
||||
"""Test update of discovered component without changes.
|
||||
|
||||
This is a test helper for the MqttDiscoveryUpdate mixin.
|
||||
"""
|
||||
config1 = copy.deepcopy(config)
|
||||
config2 = copy.deepcopy(config)
|
||||
config2[CONF_PREFIX][PREFIX_CMND] = "cmnd2"
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state is not None
|
||||
assert state.name == "Test"
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not discovery_update.called
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert discovery_update.called
|
||||
|
||||
|
||||
async def help_test_discovery_device_remove(hass, mqtt_mock, domain, config):
|
||||
"""Test domain entity is removed when device is removed."""
|
||||
device_reg = await hass.helpers.device_registry.async_get_registry()
|
||||
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
config = copy.deepcopy(config)
|
||||
unique_id = f"{config[CONF_MAC]}_{domain}_0"
|
||||
|
||||
data = json.dumps(config)
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device = device_reg.async_get_device(set(), {("mac", config[CONF_MAC])})
|
||||
assert device is not None
|
||||
assert entity_reg.async_get_entity_id(domain, "tasmota", unique_id)
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device = device_reg.async_get_device(set(), {("mac", config[CONF_MAC])})
|
||||
assert device is None
|
||||
assert not entity_reg.async_get_entity_id(domain, "tasmota", unique_id)
|
||||
|
||||
|
||||
async def help_test_entity_id_update_subscriptions(
|
||||
hass, mqtt_mock, domain, config, topics=None
|
||||
):
|
||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
config = copy.deepcopy(config)
|
||||
data = json.dumps(config)
|
||||
|
||||
mqtt_mock.async_subscribe.reset_mock()
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
topics = [get_topic_tele_state(config), get_topic_tele_will(config)]
|
||||
assert len(topics) > 0
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state is not None
|
||||
assert mqtt_mock.async_subscribe.call_count == len(topics)
|
||||
for topic in topics:
|
||||
mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY)
|
||||
mqtt_mock.async_subscribe.reset_mock()
|
||||
|
||||
entity_reg.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state is None
|
||||
|
||||
state = hass.states.get(f"{domain}.milk")
|
||||
assert state is not None
|
||||
for topic in topics:
|
||||
mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY)
|
||||
|
||||
|
||||
async def help_test_entity_id_update_discovery_update(hass, mqtt_mock, domain, config):
|
||||
"""Test MQTT discovery update after entity_id is updated."""
|
||||
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
config = copy.deepcopy(config)
|
||||
data = json.dumps(config)
|
||||
|
||||
topic = get_topic_tele_will(config)
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_mqtt_message(hass, topic, get_state_online(config))
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, topic, get_state_offline(config))
|
||||
state = hass.states.get(f"{domain}.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
entity_reg.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config[CONF_PREFIX][PREFIX_TELE] != "tele2"
|
||||
config[CONF_PREFIX][PREFIX_TELE] = "tele2"
|
||||
data = json.dumps(config)
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(domain)) == 1
|
||||
|
||||
topic = get_topic_tele_will(config)
|
||||
async_fire_mqtt_message(hass, topic, get_state_online(config))
|
||||
state = hass.states.get(f"{domain}.milk")
|
||||
assert state.state != STATE_UNAVAILABLE
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Test config flow."""
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_user_setup(hass, mqtt_mock):
|
||||
"""Test we can finish a config flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["result"].data == {
|
||||
"discovery_prefix": "tasmota/discovery",
|
||||
}
|
||||
|
||||
|
||||
async def test_user_setup_advanced(hass, mqtt_mock):
|
||||
"""Test we can finish a config flow."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user", "show_advanced_options": True}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"discovery_prefix": "test_tasmota/discovery"}
|
||||
)
|
||||
|
||||
assert result["type"] == "create_entry"
|
||||
assert result["result"].data == {
|
||||
"discovery_prefix": "test_tasmota/discovery",
|
||||
}
|
||||
|
||||
|
||||
async def test_user_setup_invalid_topic_prefix(hass, mqtt_mock):
|
||||
"""Test if connection cannot be made."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user", "show_advanced_options": True}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {"discovery_prefix": "tasmota/config/#"}
|
||||
)
|
||||
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"]["base"] == "invalid_discovery_topic"
|
||||
|
||||
|
||||
async def test_user_single_instance(hass, mqtt_mock):
|
||||
"""Test we only allow a single config flow."""
|
||||
MockConfigEntry(domain="tasmota").add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"tasmota", context={"source": "user"}
|
||||
)
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
@@ -0,0 +1,372 @@
|
||||
"""The tests for the MQTT discovery."""
|
||||
import copy
|
||||
import json
|
||||
|
||||
from hatasmota.const import CONF_ONLINE
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.tasmota.const import DEFAULT_PREFIX
|
||||
from homeassistant.components.tasmota.discovery import ALREADY_DISCOVERED
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import async_fire_mqtt_message
|
||||
from tests.components.tasmota.conftest import setup_tasmota_helper
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"dn": "My Device",
|
||||
"fn": ["Beer", "Milk", "Three", "Four", "Five"],
|
||||
"hn": "tasmota_49A3BC",
|
||||
"mac": "00000049A3BC",
|
||||
"md": "Sonoff 123",
|
||||
"ofl": "offline",
|
||||
CONF_ONLINE: "online",
|
||||
"state": ["OFF", "ON", "TOGGLE", "HOLD"],
|
||||
"sw": "2.3.3.4",
|
||||
"t": "tasmota_49A3BC",
|
||||
"t_f": "%topic%/%prefix%/",
|
||||
"t_p": ["cmnd", "stat", "tele"],
|
||||
"li": [0, 0, 0, 0, 0, 0, 0, 0],
|
||||
"rl": [0, 0, 0, 0, 0, 0, 0, 0],
|
||||
"se": [],
|
||||
"ver": 1,
|
||||
}
|
||||
|
||||
|
||||
async def test_subscribing_config_topic(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test setting up discovery."""
|
||||
discovery_topic = DEFAULT_PREFIX
|
||||
|
||||
assert mqtt_mock.async_subscribe.called
|
||||
call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
|
||||
assert call_args[0] == discovery_topic + "/#"
|
||||
assert call_args[2] == 0
|
||||
|
||||
|
||||
async def test_valid_discovery_message(hass, mqtt_mock, caplog):
|
||||
"""Test discovery callback called."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
|
||||
) as mock_tasmota_has_entities:
|
||||
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
|
||||
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
|
||||
async def test_invalid_mac(hass, mqtt_mock, caplog):
|
||||
"""Test topic is not matching device MAC."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.tasmota.discovery.tasmota_has_entities_with_platform"
|
||||
) as mock_tasmota_has_entities:
|
||||
await setup_tasmota_helper(hass)
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass, f"{DEFAULT_PREFIX}/00000049A3BA/config", json.dumps(config)
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert "MAC mismatch" in caplog.text
|
||||
assert not mock_tasmota_has_entities.called
|
||||
|
||||
|
||||
async def test_correct_config_discovery(
|
||||
hass, mqtt_mock, caplog, device_reg, entity_reg, setup_tasmota
|
||||
):
|
||||
"""Test receiving valid discovery message."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config["rl"][0] = 1
|
||||
mac = config["mac"]
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device and registry entries are created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
entity_entry = entity_reg.async_get("switch.beer")
|
||||
assert entity_entry is not None
|
||||
|
||||
state = hass.states.get("switch.beer")
|
||||
assert state is not None
|
||||
assert state.name == "Beer"
|
||||
|
||||
assert (mac, "switch", 0) in hass.data[ALREADY_DISCOVERED]
|
||||
|
||||
|
||||
async def test_device_discover(
|
||||
hass, mqtt_mock, caplog, device_reg, entity_reg, setup_tasmota
|
||||
):
|
||||
"""Test setting up a device."""
|
||||
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()
|
||||
|
||||
# Verify device and registry entries are created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
assert device_entry.manufacturer == "Tasmota"
|
||||
assert device_entry.model == config["md"]
|
||||
assert device_entry.name == config["dn"]
|
||||
assert device_entry.sw_version == config["sw"]
|
||||
|
||||
|
||||
async def test_device_update(
|
||||
hass, mqtt_mock, caplog, device_reg, entity_reg, setup_tasmota
|
||||
):
|
||||
"""Test updating a device."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config["md"] = "Model 1"
|
||||
config["dn"] = "Name 1"
|
||||
config["sw"] = "v1.2.3.4"
|
||||
mac = config["mac"]
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
|
||||
# Update device parameters
|
||||
config["md"] = "Another model"
|
||||
config["dn"] = "Another name"
|
||||
config["sw"] = "v6.6.6"
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is updated
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
assert device_entry.model == "Another model"
|
||||
assert device_entry.name == "Another name"
|
||||
assert device_entry.sw_version == "v6.6.6"
|
||||
|
||||
|
||||
@pytest.mark.no_fail_on_log_exception
|
||||
async def test_discovery_broken(hass, mqtt_mock, caplog, device_reg, setup_tasmota):
|
||||
"""Test handling of exception when creating discovered device."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
mac = config["mac"]
|
||||
data = json.dumps(config)
|
||||
|
||||
# Trigger an exception when the entity is added
|
||||
with patch(
|
||||
"hatasmota.discovery.get_device_config_helper",
|
||||
return_value=object(),
|
||||
):
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{mac}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is not created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is None
|
||||
assert (
|
||||
"Exception in async_discover_device when dispatching 'tasmota_discovery_device'"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{mac}/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
|
||||
|
||||
async def test_device_remove(
|
||||
hass, mqtt_mock, caplog, device_reg, entity_reg, setup_tasmota
|
||||
):
|
||||
"""Test removing a discovered device."""
|
||||
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()
|
||||
|
||||
# Verify device entry is created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
"",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is removed
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_device_remove_stale(hass, mqtt_mock, caplog, device_reg, setup_tasmota):
|
||||
"""Test removing a stale (undiscovered) device does not throw."""
|
||||
mac = "00000049A3BC"
|
||||
|
||||
config_entry = hass.config_entries.async_entries("tasmota")[0]
|
||||
|
||||
# Create a device
|
||||
device_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={("mac", mac)},
|
||||
)
|
||||
|
||||
# Verify device entry was created
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
|
||||
# Remove the device
|
||||
device_reg.async_remove_device(device_entry.id)
|
||||
|
||||
# Verify device entry is removed
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_device_rediscover(
|
||||
hass, mqtt_mock, caplog, device_reg, entity_reg, setup_tasmota
|
||||
):
|
||||
"""Test removing a device."""
|
||||
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()
|
||||
|
||||
# Verify device entry is created
|
||||
device_entry1 = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry1 is not None
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
"",
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is removed
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is None
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device entry is created, and id is reused
|
||||
device_entry = device_reg.async_get_device(set(), {("mac", mac)})
|
||||
assert device_entry is not None
|
||||
assert device_entry1.id == device_entry.id
|
||||
|
||||
|
||||
async def test_entity_duplicate_discovery(hass, mqtt_mock, caplog, setup_tasmota):
|
||||
"""Test entities are not duplicated."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config["rl"][0] = 1
|
||||
mac = config["mac"]
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.beer")
|
||||
state_duplicate = hass.states.get("binary_sensor.beer1")
|
||||
|
||||
assert state is not None
|
||||
assert state.name == "Beer"
|
||||
assert state_duplicate is None
|
||||
assert (
|
||||
f"Entity already added, sending update: switch ('{mac}', 'switch', 0)"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_duplicate_removal(hass, mqtt_mock, caplog, setup_tasmota):
|
||||
"""Test removing entity twice."""
|
||||
config = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config["rl"][0] = 1
|
||||
mac = config["mac"]
|
||||
|
||||
async_fire_mqtt_message(
|
||||
hass,
|
||||
f"{DEFAULT_PREFIX}/{mac}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
config["rl"][0] = 0
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{mac}/config", json.dumps(config))
|
||||
await hass.async_block_till_done()
|
||||
assert f"Removing entity: switch ('{mac}', 'switch', 0)" in caplog.text
|
||||
|
||||
caplog.clear()
|
||||
async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{mac}/config", json.dumps(config))
|
||||
await hass.async_block_till_done()
|
||||
assert f"Removing entity: switch ('{mac}', 'switch', 0)" not in caplog.text
|
||||
@@ -0,0 +1,177 @@
|
||||
"""The tests for the MQTT switch platform."""
|
||||
import copy
|
||||
import json
|
||||
|
||||
from hatasmota.const import CONF_ONLINE
|
||||
|
||||
from homeassistant.components import switch
|
||||
from homeassistant.components.tasmota.const import DEFAULT_PREFIX
|
||||
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON
|
||||
|
||||
from .test_common import (
|
||||
help_test_availability,
|
||||
help_test_availability_discovery_update,
|
||||
help_test_availability_when_connection_lost,
|
||||
help_test_discovery_device_remove,
|
||||
help_test_discovery_removal,
|
||||
help_test_discovery_update_unchanged,
|
||||
help_test_entity_id_update_discovery_update,
|
||||
help_test_entity_id_update_subscriptions,
|
||||
)
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import async_fire_mqtt_message
|
||||
from tests.components.switch import common
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"dn": "My Device",
|
||||
"fn": ["Test", "Beer", "Milk", "Four", "Five"],
|
||||
"hn": "tasmota_49A3BC",
|
||||
"mac": "00000049A3BC",
|
||||
"md": "Sonoff 123",
|
||||
"ofl": "offline",
|
||||
CONF_ONLINE: "online",
|
||||
"state": ["OFF", "ON", "TOGGLE", "HOLD"],
|
||||
"sw": "2.3.3.4",
|
||||
"t": "tasmota_49A3BC",
|
||||
"t_f": "%topic%/%prefix%/",
|
||||
"t_p": ["cmnd", "stat", "tele"],
|
||||
"li": [0, 0, 0, 0, 0, 0, 0, 0],
|
||||
"rl": [1, 0, 0, 0, 0, 0, 0, 0],
|
||||
"se": [],
|
||||
"ver": 1,
|
||||
}
|
||||
|
||||
|
||||
async def test_controlling_state_via_mqtt(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test state update via MQTT."""
|
||||
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()
|
||||
|
||||
state = hass.states.get("switch.test")
|
||||
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("switch.test")
|
||||
assert state.state == STATE_OFF
|
||||
assert not state.attributes.get(ATTR_ASSUMED_STATE)
|
||||
|
||||
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON"}')
|
||||
|
||||
state = hass.states.get("switch.test")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"OFF"}')
|
||||
|
||||
state = hass.states.get("switch.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_sending_mqtt_commands(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test the sending MQTT commands."""
|
||||
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()
|
||||
|
||||
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "online")
|
||||
state = hass.states.get("switch.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
# Turn the switch on and verify MQTT message is sent
|
||||
await common.async_turn_on(hass, "switch.test")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"tasmota_49A3BC/cmnd/POWER1", "ON", 0, False
|
||||
)
|
||||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
# Tasmota is not optimistic, the state should still be off
|
||||
state = hass.states.get("switch.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
# Turn the switch off and verify MQTT message is sent
|
||||
await common.async_turn_off(hass, "switch.test")
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"tasmota_49A3BC/cmnd/POWER1", "OFF", 0, False
|
||||
)
|
||||
|
||||
state = hass.states.get("switch.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_availability_when_connection_lost(
|
||||
hass, mqtt_client_mock, mqtt_mock, setup_tasmota
|
||||
):
|
||||
"""Test availability after MQTT disconnection."""
|
||||
await help_test_availability_when_connection_lost(
|
||||
hass, mqtt_client_mock, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_availability(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test availability."""
|
||||
await help_test_availability(hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG)
|
||||
|
||||
|
||||
async def test_availability_discovery_update(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test availability discovery update."""
|
||||
await help_test_availability_discovery_update(
|
||||
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_removal_switch(hass, mqtt_mock, caplog, setup_tasmota):
|
||||
"""Test removal of discovered switch."""
|
||||
config1 = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config2 = copy.deepcopy(DEFAULT_CONFIG)
|
||||
config2["rl"][0] = 0
|
||||
|
||||
await help_test_discovery_removal(
|
||||
hass, mqtt_mock, caplog, switch.DOMAIN, config1, config2
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_update_unchanged_switch(
|
||||
hass, mqtt_mock, caplog, setup_tasmota
|
||||
):
|
||||
"""Test update of discovered switch."""
|
||||
with patch(
|
||||
"homeassistant.components.tasmota.switch.TasmotaSwitch.discovery_update"
|
||||
) as discovery_update:
|
||||
await help_test_discovery_update_unchanged(
|
||||
hass, mqtt_mock, caplog, switch.DOMAIN, DEFAULT_CONFIG, discovery_update
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_device_remove(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test device registry remove."""
|
||||
await help_test_discovery_device_remove(
|
||||
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_id_update_subscriptions(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
||||
await help_test_entity_id_update_subscriptions(
|
||||
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_id_update_discovery_update(hass, mqtt_mock, setup_tasmota):
|
||||
"""Test MQTT discovery update when entity_id is updated."""
|
||||
await help_test_entity_id_update_discovery_update(
|
||||
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG
|
||||
)
|
||||
Reference in new issue
Block a user