Clean up async_dispatcher_connect helper usage (#68613)

This commit is contained in:
Franck Nijhof 2022-03-24 13:19:11 +01:00 committed by GitHub
parent de40770926
commit 15cffbe496
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 75 additions and 65 deletions

View file

@ -15,7 +15,10 @@ from homeassistant.const import CONF_API_KEY, CONF_NAME
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import Throttle
@ -128,9 +131,7 @@ class AfterShipSensor(SensorEntity):
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
UPDATE_TOPIC, self._force_update
)
async_dispatcher_connect(self.hass, UPDATE_TOPIC, self._force_update)
)
async def _force_update(self) -> None:

View file

@ -22,6 +22,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
@ -95,8 +96,8 @@ class AlarmDecoderAlarmPanel(AlarmControlPanelEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_PANEL_MESSAGE, self._message_callback
async_dispatcher_connect(
self.hass, SIGNAL_PANEL_MESSAGE, self._message_callback
)
)

View file

@ -4,6 +4,7 @@ import logging
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
@ -90,26 +91,24 @@ class AlarmDecoderBinarySensor(BinarySensorEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_ZONE_FAULT, self._fault_callback
async_dispatcher_connect(self.hass, SIGNAL_ZONE_FAULT, self._fault_callback)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass, SIGNAL_ZONE_RESTORE, self._restore_callback
)
)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_ZONE_RESTORE, self._restore_callback
async_dispatcher_connect(
self.hass, SIGNAL_RFX_MESSAGE, self._rfx_message_callback
)
)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_RFX_MESSAGE, self._rfx_message_callback
)
)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_REL_MESSAGE, self._rel_message_callback
async_dispatcher_connect(
self.hass, SIGNAL_REL_MESSAGE, self._rel_message_callback
)
)

View file

@ -2,6 +2,7 @@
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import SIGNAL_PANEL_MESSAGE
@ -26,8 +27,8 @@ class AlarmDecoderSensor(SensorEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_PANEL_MESSAGE, self._message_callback
async_dispatcher_connect(
self.hass, SIGNAL_PANEL_MESSAGE, self._message_callback
)
)

View file

@ -20,6 +20,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -146,8 +147,8 @@ class AquaLogicSensor(SensorEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
UPDATE_TOPIC, self.async_update_callback
async_dispatcher_connect(
self.hass, UPDATE_TOPIC, self.async_update_callback
)
)

View file

@ -8,6 +8,7 @@ from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.const import CONF_MONITORED_CONDITIONS
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -96,7 +97,5 @@ class AquaLogicSwitch(SwitchEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
UPDATE_TOPIC, self.async_write_ha_state
)
async_dispatcher_connect(self.hass, UPDATE_TOPIC, self.async_write_ha_state)
)

View file

@ -16,7 +16,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, TEMP_FAHRENHEIT, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
@ -119,9 +119,7 @@ class EcoNetEntity(Entity):
"""Subscribe to device events."""
await super().async_added_to_hass()
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
PUSH_UPDATE, self.on_update_received
)
async_dispatcher_connect(self.hass, PUSH_UPDATE, self.on_update_received)
)
@callback

View file

@ -2,6 +2,7 @@
from enocean.protocol.packet import Packet
from enocean.utils import combine_hex
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from .const import SIGNAL_RECEIVE_MESSAGE, SIGNAL_SEND_MESSAGE
@ -18,8 +19,8 @@ class EnOceanEntity(Entity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_RECEIVE_MESSAGE, self._message_received_callback
async_dispatcher_connect(
self.hass, SIGNAL_RECEIVE_MESSAGE, self._message_received_callback
)
)

View file

@ -18,6 +18,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.typing import ConfigType
@ -65,8 +66,10 @@ class HomeKitEntity(Entity):
async def async_added_to_hass(self) -> None:
"""Entity added to hass."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
self._accessory.signal_state_updated, self.async_write_ha_state
async_dispatcher_connect(
self.hass,
self._accessory.signal_state_updated,
self.async_write_ha_state,
)
)

View file

@ -15,6 +15,7 @@ from homeassistant.const import (
ATTR_LONGITUDE,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
@ -114,8 +115,10 @@ class MobileAppEntity(TrackerEntity, RestoreEntity):
async def async_added_to_hass(self):
"""Call when entity about to be added to Home Assistant."""
await super().async_added_to_hass()
self._dispatch_unsub = self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_LOCATION_UPDATE.format(self._entry.entry_id), self.update_data
self._dispatch_unsub = async_dispatcher_connect(
self.hass,
SIGNAL_LOCATION_UPDATE.format(self._entry.entry_id),
self.update_data,
)
# Don't restore if we got set up with data.

View file

@ -18,6 +18,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import ConfigType
from homeassistant.setup import async_when_setup
@ -101,8 +102,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
hass.data[DOMAIN]["unsub"] = hass.helpers.dispatcher.async_dispatcher_connect(
DOMAIN, async_handle_message
hass.data[DOMAIN]["unsub"] = async_dispatcher_connect(
hass, DOMAIN, async_handle_message
)
return True

View file

@ -2,6 +2,7 @@
from pyplaato.models.device import PlaatoDevice
from homeassistant.helpers import entity
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
DEVICE,
@ -95,7 +96,8 @@ class PlaatoEntity(entity.Entity):
)
else:
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
async_dispatcher_connect(
self.hass,
SENSOR_SIGNAL % (self._device_id, self._sensor_type),
self.async_write_ha_state,
)

View file

@ -21,6 +21,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType
@ -96,9 +97,7 @@ class QSEntity(Entity):
async def async_added_to_hass(self):
"""Listen for updates from QSUSb via dispatcher."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
self.qsid, self.update_packet
)
async_dispatcher_connect(self.hass, self.qsid, self.update_packet)
)

