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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move more MQTT platforms to config entries #18180

Merged
merged 5 commits into from
Nov 6, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion homeassistant/components/lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def is_locked(hass, entity_id=None):

async def async_setup(hass, config):
"""Track states and offer events for locks."""
component = EntityComponent(
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LOCKS)

await component.async_setup(config)
Expand All @@ -79,6 +79,11 @@ async def async_setup(hass, config):
return True


async def async_setup_entry(hass, entry):
"""Set up a config entry."""
return await hass.data[DOMAIN].async_setup_entry(entry)


class LockDevice(Entity):
"""Representation of a lock."""

Expand Down
35 changes: 25 additions & 10 deletions homeassistant/components/lock/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
CONF_QOS, CONF_RETAIN, MqttAvailability, MqttDiscoveryUpdate)
from homeassistant.const import (
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE)
from homeassistant.components import mqtt
from homeassistant.components import mqtt, lock
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType, ConfigType

_LOGGER = logging.getLogger(__name__)

Expand All @@ -40,20 +43,32 @@
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)


async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the MQTT lock."""
if discovery_info is not None:
config = PLATFORM_SCHEMA(discovery_info)
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_entity(hass, config, async_add_entities)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT lock dynamically through MQTT discovery."""
async def async_discover(discovery_payload):
"""Discover and add an MQTT lock."""
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(hass, config, async_add_entities,
discovery_payload[ATTR_DISCOVERY_HASH])

async_dispatcher_connect(
hass, MQTT_DISCOVERY_NEW.format(lock.DOMAIN, 'mqtt'),
async_discover)


async def _async_setup_entity(hass, config, async_add_entities,
discovery_hash=None):
"""Set up the MQTT Lock platform."""
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = hass

discovery_hash = None
if discovery_info is not None and ATTR_DISCOVERY_HASH in discovery_info:
discovery_hash = discovery_info[ATTR_DISCOVERY_HASH]

async_add_entities([MqttLock(
config.get(CONF_NAME),
config.get(CONF_STATE_TOPIC),
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'camera': ['mqtt'],
'cover': ['mqtt'],
'light': ['mqtt'],
'lock': ['mqtt'],
'sensor': ['mqtt'],
'switch': ['mqtt'],
'climate': ['mqtt'],
Expand Down
7 changes: 4 additions & 3 deletions tests/components/lock/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from homeassistant.setup import async_setup_component
from homeassistant.const import (
STATE_LOCKED, STATE_UNLOCKED, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE)
import homeassistant.components.lock as lock
from homeassistant.components import lock, mqtt
from homeassistant.components.mqtt.discovery import async_start

from tests.common import async_fire_mqtt_message
from tests.common import async_fire_mqtt_message, MockConfigEntry


async def test_controlling_state_via_topic(hass, mqtt_mock):
Expand Down Expand Up @@ -136,7 +136,8 @@ async def test_custom_availability_payload(hass, mqtt_mock):

async def test_discovery_removal_lock(hass, mqtt_mock, caplog):
"""Test removal of discovered lock."""
await async_start(hass, 'homeassistant', {'mqtt': {}})
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)
data = (
'{ "name": "Beer",'
' "command_topic": "test_topic" }'
Expand Down