mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Cancel tuya updates on the stop event (#49324)
This commit is contained in:
parent
673f542cde
commit
94c803d83b
@ -14,7 +14,12 @@ from tuyaha.tuyaapi import (
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_PLATFORM, CONF_USERNAME
|
from homeassistant.const import (
|
||||||
|
CONF_PASSWORD,
|
||||||
|
CONF_PLATFORM,
|
||||||
|
CONF_USERNAME,
|
||||||
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -61,6 +66,7 @@ TUYA_TYPE_TO_HA = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TUYA_TRACKER = "tuya_tracker"
|
TUYA_TRACKER = "tuya_tracker"
|
||||||
|
STOP_CANCEL = "stop_event_cancel"
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
vol.All(
|
vol.All(
|
||||||
@ -139,8 +145,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
raise ConfigEntryNotReady() from exc
|
raise ConfigEntryNotReady() from exc
|
||||||
|
|
||||||
except TuyaAPIRateLimitException as exc:
|
except TuyaAPIRateLimitException as exc:
|
||||||
_LOGGER.error("Tuya login rate limited")
|
raise ConfigEntryNotReady("Tuya login rate limited") from exc
|
||||||
raise ConfigEntryNotReady() from exc
|
|
||||||
|
|
||||||
except TuyaAPIException as exc:
|
except TuyaAPIException as exc:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
@ -149,7 +154,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
hass.data[DOMAIN] = {
|
domain_data = hass.data[DOMAIN] = {
|
||||||
TUYA_DATA: tuya,
|
TUYA_DATA: tuya,
|
||||||
TUYA_DEVICES_CONF: entry.options.copy(),
|
TUYA_DEVICES_CONF: entry.options.copy(),
|
||||||
TUYA_TRACKER: None,
|
TUYA_TRACKER: None,
|
||||||
@ -174,22 +179,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
dev_type = device.device_type()
|
dev_type = device.device_type()
|
||||||
if (
|
if (
|
||||||
dev_type in TUYA_TYPE_TO_HA
|
dev_type in TUYA_TYPE_TO_HA
|
||||||
and device.object_id() not in hass.data[DOMAIN]["entities"]
|
and device.object_id() not in domain_data["entities"]
|
||||||
):
|
):
|
||||||
ha_type = TUYA_TYPE_TO_HA[dev_type]
|
ha_type = TUYA_TYPE_TO_HA[dev_type]
|
||||||
if ha_type not in device_type_list:
|
if ha_type not in device_type_list:
|
||||||
device_type_list[ha_type] = []
|
device_type_list[ha_type] = []
|
||||||
device_type_list[ha_type].append(device.object_id())
|
device_type_list[ha_type].append(device.object_id())
|
||||||
hass.data[DOMAIN]["entities"][device.object_id()] = None
|
domain_data["entities"][device.object_id()] = None
|
||||||
|
|
||||||
for ha_type, dev_ids in device_type_list.items():
|
for ha_type, dev_ids in device_type_list.items():
|
||||||
config_entries_key = f"{ha_type}.tuya"
|
config_entries_key = f"{ha_type}.tuya"
|
||||||
if config_entries_key not in hass.data[DOMAIN][ENTRY_IS_SETUP]:
|
if config_entries_key not in domain_data[ENTRY_IS_SETUP]:
|
||||||
hass.data[DOMAIN]["pending"][ha_type] = dev_ids
|
domain_data["pending"][ha_type] = dev_ids
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.async_forward_entry_setup(entry, ha_type)
|
hass.config_entries.async_forward_entry_setup(entry, ha_type)
|
||||||
)
|
)
|
||||||
hass.data[DOMAIN][ENTRY_IS_SETUP].add(config_entries_key)
|
domain_data[ENTRY_IS_SETUP].add(config_entries_key)
|
||||||
else:
|
else:
|
||||||
async_dispatcher_send(hass, TUYA_DISCOVERY_NEW.format(ha_type), dev_ids)
|
async_dispatcher_send(hass, TUYA_DISCOVERY_NEW.format(ha_type), dev_ids)
|
||||||
|
|
||||||
@ -212,15 +217,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
newlist_ids = []
|
newlist_ids = []
|
||||||
for device in device_list:
|
for device in device_list:
|
||||||
newlist_ids.append(device.object_id())
|
newlist_ids.append(device.object_id())
|
||||||
for dev_id in list(hass.data[DOMAIN]["entities"]):
|
for dev_id in list(domain_data["entities"]):
|
||||||
if dev_id not in newlist_ids:
|
if dev_id not in newlist_ids:
|
||||||
async_dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
|
async_dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
|
||||||
hass.data[DOMAIN]["entities"].pop(dev_id)
|
domain_data["entities"].pop(dev_id)
|
||||||
|
|
||||||
hass.data[DOMAIN][TUYA_TRACKER] = async_track_time_interval(
|
domain_data[TUYA_TRACKER] = async_track_time_interval(
|
||||||
hass, async_poll_devices_update, timedelta(minutes=2)
|
hass, async_poll_devices_update, timedelta(minutes=2)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _async_cancel_tuya_tracker(event):
|
||||||
|
domain_data[TUYA_TRACKER]()
|
||||||
|
|
||||||
|
domain_data[STOP_CANCEL] = hass.bus.async_listen_once(
|
||||||
|
EVENT_HOMEASSISTANT_STOP, _async_cancel_tuya_tracker
|
||||||
|
)
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_PULL_DEVICES, async_poll_devices_update
|
DOMAIN, SERVICE_PULL_DEVICES, async_poll_devices_update
|
||||||
)
|
)
|
||||||
@ -236,19 +249,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
"""Unloading the Tuya platforms."""
|
"""Unloading the Tuya platforms."""
|
||||||
|
domain_data = hass.data[DOMAIN]
|
||||||
|
|
||||||
unload_ok = all(
|
unload_ok = all(
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
*[
|
*[
|
||||||
hass.config_entries.async_forward_entry_unload(
|
hass.config_entries.async_forward_entry_unload(
|
||||||
entry, platform.split(".", 1)[0]
|
entry, platform.split(".", 1)[0]
|
||||||
)
|
)
|
||||||
for platform in hass.data[DOMAIN][ENTRY_IS_SETUP]
|
for platform in domain_data[ENTRY_IS_SETUP]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
hass.data[DOMAIN]["listener"]()
|
domain_data["listener"]()
|
||||||
hass.data[DOMAIN][TUYA_TRACKER]()
|
domain_data[STOP_CANCEL]()
|
||||||
|
domain_data[TUYA_TRACKER]()
|
||||||
hass.services.async_remove(DOMAIN, SERVICE_FORCE_UPDATE)
|
hass.services.async_remove(DOMAIN, SERVICE_FORCE_UPDATE)
|
||||||
hass.services.async_remove(DOMAIN, SERVICE_PULL_DEVICES)
|
hass.services.async_remove(DOMAIN, SERVICE_PULL_DEVICES)
|
||||||
hass.data.pop(DOMAIN)
|
hass.data.pop(DOMAIN)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user