diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 3da9a17c3aaf6c947e5f47cfc75e77a5ab17a284..b97a0bc8770f59913ea92a24481b528a35438de5 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -50,7 +50,12 @@ from homeassistant.core import ( ) from homeassistant.data_entry_flow import BaseServiceInfo from homeassistant.exceptions import HomeAssistantError, TemplateError, Unauthorized -from homeassistant.helpers import config_validation as cv, event, template +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + event, + template, +) from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity from homeassistant.helpers.frame import report @@ -1181,8 +1186,8 @@ def _matcher_for_topic(subscription: str) -> Any: @websocket_api.websocket_command( {vol.Required("type"): "mqtt/device/debug_info", vol.Required("device_id"): str} ) -@websocket_api.async_response -async def websocket_mqtt_info(hass, connection, msg): +@callback +def websocket_mqtt_info(hass, connection, msg): """Get MQTT debug info for device.""" device_id = msg["device_id"] mqtt_info = debug_info.info_for_device(hass, device_id) @@ -1193,13 +1198,13 @@ async def websocket_mqtt_info(hass, connection, msg): @websocket_api.websocket_command( {vol.Required("type"): "mqtt/device/remove", vol.Required("device_id"): str} ) -@websocket_api.async_response -async def websocket_remove_device(hass, connection, msg): +@callback +def websocket_remove_device(hass, connection, msg): """Delete device.""" device_id = msg["device_id"] - dev_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) - if not (device := dev_registry.async_get(device_id)): + if not (device := device_registry.async_get(device_id)): connection.send_error( msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found" ) @@ -1209,7 +1214,7 @@ async def websocket_remove_device(hass, connection, msg): config_entry = hass.config_entries.async_get_entry(config_entry) # Only delete the device if it belongs to an MQTT device entry if config_entry.domain == DOMAIN: - dev_registry.async_remove_device(device_id) + device_registry.async_remove_device(device_id) connection.send_message(websocket_api.result_message(msg["id"])) return diff --git a/homeassistant/components/mqtt/debug_info.py b/homeassistant/components/mqtt/debug_info.py index ca9e56f8efb4f71ed13222b8c7fed9f29b1c8073..3bf07db183238c656de2a1bea2d15971418a5faf 100644 --- a/homeassistant/components/mqtt/debug_info.py +++ b/homeassistant/components/mqtt/debug_info.py @@ -162,7 +162,7 @@ def info_for_device(hass, device_id): mqtt_info = {"entities": [], "triggers": []} entity_registry = er.async_get(hass) - entries = hass.helpers.entity_registry.async_entries_for_device( + entries = er.async_entries_for_device( entity_registry, device_id, include_disabled_entities=True ) mqtt_debug_info = hass.data[DATA_MQTT_DEBUG_INFO] diff --git a/homeassistant/components/mqtt/device_trigger.py b/homeassistant/components/mqtt/device_trigger.py index 78f52e5872614e919d66142cd1ed13d48e3ec3d3..f621021e124dc7b5de69e7d7208597ea5c5510c4 100644 --- a/homeassistant/components/mqtt/device_trigger.py +++ b/homeassistant/components/mqtt/device_trigger.py @@ -23,7 +23,7 @@ from homeassistant.const import ( ) from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv +from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -190,9 +190,9 @@ class Trigger: trig.remove = None -async def _update_device(hass, config_entry, config): +def _update_device(hass, config_entry, config): """Update device registry.""" - device_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) config_entry_id = config_entry.entry_id device_info = device_info_from_config(config[CONF_DEVICE]) @@ -228,7 +228,7 @@ async def async_setup_trigger(hass, config, config_entry, discovery_data): _LOGGER.info("Updating trigger: %s", discovery_hash) debug_info.update_trigger_discovery_data(hass, discovery_hash, payload) config = TRIGGER_DISCOVERY_SCHEMA(payload) - await _update_device(hass, config_entry, config) + _update_device(hass, config_entry, config) device_trigger = hass.data[DEVICE_TRIGGERS][discovery_id] await device_trigger.update_trigger(config, discovery_hash, remove_signal) async_dispatcher_send(hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None) @@ -237,9 +237,9 @@ async def async_setup_trigger(hass, config, config_entry, discovery_data): hass, MQTT_DISCOVERY_UPDATED.format(discovery_hash), discovery_update ) - await _update_device(hass, config_entry, config) + _update_device(hass, config_entry, config) - device_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) device = device_registry.async_get_device( {(DOMAIN, id_) for id_ in config[CONF_DEVICE][CONF_IDENTIFIERS]}, {tuple(x) for x in config[CONF_DEVICE][CONF_CONNECTIONS]}, diff --git a/homeassistant/components/mqtt/tag.py b/homeassistant/components/mqtt/tag.py index e415225080238ebbd9d1c8ed44c8c53efc0ba318..186f11534b970a7e605a972c2ed669954fa288b4 100644 --- a/homeassistant/components/mqtt/tag.py +++ b/homeassistant/components/mqtt/tag.py @@ -5,6 +5,7 @@ import logging import voluptuous as vol from homeassistant.const import CONF_DEVICE, CONF_PLATFORM, CONF_VALUE_TEMPLATE +from homeassistant.helpers import device_registry as dr import homeassistant.helpers.config_validation as cv from homeassistant.helpers.device_registry import EVENT_DEVICE_REGISTRY_UPDATED from homeassistant.helpers.dispatcher import ( @@ -61,9 +62,9 @@ async def async_setup_tag(hass, config, config_entry, discovery_data): device_id = None if CONF_DEVICE in config: - await _update_device(hass, config_entry, config) + _update_device(hass, config_entry, config) - device_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) device = device_registry.async_get_device( {(DOMAIN, id_) for id_ in config[CONF_DEVICE][CONF_IDENTIFIERS]}, {tuple(x) for x in config[CONF_DEVICE][CONF_CONNECTIONS]}, @@ -134,7 +135,7 @@ class MQTTTagScanner: config = PLATFORM_SCHEMA(payload) self._config = config if self.device_id: - await _update_device(self.hass, self._config_entry, config) + _update_device(self.hass, self._config_entry, config) self._setup_from_config(config) await self.subscribe_topics() @@ -215,9 +216,9 @@ class MQTTTagScanner: self.hass.data[TAGS][self.device_id].pop(discovery_id) -async def _update_device(hass, config_entry, config): +def _update_device(hass, config_entry, config): """Update device registry.""" - device_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) config_entry_id = config_entry.entry_id device_info = device_info_from_config(config[CONF_DEVICE])