Add device entry id to events (#44407)

* Add device entry id to events

* Only add device id if device is known for now
This commit is contained in:
Joakim Plate 2021-01-04 15:31:10 +01:00 committed by GitHub
parent e9f7e67f4c
commit c1027cace6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.binary_sensor import DEVICE_CLASSES_SCHEMA
from homeassistant.const import (
ATTR_DEVICE_ID,
CONF_COMMAND_OFF,
CONF_COMMAND_ON,
CONF_DEVICE,
@ -37,6 +38,7 @@ from homeassistant.const import (
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.helpers.restore_state import RestoreEntity
from .const import (
@ -275,6 +277,10 @@ async def async_setup_internal(hass, entry: config_entries.ConfigEntry):
# Setup some per device config
devices = _get_device_lookup(config[CONF_DEVICES])
device_registry: DeviceRegistry = (
await hass.helpers.device_registry.async_get_registry()
)
# Declare the Handle event
@callback
def async_handle_receive(event):
@ -297,9 +303,18 @@ async def async_setup_internal(hass, entry: config_entries.ConfigEntry):
data_bits = get_device_data_bits(event.device, devices)
device_id = get_device_id(event.device, data_bits=data_bits)
# Register new devices
if config[CONF_AUTOMATIC_ADD] and device_id not in devices:
_add_device(event, device_id)
if device_id not in devices:
if config[CONF_AUTOMATIC_ADD]:
_add_device(event, device_id)
else:
return
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, *device_id)},
connections=set(),
)
if device_entry:
event_data[ATTR_DEVICE_ID] = device_entry.id
# Callback to HA registered components.
hass.helpers.dispatcher.async_dispatcher_send(SIGNAL_EVENT, event, device_id)

View File

@ -5,6 +5,10 @@ from unittest.mock import call
from homeassistant.components.rfxtrx import DOMAIN
from homeassistant.components.rfxtrx.const import EVENT_RFXTRX_EVENT
from homeassistant.core import callback
from homeassistant.helpers.device_registry import (
DeviceRegistry,
async_get_registry as async_get_device_registry,
)
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@ -75,6 +79,8 @@ async def test_fire_event(hass, rfxtrx):
await hass.async_block_till_done()
await hass.async_start()
device_registry: DeviceRegistry = await async_get_device_registry(hass)
calls = []
@callback
@ -88,6 +94,16 @@ async def test_fire_event(hass, rfxtrx):
await rfxtrx.signal("0b1100cd0213c7f210010f51")
await rfxtrx.signal("0716000100900970")
device_id_1 = device_registry.async_get_device(
identifiers={("rfxtrx", "11", "0", "213c7f2:16")}, connections=set()
)
assert device_id_1
device_id_2 = device_registry.async_get_device(
identifiers={("rfxtrx", "16", "0", "00:90")}, connections=set()
)
assert device_id_2
assert calls == [
{
"packet_type": 17,
@ -96,6 +112,7 @@ async def test_fire_event(hass, rfxtrx):
"id_string": "213c7f2:16",
"data": "0b1100cd0213c7f210010f51",
"values": {"Command": "On", "Rssi numeric": 5},
"device_id": device_id_1.id,
},
{
"packet_type": 22,
@ -104,6 +121,7 @@ async def test_fire_event(hass, rfxtrx):
"id_string": "00:90",
"data": "0716000100900970",
"values": {"Command": "Chime", "Rssi numeric": 7, "Sound": 9},
"device_id": device_id_2.id,
},
]