Better ZHA device reconfiguration (#49672)

* initial take

* cleanup

* fix mock for configure_reporting
This commit is contained in:
David F. Mulcahey 2021-04-27 10:04:22 -04:00 committed by GitHub
parent 6bc0fb2e42
commit b91d2be00b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 16 deletions

View file

@ -54,6 +54,7 @@ from .core.const import (
WARNING_DEVICE_SQUAWK_MODE_ARMED,
WARNING_DEVICE_STROBE_HIGH,
WARNING_DEVICE_STROBE_YES,
ZHA_CHANNEL_MSG,
ZHA_CONFIG_SCHEMAS,
)
from .core.group import GroupMember
@ -468,34 +469,21 @@ async def websocket_reconfigure_node(hass, connection, msg):
zha_gateway: ZhaGatewayType = hass.data[DATA_ZHA][DATA_ZHA_GATEWAY]
ieee = msg[ATTR_IEEE]
device: ZhaDeviceType = zha_gateway.get_device(ieee)
ieee_str = str(device.ieee)
nwk_str = device.nwk.__repr__()
class DeviceLogFilterer(logging.Filter):
"""Log filterer that limits messages to the specified device."""
def filter(self, record):
message = record.getMessage()
return nwk_str in message or ieee_str in message
filterer = DeviceLogFilterer()
async def forward_messages(data):
"""Forward events to websocket."""
connection.send_message(websocket_api.event_message(msg["id"], data))
remove_dispatcher_function = async_dispatcher_connect(
hass, "zha_gateway_message", forward_messages
hass, ZHA_CHANNEL_MSG, forward_messages
)
@callback
def async_cleanup() -> None:
"""Remove signal listener and turn off debug mode."""
zha_gateway.async_disable_debug_mode(filterer=filterer)
"""Remove signal listener."""
remove_dispatcher_function()
connection.subscriptions[msg["id"]] = async_cleanup
zha_gateway.async_enable_debug_mode(filterer=filterer)
_LOGGER.debug("Reconfiguring node with ieee_address: %s", ieee)
hass.async_create_task(device.async_configure())