Skip to content

Commit

Permalink
Use config entry callbacks in Gree (#104740)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Nov 29, 2023
1 parent e10d58e commit 1fefa93
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
15 changes: 4 additions & 11 deletions homeassistant/components/gree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from .bridge import DiscoveryService
from .const import (
COORDINATORS,
DATA_DISCOVERY_INTERVAL,
DATA_DISCOVERY_SERVICE,
DISCOVERY_SCAN_INTERVAL,
DISPATCHERS,
Expand All @@ -29,7 +28,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
gree_discovery = DiscoveryService(hass)
hass.data[DATA_DISCOVERY_SERVICE] = gree_discovery

hass.data[DOMAIN].setdefault(DISPATCHERS, [])
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async def _async_scan_update(_=None):
Expand All @@ -39,22 +37,17 @@ async def _async_scan_update(_=None):
_LOGGER.debug("Scanning network for Gree devices")
await _async_scan_update()

hass.data[DOMAIN][DATA_DISCOVERY_INTERVAL] = async_track_time_interval(
hass, _async_scan_update, timedelta(seconds=DISCOVERY_SCAN_INTERVAL)
entry.async_on_unload(
async_track_time_interval(
hass, _async_scan_update, timedelta(seconds=DISCOVERY_SCAN_INTERVAL)
)
)

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if hass.data[DOMAIN].get(DISPATCHERS) is not None:
for cleanup in hass.data[DOMAIN][DISPATCHERS]:
cleanup()

if hass.data[DOMAIN].get(DATA_DISCOVERY_INTERVAL) is not None:
hass.data[DOMAIN].pop(DATA_DISCOVERY_INTERVAL)()

if hass.data.get(DATA_DISCOVERY_SERVICE) is not None:
hass.data.pop(DATA_DISCOVERY_SERVICE)

Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/gree/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from .const import (
COORDINATORS,
DISPATCH_DEVICE_DISCOVERED,
DISPATCHERS,
DOMAIN,
FAN_MEDIUM_HIGH,
FAN_MEDIUM_LOW,
Expand Down Expand Up @@ -88,7 +87,7 @@

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Gree HVAC device from a config entry."""
Expand All @@ -101,7 +100,7 @@ def init_device(coordinator):
for coordinator in hass.data[DOMAIN][COORDINATORS]:
init_device(coordinator)

hass.data[DOMAIN][DISPATCHERS].append(
entry.async_on_unload(
async_dispatcher_connect(hass, DISPATCH_DEVICE_DISCOVERED, init_device)
)

Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/gree/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
COORDINATORS = "coordinators"

DATA_DISCOVERY_SERVICE = "gree_discovery"
DATA_DISCOVERY_INTERVAL = "gree_discovery_interval"

DISCOVERY_SCAN_INTERVAL = 300
DISCOVERY_TIMEOUT = 8
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/gree/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DISPATCHERS, DOMAIN
from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DOMAIN
from .entity import GreeEntity


Expand Down Expand Up @@ -102,7 +102,7 @@ def _set_anion(device: Device, value: bool) -> None:

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Gree HVAC device from a config entry."""
Expand All @@ -119,7 +119,7 @@ def init_device(coordinator):
for coordinator in hass.data[DOMAIN][COORDINATORS]:
init_device(coordinator)

hass.data[DOMAIN][DISPATCHERS].append(
entry.async_on_unload(
async_dispatcher_connect(hass, DISPATCH_DEVICE_DISCOVERED, init_device)
)

Expand Down

0 comments on commit 1fefa93

Please sign in to comment.