Skip to content

Commit

Permalink
Disable extra=vol.ALLOW_EXTRA for MQTT platforms. (home-assistant#20562)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored and balloob committed Jan 29, 2019
1 parent 48f0e83 commit 89fc3b2
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 56 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> \
vol.Optional(CONF_JSON_ATTRS_TOPIC): valid_subscribe_topic,
})

MQTT_BASE_PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(SCHEMA_BASE)
MQTT_BASE_PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA_2.extend(SCHEMA_BASE)

# Sensor type platforms subscribe to MQTT events
MQTT_RO_PLATFORM_SCHEMA = MQTT_BASE_PLATFORM_SCHEMA.extend({
Expand Down Expand Up @@ -985,6 +985,7 @@ def discovery_callback(payload):
elif self._discovery_update:
# Non-empty payload: Notify component
_LOGGER.info("Updating component: %s", self.entity_id)
payload.pop(ATTR_DISCOVERY_HASH)
self.hass.async_create_task(self._discovery_update(payload))

if self._discovery_hash:
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/mqtt/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend({
vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_RETAIN, default=mqtt.DEFAULT_RETAIN): cv.boolean,
vol.Required(CONF_STATE_TOPIC): mqtt.valid_subscribe_topic,
vol.Optional(CONF_CODE): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
Expand All @@ -63,7 +64,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add an MQTT alarm control panel."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add a MQTT binary sensor."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add an MQTT cover."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ async def async_device_message_received(topic, payload, qos):
key = ABBREVIATIONS.get(key, key)
payload[key] = payload.pop(abbreviated_key)

if TOPIC_BASE in payload:
base = payload[TOPIC_BASE]
base = payload.pop(TOPIC_BASE, None)
if base:
for key, value in payload.items():
if isinstance(value, str) and value:
if value[0] == TOPIC_BASE and key.endswith('_topic'):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add a MQTT fan."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
19 changes: 13 additions & 6 deletions homeassistant/components/mqtt/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType, ConfigType

from . import schema_basic
from . import schema_json
from . import schema_template

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['mqtt']
Expand All @@ -28,6 +24,10 @@

def validate_mqtt_light(value):
"""Validate MQTT light schema."""
from . import schema_basic
from . import schema_json
from . import schema_template

schemas = {
'basic': schema_basic.PLATFORM_SCHEMA_BASIC,
'json': schema_json.PLATFORM_SCHEMA_JSON,
Expand All @@ -36,9 +36,12 @@ def validate_mqtt_light(value):
return schemas[value[CONF_SCHEMA]](value)


PLATFORM_SCHEMA = vol.All(vol.Schema({
MQTT_LIGHT_SCHEMA_SCHEMA = vol.Schema({
vol.Optional(CONF_SCHEMA, default='basic'): vol.All(
vol.Lower, vol.Any('basic', 'json', 'template'))
})

PLATFORM_SCHEMA = vol.All(MQTT_LIGHT_SCHEMA_SCHEMA.extend({
}, extra=vol.ALLOW_EXTRA), validate_mqtt_light)


Expand All @@ -53,7 +56,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add a MQTT light."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand All @@ -70,6 +73,10 @@ async def async_discover(discovery_payload):
async def _async_setup_entity(config, async_add_entities, config_entry=None,
discovery_hash=None):
"""Set up a MQTT Light."""
from . import schema_basic
from . import schema_json
from . import schema_template

setup_entity = {
'basic': schema_basic.async_setup_entity_basic,
'json': schema_json.async_setup_entity_json,
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/mqtt/light/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import homeassistant.helpers.config_validation as cv
import homeassistant.util.color as color_util

from . import MQTT_LIGHT_SCHEMA_SCHEMA

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['mqtt']
Expand Down Expand Up @@ -108,7 +110,7 @@
vol.In(VALUES_ON_COMMAND_TYPE),
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema).extend(MQTT_LIGHT_SCHEMA_SCHEMA.schema)


async def async_setup_entity_basic(config, async_add_entities, config_entry,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/mqtt/light/schema_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from homeassistant.helpers.typing import ConfigType
import homeassistant.util.color as color_util

from . import MQTT_LIGHT_SCHEMA_SCHEMA
from .schema_basic import CONF_BRIGHTNESS_SCALE

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -81,7 +82,7 @@
vol.Required(CONF_COMMAND_TOPIC): mqtt.valid_publish_topic,
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema).extend(MQTT_LIGHT_SCHEMA_SCHEMA.schema)


async def async_setup_entity_json(config: ConfigType, async_add_entities,
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/components/mqtt/light/schema_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import homeassistant.util.color as color_util
from homeassistant.helpers.restore_state import RestoreEntity

from . import MQTT_LIGHT_SCHEMA_SCHEMA

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'mqtt_template'
Expand Down Expand Up @@ -67,7 +69,7 @@
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema).extend(MQTT_LIGHT_SCHEMA_SCHEMA.schema)


async def async_setup_entity_template(config, async_add_entities, config_entry,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add an MQTT lock."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover_sensor(discovery_payload):
"""Discover and add a discovered MQTT sensor."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add a MQTT switch."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_discover(discovery_payload):
"""Discover and add a MQTT vacuum."""
try:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(config, async_add_entities, config_entry,
discovery_hash)
Expand Down
8 changes: 4 additions & 4 deletions tests/components/mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ async def test_discovery_removal_alarm(hass, mqtt_mock, caplog):

data = (
'{ "name": "Beer",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down Expand Up @@ -409,12 +409,12 @@ async def test_discovery_update_alarm(hass, mqtt_mock, caplog):

data1 = (
'{ "name": "Beer",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
data2 = (
'{ "name": "Milk",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down Expand Up @@ -451,7 +451,7 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
)
data2 = (
'{ "name": "Milk",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down
5 changes: 2 additions & 3 deletions tests/components/mqtt/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
await async_start(hass, 'homeassistant', {}, entry)
data1 = (
'{ "name": "Beer",'
' "command_topic": "test_topic",'
' "state_topic": "test_topic",'
' "json_attributes_topic": "attr-topic1" }'
)
data2 = (
'{ "name": "Beer",'
' "command_topic": "test_topic",'
' "state_topic": "test_topic",'
' "json_attributes_topic": "attr-topic2" }'
)
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
Expand Down Expand Up @@ -540,7 +540,6 @@ async def test_entity_device_info_update(hass, mqtt_mock):
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'command_topic': 'test-command-topic',
'device': {
'identifiers': ['helloworld'],
'connections': [
Expand Down
12 changes: 6 additions & 6 deletions tests/components/mqtt/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,13 +1177,13 @@ async def test_unique_id(hass):
light.DOMAIN: [{
'platform': 'mqtt',
'name': 'Test 1',
'status_topic': 'test-topic',
'state_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'status_topic': 'test-topic',
'state_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
Expand All @@ -1200,7 +1200,7 @@ async def test_discovery_removal_light(hass, mqtt_mock, caplog):

data = (
'{ "name": "Beer",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down Expand Up @@ -1245,12 +1245,12 @@ async def test_discovery_update_light(hass, mqtt_mock, caplog):

data1 = (
'{ "name": "Beer",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
data2 = (
'{ "name": "Milk",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down Expand Up @@ -1284,7 +1284,7 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
)
data2 = (
'{ "name": "Milk",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down
10 changes: 5 additions & 5 deletions tests/components/mqtt/test_light_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,14 @@ async def test_unique_id(hass):
'platform': 'mqtt',
'name': 'Test 1',
'schema': 'json',
'status_topic': 'test-topic',
'state_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'schema': 'json',
'status_topic': 'test-topic',
'state_topic': 'test-topic',
'command_topic': 'test_topic',
'unique_id': 'TOTALLY_UNIQUE'
}]
Expand Down Expand Up @@ -714,13 +714,13 @@ async def test_discovery_update_light(hass, mqtt_mock, caplog):
data1 = (
'{ "name": "Beer",'
' "schema": "json",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
data2 = (
'{ "name": "Milk",'
' "schema": "json",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down Expand Up @@ -755,7 +755,7 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
data2 = (
'{ "name": "Milk",'
' "schema": "json",'
' "status_topic": "test_topic",'
' "state_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

Expand Down
Loading

0 comments on commit 89fc3b2

Please sign in to comment.