Clean mysensors on_unload (#51521)

* Clean mysensors on_unload

* Fix docstring
This commit is contained in:
Martin Hjelmare 2021-06-05 20:02:32 +02:00 committed by GitHub
parent 59b5f94569
commit 984695e99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 31 deletions

View File

@ -218,7 +218,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG], hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG],
) )
await on_unload( on_unload(
hass, hass,
entry.entry_id, entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(

View File

@ -10,7 +10,6 @@ from homeassistant.components.binary_sensor import (
DOMAIN, DOMAIN,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ON from homeassistant.const import STATE_ON
@ -18,6 +17,8 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .helpers import on_unload
SENSORS = { SENSORS = {
"S_DOOR": "door", "S_DOOR": "door",
"S_MOTION": DEVICE_CLASS_MOTION, "S_MOTION": DEVICE_CLASS_MOTION,
@ -48,9 +49,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities, async_add_entities=async_add_entities,
) )
await on_unload( on_unload(
hass, hass,
config_entry, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN), MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View File

@ -13,7 +13,6 @@ from homeassistant.components.climate.const import (
SUPPORT_TARGET_TEMPERATURE, SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE, SUPPORT_TARGET_TEMPERATURE_RANGE,
) )
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
@ -21,6 +20,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .helpers import on_unload
DICT_HA_TO_MYS = { DICT_HA_TO_MYS = {
HVAC_MODE_AUTO: "AutoChangeOver", HVAC_MODE_AUTO: "AutoChangeOver",
HVAC_MODE_COOL: "CoolOn", HVAC_MODE_COOL: "CoolOn",
@ -55,9 +56,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities, async_add_entities=async_add_entities,
) )
await on_unload( on_unload(
hass, hass,
config_entry, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN), MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View File

@ -4,7 +4,6 @@ import logging
from homeassistant.components import mysensors from homeassistant.components import mysensors
from homeassistant.components.cover import ATTR_POSITION, DOMAIN, CoverEntity from homeassistant.components.cover import ATTR_POSITION, DOMAIN, CoverEntity
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
@ -12,6 +11,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .helpers import on_unload
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -42,7 +43,7 @@ async def async_setup_entry(
async_add_entities=async_add_entities, async_add_entities=async_add_entities,
) )
await on_unload( on_unload(
hass, hass,
config_entry.entry_id, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(

View File

@ -1,12 +1,14 @@
"""Support for tracking MySensors devices.""" """Support for tracking MySensors devices."""
from homeassistant.components import mysensors from homeassistant.components import mysensors
from homeassistant.components.device_tracker import DOMAIN from homeassistant.components.device_tracker import DOMAIN
from homeassistant.components.mysensors import DevId, on_unload from homeassistant.components.mysensors import DevId
from homeassistant.components.mysensors.const import ATTR_GATEWAY_ID, GatewayId from homeassistant.components.mysensors.const import ATTR_GATEWAY_ID, GatewayId
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import slugify from homeassistant.util import slugify
from .helpers import on_unload
async def async_setup_scanner( async def async_setup_scanner(
hass: HomeAssistant, config, async_see, discovery_info=None hass: HomeAssistant, config, async_see, discovery_info=None
@ -28,7 +30,7 @@ async def async_setup_scanner(
for device in new_devices: for device in new_devices:
gateway_id: GatewayId = discovery_info[ATTR_GATEWAY_ID] gateway_id: GatewayId = discovery_info[ATTR_GATEWAY_ID]
dev_id: DevId = (gateway_id, device.node_id, device.child_id, device.value_type) dev_id: DevId = (gateway_id, device.node_id, device.child_id, device.value_type)
await on_unload( on_unload(
hass, hass,
gateway_id, gateway_id,
async_dispatcher_connect( async_dispatcher_connect(
@ -37,7 +39,7 @@ async def async_setup_scanner(
device.async_update_callback, device.async_update_callback,
), ),
) )
await on_unload( on_unload(
hass, hass,
gateway_id, gateway_id,
async_dispatcher_connect( async_dispatcher_connect(

View File

@ -292,7 +292,7 @@ async def _gw_start(hass: HomeAssistant, entry: ConfigEntry, gateway: BaseAsyncG
async def stop_this_gw(_: Event): async def stop_this_gw(_: Event):
await gw_stop(hass, entry, gateway) await gw_stop(hass, entry, gateway)
await on_unload( on_unload(
hass, hass,
entry.entry_id, entry.entry_id,
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_this_gw), hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_this_gw),

View File

@ -10,7 +10,6 @@ from mysensors import BaseAsyncGateway, Message
from mysensors.sensor import ChildSensor from mysensors.sensor import ChildSensor
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -35,18 +34,13 @@ _LOGGER = logging.getLogger(__name__)
SCHEMAS = Registry() SCHEMAS = Registry()
async def on_unload( @callback
hass: HomeAssistant, entry: ConfigEntry | GatewayId, fnct: Callable def on_unload(hass: HomeAssistant, gateway_id: GatewayId, fnct: Callable) -> None:
) -> None:
"""Register a callback to be called when entry is unloaded. """Register a callback to be called when entry is unloaded.
This function is used by platforms to cleanup after themselves. This function is used by platforms to cleanup after themselves.
""" """
if isinstance(entry, GatewayId): key = MYSENSORS_ON_UNLOAD.format(gateway_id)
uniqueid = entry
else:
uniqueid = entry.entry_id
key = MYSENSORS_ON_UNLOAD.format(uniqueid)
if key not in hass.data[DOMAIN]: if key not in hass.data[DOMAIN]:
hass.data[DOMAIN][key] = [] hass.data[DOMAIN][key] = []
hass.data[DOMAIN][key].append(fnct) hass.data[DOMAIN][key].append(fnct)

View File

@ -10,7 +10,6 @@ from homeassistant.components.light import (
SUPPORT_WHITE_VALUE, SUPPORT_WHITE_VALUE,
LightEntity, LightEntity,
) )
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
@ -20,6 +19,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
from homeassistant.util.color import rgb_hex_to_rgb_list from homeassistant.util.color import rgb_hex_to_rgb_list
from .helpers import on_unload
SUPPORT_MYSENSORS_RGBW = SUPPORT_COLOR | SUPPORT_WHITE_VALUE SUPPORT_MYSENSORS_RGBW = SUPPORT_COLOR | SUPPORT_WHITE_VALUE
@ -45,9 +46,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities, async_add_entities=async_add_entities,
) )
await on_unload( on_unload(
hass, hass,
config_entry, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN), MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View File

@ -2,7 +2,6 @@
from awesomeversion import AwesomeVersion from awesomeversion import AwesomeVersion
from homeassistant.components import mysensors from homeassistant.components import mysensors
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.components.sensor import DOMAIN, SensorEntity from homeassistant.components.sensor import DOMAIN, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -27,6 +26,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .helpers import on_unload
SENSORS = { SENSORS = {
"V_TEMP": [None, "mdi:thermometer"], "V_TEMP": [None, "mdi:thermometer"],
"V_HUM": [PERCENTAGE, "mdi:water-percent"], "V_HUM": [PERCENTAGE, "mdi:water-percent"],
@ -79,9 +80,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities, async_add_entities=async_add_entities,
) )
await on_unload( on_unload(
hass, hass,
config_entry, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN), MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View File

@ -8,10 +8,10 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import on_unload
from ...config_entries import ConfigEntry from ...config_entries import ConfigEntry
from ...helpers.dispatcher import async_dispatcher_connect from ...helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN as MYSENSORS_DOMAIN, MYSENSORS_DISCOVERY, SERVICE_SEND_IR_CODE from .const import DOMAIN as MYSENSORS_DOMAIN, MYSENSORS_DISCOVERY, SERVICE_SEND_IR_CODE
from .helpers import on_unload
ATTR_IR_CODE = "V_IR_SEND" ATTR_IR_CODE = "V_IR_SEND"
@ -83,9 +83,9 @@ async def async_setup_entry(
schema=SEND_IR_CODE_SERVICE_SCHEMA, schema=SEND_IR_CODE_SERVICE_SCHEMA,
) )
await on_unload( on_unload(
hass, hass,
config_entry, config_entry.entry_id,
async_dispatcher_connect( async_dispatcher_connect(
hass, hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN), MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),