Skip to content
Snippets Groups Projects
Unverified Commit cd004642 authored by Erik Montnemery's avatar Erik Montnemery Committed by GitHub
Browse files

Remove use of hass.helpers from MQTT (#66757)

* Remove use of hass.helpers from MQTT

* Tweak
parent 1a247f7d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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]
......
......@@ -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]},
......
......@@ -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])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment