mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 06:17:07 +00:00
Fix mobile app dispatcher performance (#99647)
Fix mobile app thundering heard The mobile_app would setup a dispatcher to listener for updates on every entity and reject the ones that were not for the unique id that it was intrested in. Instead we now register for a signal per unique id since we were previously generating O(entities*sensors*devices) callbacks which was causing the event loop to stall when there were a large number of mobile app users.
This commit is contained in:
parent
9e03f8a8d6
commit
5d1fe0eb00
@ -1,6 +1,8 @@
|
|||||||
"""A entity class for mobile_app."""
|
"""A entity class for mobile_app."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ICON, CONF_NAME, CONF_UNIQUE_ID, STATE_UNAVAILABLE
|
from homeassistant.const import ATTR_ICON, CONF_NAME, CONF_UNIQUE_ID, STATE_UNAVAILABLE
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
@ -36,7 +38,9 @@ class MobileAppEntity(RestoreEntity):
|
|||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
self.hass, SIGNAL_SENSOR_UPDATE, self._handle_update
|
self.hass,
|
||||||
|
f"{SIGNAL_SENSOR_UPDATE}-{self._attr_unique_id}",
|
||||||
|
self._handle_update,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -96,10 +100,7 @@ class MobileAppEntity(RestoreEntity):
|
|||||||
return self._config.get(ATTR_SENSOR_STATE) != STATE_UNAVAILABLE
|
return self._config.get(ATTR_SENSOR_STATE) != STATE_UNAVAILABLE
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_update(self, incoming_id, data):
|
def _handle_update(self, data: dict[str, Any]) -> None:
|
||||||
"""Handle async event updates."""
|
"""Handle async event updates."""
|
||||||
if incoming_id != self._attr_unique_id:
|
self._config.update(data)
|
||||||
return
|
|
||||||
|
|
||||||
self._config = {**self._config, **data}
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -607,7 +607,7 @@ async def webhook_register_sensor(
|
|||||||
if changes:
|
if changes:
|
||||||
entity_registry.async_update_entity(existing_sensor, **changes)
|
entity_registry.async_update_entity(existing_sensor, **changes)
|
||||||
|
|
||||||
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE, unique_store_key, data)
|
async_dispatcher_send(hass, f"{SIGNAL_SENSOR_UPDATE}-{unique_store_key}", data)
|
||||||
else:
|
else:
|
||||||
data[CONF_UNIQUE_ID] = unique_store_key
|
data[CONF_UNIQUE_ID] = unique_store_key
|
||||||
data[
|
data[
|
||||||
@ -693,8 +693,7 @@ async def webhook_update_sensor_states(
|
|||||||
sensor[CONF_WEBHOOK_ID] = config_entry.data[CONF_WEBHOOK_ID]
|
sensor[CONF_WEBHOOK_ID] = config_entry.data[CONF_WEBHOOK_ID]
|
||||||
async_dispatcher_send(
|
async_dispatcher_send(
|
||||||
hass,
|
hass,
|
||||||
SIGNAL_SENSOR_UPDATE,
|
f"{SIGNAL_SENSOR_UPDATE}-{unique_store_key}",
|
||||||
unique_store_key,
|
|
||||||
sensor,
|
sensor,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user