Clean mysensors on_unload ()

* 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],
)
await on_unload(
on_unload(
hass,
entry.entry_id,
async_dispatcher_connect(

View file

@ -10,7 +10,6 @@ from homeassistant.components.binary_sensor import (
DOMAIN,
BinarySensorEntity,
)
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry
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.entity_platform import AddEntitiesCallback
from .helpers import on_unload
SENSORS = {
"S_DOOR": "door",
"S_MOTION": DEVICE_CLASS_MOTION,
@ -48,9 +49,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities,
)
await on_unload(
on_unload(
hass,
config_entry,
config_entry.entry_id,
async_dispatcher_connect(
hass,
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_RANGE,
)
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry
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.entity_platform import AddEntitiesCallback
from .helpers import on_unload
DICT_HA_TO_MYS = {
HVAC_MODE_AUTO: "AutoChangeOver",
HVAC_MODE_COOL: "CoolOn",
@ -55,9 +56,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities,
)
await on_unload(
on_unload(
hass,
config_entry,
config_entry.entry_id,
async_dispatcher_connect(
hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View file

@ -4,7 +4,6 @@ import logging
from homeassistant.components import mysensors
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.config_entries import ConfigEntry
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.entity_platform import AddEntitiesCallback
from .helpers import on_unload
_LOGGER = logging.getLogger(__name__)
@ -42,7 +43,7 @@ async def async_setup_entry(
async_add_entities=async_add_entities,
)
await on_unload(
on_unload(
hass,
config_entry.entry_id,
async_dispatcher_connect(

View file

@ -1,12 +1,14 @@
"""Support for tracking MySensors devices."""
from homeassistant.components import mysensors
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.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import slugify
from .helpers import on_unload
async def async_setup_scanner(
hass: HomeAssistant, config, async_see, discovery_info=None
@ -28,7 +30,7 @@ async def async_setup_scanner(
for device in new_devices:
gateway_id: GatewayId = discovery_info[ATTR_GATEWAY_ID]
dev_id: DevId = (gateway_id, device.node_id, device.child_id, device.value_type)
await on_unload(
on_unload(
hass,
gateway_id,
async_dispatcher_connect(
@ -37,7 +39,7 @@ async def async_setup_scanner(
device.async_update_callback,
),
)
await on_unload(
on_unload(
hass,
gateway_id,
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):
await gw_stop(hass, entry, gateway)
await on_unload(
on_unload(
hass,
entry.entry_id,
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
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
@ -35,18 +34,13 @@ _LOGGER = logging.getLogger(__name__)
SCHEMAS = Registry()
async def on_unload(
hass: HomeAssistant, entry: ConfigEntry | GatewayId, fnct: Callable
) -> None:
@callback
def on_unload(hass: HomeAssistant, gateway_id: GatewayId, fnct: Callable) -> None:
"""Register a callback to be called when entry is unloaded.
This function is used by platforms to cleanup after themselves.
"""
if isinstance(entry, GatewayId):
uniqueid = entry
else:
uniqueid = entry.entry_id
key = MYSENSORS_ON_UNLOAD.format(uniqueid)
key = MYSENSORS_ON_UNLOAD.format(gateway_id)
if key not in hass.data[DOMAIN]:
hass.data[DOMAIN][key] = []
hass.data[DOMAIN][key].append(fnct)

View file

@ -10,7 +10,6 @@ from homeassistant.components.light import (
SUPPORT_WHITE_VALUE,
LightEntity,
)
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.config_entries import ConfigEntry
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
from homeassistant.util.color import rgb_hex_to_rgb_list
from .helpers import on_unload
SUPPORT_MYSENSORS_RGBW = SUPPORT_COLOR | SUPPORT_WHITE_VALUE
@ -45,9 +46,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities,
)
await on_unload(
on_unload(
hass,
config_entry,
config_entry.entry_id,
async_dispatcher_connect(
hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),

View file

@ -2,7 +2,6 @@
from awesomeversion import AwesomeVersion
from homeassistant.components import mysensors
from homeassistant.components.mysensors import on_unload
from homeassistant.components.mysensors.const import MYSENSORS_DISCOVERY
from homeassistant.components.sensor import DOMAIN, SensorEntity
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.entity_platform import AddEntitiesCallback
from .helpers import on_unload
SENSORS = {
"V_TEMP": [None, "mdi:thermometer"],
"V_HUM": [PERCENTAGE, "mdi:water-percent"],
@ -79,9 +80,9 @@ async def async_setup_entry(
async_add_entities=async_add_entities,
)
await on_unload(
on_unload(
hass,
config_entry,
config_entry.entry_id,
async_dispatcher_connect(
hass,
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
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import on_unload
from ...config_entries import ConfigEntry
from ...helpers.dispatcher import async_dispatcher_connect
from .const import DOMAIN as MYSENSORS_DOMAIN, MYSENSORS_DISCOVERY, SERVICE_SEND_IR_CODE
from .helpers import on_unload
ATTR_IR_CODE = "V_IR_SEND"
@ -83,9 +83,9 @@ async def async_setup_entry(
schema=SEND_IR_CODE_SERVICE_SCHEMA,
)
await on_unload(
on_unload(
hass,
config_entry,
config_entry.entry_id,
async_dispatcher_connect(
hass,
MYSENSORS_DISCOVERY.format(config_entry.entry_id, DOMAIN),