Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to reload mqtt from yaml #39531

Merged
merged 1 commit into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions homeassistant/components/mqtt/__init__.py
Expand Up @@ -136,6 +136,20 @@
DISCOVERY_COOLDOWN = 2
TIMEOUT_ACK = 1

PLATFORMS = [
"alarm_control_panel",
"binary_sensor",
"camera",
"climate",
"cover",
"fan",
"light",
"lock",
"sensor",
"switch",
"vacuum",
]


def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType:
"""Validate that a device info entry has at least one identifying value."""
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/alarm_control_panel.py
Expand Up @@ -31,6 +31,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
Expand All @@ -39,6 +40,8 @@
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -100,6 +103,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT alarm control panel through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/binary_sensor.py
Expand Up @@ -24,13 +24,16 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.helpers.event as evt
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util import dt as dt_util

from . import (
ATTR_DISCOVERY_HASH,
CONF_QOS,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -72,6 +75,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT binary sensor through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/camera.py
Expand Up @@ -9,11 +9,14 @@
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
ATTR_DISCOVERY_HASH,
CONF_QOS,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -46,6 +49,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT camera through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/climate.py
Expand Up @@ -48,13 +48,16 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
ATTR_DISCOVERY_HASH,
CONF_QOS,
CONF_RETAIN,
DOMAIN,
MQTT_BASE_PLATFORM_SCHEMA,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -239,6 +242,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT climate device through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(hass, config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/cover.py
Expand Up @@ -34,6 +34,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
Expand All @@ -42,6 +43,8 @@
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -170,6 +173,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT cover through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/fan.py
Expand Up @@ -26,6 +26,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
Expand All @@ -34,6 +35,8 @@
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -111,6 +114,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT fan through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/mqtt/light/__init__.py
Expand Up @@ -10,8 +10,10 @@
clear_discovery_hash,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from .. import DOMAIN, PLATFORMS
from .schema import CONF_SCHEMA, MQTT_LIGHT_SCHEMA_SCHEMA
from .schema_basic import PLATFORM_SCHEMA_BASIC, async_setup_entity_basic
from .schema_json import PLATFORM_SCHEMA_JSON, async_setup_entity_json
Expand Down Expand Up @@ -39,6 +41,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT light through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(hass, config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/lock.py
Expand Up @@ -15,6 +15,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

from . import (
Expand All @@ -23,6 +24,8 @@
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -73,6 +76,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT lock panel through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/sensor.py
Expand Up @@ -22,13 +22,16 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from homeassistant.util import dt as dt_util

from . import (
ATTR_DISCOVERY_HASH,
CONF_QOS,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -66,6 +69,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT sensors through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities)


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/mqtt/services.yaml
Expand Up @@ -35,3 +35,6 @@ dump:
description: how long we should listen for messages in seconds
example: 5
default: 5

reload:
description: Reload all mqtt entities from yaml.
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/switch.py
Expand Up @@ -19,6 +19,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, HomeAssistantType

Expand All @@ -28,6 +29,8 @@
CONF_QOS,
CONF_RETAIN,
CONF_STATE_TOPIC,
DOMAIN,
PLATFORMS,
MqttAttributes,
MqttAvailability,
MqttDiscoveryUpdate,
Expand Down Expand Up @@ -69,6 +72,7 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
):
"""Set up MQTT switch through configuration.yaml."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities, discovery_info)


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/mqtt/vacuum/__init__.py
Expand Up @@ -10,7 +10,9 @@
)
from homeassistant.components.vacuum import DOMAIN
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.reload import async_setup_reload_service

from .. import DOMAIN as MQTT_DOMAIN, PLATFORMS
from .schema import CONF_SCHEMA, LEGACY, MQTT_VACUUM_SCHEMA, STATE
from .schema_legacy import PLATFORM_SCHEMA_LEGACY, async_setup_entity_legacy
from .schema_state import PLATFORM_SCHEMA_STATE, async_setup_entity_state
Expand All @@ -31,6 +33,7 @@ def validate_mqtt_vacuum(value):

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up MQTT vacuum through configuration.yaml."""
await async_setup_reload_service(hass, MQTT_DOMAIN, PLATFORMS)
await _async_setup_entity(config, async_add_entities, discovery_info)


Expand Down
45 changes: 44 additions & 1 deletion tests/components/mqtt/test_light.py
Expand Up @@ -153,11 +153,14 @@
payload_off: "off"

"""
from os import path

import pytest

from homeassistant import config as hass_config
from homeassistant.components import light, mqtt
from homeassistant.components.mqtt.discovery import async_start
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON
from homeassistant.const import ATTR_ASSUMED_STATE, SERVICE_RELOAD, STATE_OFF, STATE_ON
import homeassistant.core as ha
from homeassistant.setup import async_setup_component

Expand Down Expand Up @@ -1558,3 +1561,43 @@ async def test_max_mireds(hass, mqtt_mock):
state = hass.states.get("light.test")
assert state.attributes.get("min_mireds") == 153
assert state.attributes.get("max_mireds") == 370


async def test_reloadable(hass, mqtt_mock):
"""Test reloading an mqtt light."""
config = {
light.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "test/set",
}
}

assert await async_setup_component(hass, light.DOMAIN, config)
await hass.async_block_till_done()

assert hass.states.get("light.test")
assert len(hass.states.async_all()) == 1

yaml_path = path.join(
_get_fixtures_base_path(),
"fixtures",
"mqtt/configuration.yaml",
)
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
"mqtt",
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()

assert len(hass.states.async_all()) == 1

assert hass.states.get("light.test") is None
assert hass.states.get("light.reload")


def _get_fixtures_base_path():
return path.dirname(path.dirname(path.dirname(__file__)))
4 changes: 4 additions & 0 deletions tests/fixtures/mqtt/configuration.yaml
@@ -0,0 +1,4 @@
light:
- platform: mqtt
name: reload
command_topic: "test/set"