mirror of https://github.com/home-assistant/core
Add device registry to MQTT switches (#17244)
This commit is contained in:
parent
744dd42ad3
commit
c41ef65da6
|
@ -14,12 +14,12 @@ from homeassistant.components.mqtt import (
|
|||
ATTR_DISCOVERY_HASH, CONF_STATE_TOPIC, CONF_COMMAND_TOPIC,
|
||||
CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
||||
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN, MqttAvailability,
|
||||
MqttDiscoveryUpdate)
|
||||
MqttDiscoveryUpdate, MqttEntityDeviceInfo)
|
||||
from homeassistant.components.mqtt.discovery import MQTT_DISCOVERY_NEW
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF,
|
||||
CONF_PAYLOAD_ON, CONF_ICON, STATE_ON)
|
||||
CONF_PAYLOAD_ON, CONF_ICON, STATE_ON, CONF_DEVICE)
|
||||
from homeassistant.components import mqtt, switch
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
@ -47,6 +47,7 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({
|
|||
vol.Optional(CONF_STATE_OFF): cv.string,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
||||
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
||||
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
||||
|
||||
|
||||
|
@ -94,13 +95,15 @@ async def _async_setup_entity(hass, config, async_add_entities,
|
|||
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
|
||||
config.get(CONF_UNIQUE_ID),
|
||||
value_template,
|
||||
config.get(CONF_DEVICE),
|
||||
discovery_hash,
|
||||
)
|
||||
|
||||
async_add_entities([newswitch])
|
||||
|
||||
|
||||
class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, SwitchDevice):
|
||||
class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
||||
SwitchDevice):
|
||||
"""Representation of a switch that can be toggled using MQTT."""
|
||||
|
||||
def __init__(self, name, icon,
|
||||
|
@ -108,11 +111,13 @@ class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, SwitchDevice):
|
|||
qos, retain, payload_on, payload_off, state_on,
|
||||
state_off, optimistic, payload_available,
|
||||
payload_not_available, unique_id: Optional[str],
|
||||
value_template, discovery_hash):
|
||||
value_template, device_config: Optional[ConfigType],
|
||||
discovery_hash):
|
||||
"""Initialize the MQTT switch."""
|
||||
MqttAvailability.__init__(self, availability_topic, qos,
|
||||
payload_available, payload_not_available)
|
||||
MqttDiscoveryUpdate.__init__(self, discovery_hash)
|
||||
MqttEntityDeviceInfo.__init__(self, device_config)
|
||||
self._state = False
|
||||
self._name = name
|
||||
self._icon = icon
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
"""The tests for the MQTT switch platform."""
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
@ -337,3 +338,42 @@ async def test_discovery_removal_switch(hass, mqtt_mock, caplog):
|
|||
|
||||
state = hass.states.get('switch.beer')
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
||||
"""Test MQTT switch device registry integration."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
entry.add_to_hass(hass)
|
||||
await async_start(hass, 'homeassistant', {}, entry)
|
||||
registry = await hass.helpers.device_registry.async_get_registry()
|
||||
|
||||
data = json.dumps({
|
||||
'platform': 'mqtt',
|
||||
'name': 'Test 1',
|
||||
'state_topic': 'test-topic',
|
||||
'command_topic': 'test-command-topic',
|
||||
'device': {
|
||||
'identifiers': ['helloworld'],
|
||||
'connections': [
|
||||
["mac", "02:5b:26:a8:dc:12"],
|
||||
],
|
||||
'manufacturer': 'Whatever',
|
||||
'name': 'Beer',
|
||||
'model': 'Glass',
|
||||
'sw_version': '0.1-beta',
|
||||
},
|
||||
'unique_id': 'veryunique'
|
||||
})
|
||||
async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
|
||||
data)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device = registry.async_get_device({('mqtt', 'helloworld')}, set())
|
||||
assert device is not None
|
||||
assert device.identifiers == {('mqtt', 'helloworld')}
|
||||
assert device.connections == {('mac', "02:5b:26:a8:dc:12")}
|
||||
assert device.manufacturer == 'Whatever'
|
||||
assert device.name == 'Beer'
|
||||
assert device.model == 'Glass'
|
||||
assert device.sw_version == '0.1-beta'
|
||||
|
|
Loading…
Reference in New Issue