Move manual configuration of MQTT fan and light to the integration key (#71676)

* Processing yaml config through entry setup

* Setup all platforms

* Update homeassistant/components/mqtt/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* adjust mock_mqtt - reference config from cache

* Fix test config entry override

* Add tests yaml setup

* additional tests

* Introduce PLATFORM_SCHEMA_MODERN

* recover temporary MQTT_BASE_PLATFORM_SCHEMA

* Allow extra key in light base schema, restore test

* Fix test for exception on platform key

* One deprecation message per platform

* Remove deprecation checks from modern schema

* Update homeassistant/components/mqtt/fan.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/fan.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/schema_json.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/light/schema_template.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/mqtt/mixins.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* rename validate_modern_schema

* Do not fail platform if a single config is broken

* Update homeassistant/components/mqtt/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Fix tests on asserting log

* Update log. Make helper transparant, remove patch

* Perform parallel processing

* Update tests/components/mqtt/test_init.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/mqtt/mixins.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* black

* Fix tests and add #new_format anchor

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Jan Bouwhuis 2022-05-19 15:04:53 +02:00 committed by GitHub
parent 9d377aabdb
commit ed1c2ea2b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 339 additions and 28 deletions

View file

@ -1,5 +1,6 @@
"""The tests for the MQTT component."""
import asyncio
import copy
from datetime import datetime, timedelta
from functools import partial
import json
@ -30,6 +31,8 @@ from homeassistant.helpers.entity import Entity
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from .test_common import help_test_setup_manual_entity_from_yaml
from tests.common import (
MockConfigEntry,
async_fire_mqtt_message,
@ -1279,6 +1282,51 @@ async def test_setup_override_configuration(hass, caplog, tmp_path):
assert calls_username_password_set[0][1] == "somepassword"
async def test_setup_manual_mqtt_with_platform_key(hass, caplog, tmp_path):
"""Test set up a manual MQTT item with a platform key."""
config = {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
await help_test_setup_manual_entity_from_yaml(
hass,
caplog,
tmp_path,
"light",
config,
)
assert (
"Invalid config for [light]: [platform] is an invalid option for [light]. "
"Check: light->platform. (See ?, line ?)" in caplog.text
)
async def test_setup_manual_mqtt_with_invalid_config(hass, caplog, tmp_path):
"""Test set up a manual MQTT item with an invalid config."""
config = {"name": "test"}
await help_test_setup_manual_entity_from_yaml(
hass,
caplog,
tmp_path,
"light",
config,
)
assert (
"Invalid config for [light]: required key not provided @ data['command_topic']."
" Got None. (See ?, line ?)" in caplog.text
)
async def test_setup_manual_mqtt_empty_platform(hass, caplog, tmp_path):
"""Test set up a manual MQTT platform without items."""
config = None
await help_test_setup_manual_entity_from_yaml(
hass,
caplog,
tmp_path,
"light",
config,
)
assert "voluptuous.error.MultipleInvalid" not in caplog.text
async def test_setup_mqtt_client_protocol(hass):
"""Test MQTT client protocol setup."""
entry = MockConfigEntry(
@ -1628,7 +1676,8 @@ async def test_setup_entry_with_config_override(hass, device_reg, mqtt_client_mo
# User sets up a config entry
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
with patch("homeassistant.components.mqtt.PLATFORMS", []):
assert await hass.config_entries.async_setup(entry.entry_id)
# Discover a device to verify the entry was setup correctly
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
@ -2413,3 +2462,23 @@ async def test_subscribe_connection_status(hass, mqtt_mock, mqtt_client_mock):
assert len(mqtt_connected_calls) == 2
assert mqtt_connected_calls[0] is True
assert mqtt_connected_calls[1] is False
async def test_one_deprecation_warning_per_platform(hass, mqtt_mock, caplog):
"""Test a deprecation warning is is logged once per platform."""
platform = "light"
config = {"platform": "mqtt", "command_topic": "test-topic"}
config1 = copy.deepcopy(config)
config1["name"] = "test1"
config2 = copy.deepcopy(config)
config2["name"] = "test2"
await async_setup_component(hass, platform, {platform: [config1, config2]})
await hass.async_block_till_done()
count = 0
for record in caplog.records:
if record.levelname == "WARNING" and (
f"Manually configured MQTT {platform}(s) found under platform key '{platform}'"
in record.message
):
count += 1
assert count == 1