View file

@ -30,6 +30,7 @@ from homeassistant.helpers.device_registry import (
DeviceEntry,
DeviceRegistry,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
@ -334,7 +335,7 @@ async def async_setup_platform_entry(
async_add_entities(constructor(event, event, device_id, {}))
config_entry.async_on_unload(
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_EVENT, _update)
async_dispatcher_connect(hass, SIGNAL_EVENT, _update)
)
@ -484,9 +485,7 @@ class RfxtrxEntity(RestoreEntity):
self._apply_event(self._event)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_EVENT, self._handle_event
)
async_dispatcher_connect(self.hass, SIGNAL_EVENT, self._handle_event)
)
@property

View file

@ -17,6 +17,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType
@ -188,8 +189,8 @@ class TellstickDevice(Entity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_TELLCORE_CALLBACK, self.update_from_callback
async_dispatcher_connect(
self.hass, SIGNAL_TELLCORE_CALLBACK, self.update_from_callback
)
)

View file

@ -8,6 +8,7 @@ from homeassistant.components.sensor import (
)
from homeassistant.const import PERCENTAGE, POWER_WATT, TEMP_FAHRENHEIT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify
@ -138,8 +139,8 @@ class WaterFurnaceSensor(SensorEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
UPDATE_TOPIC, self.async_update_callback
async_dispatcher_connect(
self.hass, UPDATE_TOPIC, self.async_update_callback
)
)

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -35,13 +36,13 @@ class APICount(SensorEntity):
async def async_added_to_hass(self) -> None:
"""Added to hass."""
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_CONNECTED, self._update_count
async_dispatcher_connect(
self.hass, SIGNAL_WEBSOCKET_CONNECTED, self._update_count
)
)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_DISCONNECTED, self._update_count
async_dispatcher_connect(
self.hass, SIGNAL_WEBSOCKET_DISCONNECTED, self._update_count
)
)

View file

@ -55,6 +55,7 @@ from homeassistant.helpers import (
restore_state,
storage,
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.json import JSONEncoder
from homeassistant.setup import async_setup_component, setup_component
from homeassistant.util.async_ import run_callback_threadsafe
@ -1208,7 +1209,7 @@ def async_mock_signal(hass, signal):
"""Mock service call."""
calls.append(args)
hass.helpers.dispatcher.async_dispatcher_connect(signal, mock_signal_handler)
async_dispatcher_connect(hass, signal, mock_signal_handler)
return calls

View file

@ -58,6 +58,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.setup import async_setup_component
@ -155,7 +156,7 @@ async def test_updates_from_connection_event(
async def set_signal():
event.set()
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_HEOS_UPDATED, set_signal)
async_dispatcher_connect(hass, SIGNAL_HEOS_UPDATED, set_signal)
# Connected
player.available = True
@ -201,7 +202,7 @@ async def test_updates_from_sources_updated(
async def set_signal():
event.set()
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_HEOS_UPDATED, set_signal)
async_dispatcher_connect(hass, SIGNAL_HEOS_UPDATED, set_signal)
input_sources.clear()
player.heos.dispatcher.send(
@ -225,7 +226,7 @@ async def test_updates_from_players_changed(
async def set_signal():
event.set()
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_HEOS_UPDATED, set_signal)
async_dispatcher_connect(hass, SIGNAL_HEOS_UPDATED, set_signal)
assert hass.states.get("media_player.test_player").state == STATE_IDLE
player.state = const.PLAY_STATE_PLAY
@ -259,7 +260,7 @@ async def test_updates_from_players_changed_new_ids(
async def set_signal():
event.set()
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_HEOS_UPDATED, set_signal)
async_dispatcher_connect(hass, SIGNAL_HEOS_UPDATED, set_signal)
player.heos.dispatcher.send(
const.SIGNAL_CONTROLLER_EVENT,
const.EVENT_PLAYERS_CHANGED,
@ -287,7 +288,7 @@ async def test_updates_from_user_changed(hass, config_entry, config, controller)
async def set_signal():
event.set()
hass.helpers.dispatcher.async_dispatcher_connect(SIGNAL_HEOS_UPDATED, set_signal)
async_dispatcher_connect(hass, SIGNAL_HEOS_UPDATED, set_signal)
controller.is_signed_in = False
controller.signed_in_username = None

View file

@ -16,6 +16,7 @@ from homeassistant.components.websocket_api.const import (
URL,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
@ -30,18 +31,14 @@ def track_connected(hass):
def track_connected():
connected_evt.append(1)
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_CONNECTED, track_connected
)
async_dispatcher_connect(hass, SIGNAL_WEBSOCKET_CONNECTED, track_connected)
disconnected_evt = []
@callback
def track_disconnected():
disconnected_evt.append(1)
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_WEBSOCKET_DISCONNECTED, track_disconnected
)
async_dispatcher_connect(hass, SIGNAL_WEBSOCKET_DISCONNECTED, track_disconnected)
return {"connected": connected_evt, "disconnected": disconnected_evt}