Skip to content

Commit 042cd0e

Browse files
committed
Merge pull request #93 from c-st/fix-async
Fix startup issues
2 parents 3bcaf5d + 6f4b61c commit 042cd0e

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

custom_components/auto_areas/__init__.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""🤖 Auto Areas. A custom component for Home Assistant which automates your areas."""
22
from __future__ import annotations
3+
import asyncio
34

45
from homeassistant.helpers import issue_registry
56
from homeassistant.config_entries import ConfigEntry, ConfigType
67
from homeassistant.const import Platform
78
from homeassistant.core import HomeAssistant
9+
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
10+
811

912
from .auto_area import (
1013
AutoArea,
@@ -18,15 +21,33 @@
1821
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1922
"""Initialize AutoArea for this config entry."""
2023
hass.data.setdefault(DOMAIN, {})
21-
hass.data[DOMAIN][entry.entry_id] = AutoArea(hass=hass, entry=entry)
2224

25+
auto_area = AutoArea(hass=hass, entry=entry)
26+
hass.data[DOMAIN][entry.entry_id] = auto_area
27+
28+
# Initialize AutoArea once HA is started
29+
if hass.is_running:
30+
initialize(hass, entry, auto_area)
31+
else:
32+
hass.bus.async_listen_once(
33+
EVENT_HOMEASSISTANT_STARTED,
34+
lambda init: initialize(hass, entry, auto_area)
35+
)
36+
37+
return True
38+
39+
async def async_init(hass: HomeAssistant, entry: ConfigEntry, auto_area: AutoArea):
40+
await auto_area.initialize()
41+
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
2342
hass.async_create_task(
2443
hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2544
)
26-
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
27-
28-
return True
2945

46+
def initialize(hass: HomeAssistant, entry: ConfigEntry, auto_area: AutoArea):
47+
"""Initialize area after HA has started."""
48+
return asyncio.run_coroutine_threadsafe(
49+
async_init(hass, entry, auto_area), hass.loop
50+
).result()
3051

3152
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3253
"""Handle removal of an entry."""

custom_components/auto_areas/auto_area.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Core entity functionality."""
22
from __future__ import annotations
33
from homeassistant.core import HomeAssistant
4-
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
54

65
from homeassistant.config_entries import ConfigEntry
76

@@ -38,16 +37,13 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
3837

3938
LOGGER.info('🤖 Auto Area "%s" (%s)', entry.title, entry.options)
4039

41-
if self.hass.is_running:
42-
self.hass.async_create_task(self.initialize())
43-
else:
44-
self.hass.bus.async_listen_once(
45-
EVENT_HOMEASSISTANT_STARTED, self.initialize()
46-
)
4740

4841
async def initialize(self):
4942
"""Subscribe to area changes and reload if necessary."""
43+
LOGGER.info("%s: Initializing after HA start", self.area.name)
44+
5045
self.auto_lights = AutoLights(self)
46+
await self.auto_lights.initialize()
5147

5248
def cleanup(self):
5349
"""Deinitialize this area."""

custom_components/auto_areas/auto_lights.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
44
from homeassistant.helpers.event import async_track_state_change
55
from homeassistant.const import (
6-
EVENT_HOMEASSISTANT_STARTED,
76
STATE_ON,
87
SERVICE_TURN_ON,
98
SERVICE_TURN_OFF,
@@ -84,25 +83,6 @@ def __init__(self, auto_area) -> None:
8483
)
8584
return
8685

87-
if self.hass.is_running:
88-
self.hass.async_create_task(self.initialize())
89-
else:
90-
self.hass.bus.async_listen_once(
91-
EVENT_HOMEASSISTANT_STARTED, self.initialize()
92-
)
93-
94-
def cleanup(self):
95-
"""Deinitialize this area."""
96-
LOGGER.debug("%s: Disabling light control", self.auto_area.area.name)
97-
if self.unsubscribe_sleep_mode is not None:
98-
self.unsubscribe_sleep_mode()
99-
100-
if self.unsubscribe_presence is not None:
101-
self.unsubscribe_presence()
102-
103-
if self.unsubscribe_illuminance is not None:
104-
self.unsubscribe_illuminance()
105-
10686
async def initialize(self):
10787
"""Start subscribing to state changes."""
10888

@@ -326,3 +306,15 @@ def get_current_illuminance(self) -> float | None:
326306
current_illuminance = None
327307

328308
return current_illuminance
309+
310+
def cleanup(self):
311+
"""Deinitialize this area."""
312+
LOGGER.debug("%s: Disabling light control", self.auto_area.area.name)
313+
if self.unsubscribe_sleep_mode is not None:
314+
self.unsubscribe_sleep_mode()
315+
316+
if self.unsubscribe_presence is not None:
317+
self.unsubscribe_presence()
318+
319+
if self.unsubscribe_illuminance is not None:
320+
self.unsubscribe_illuminance()

custom_components/auto_areas/binary_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_presence_entities(self) -> list(str):
9494

9595
async def async_added_to_hass(self):
9696
"""Start tracking sensors."""
97-
LOGGER.info(
97+
LOGGER.debug(
9898
"%s: Presence detection entities %s",
9999
self.auto_area.area.name,
100100
self.presence_entities,

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
colorlog==6.8.2
2-
homeassistant==2023.2.0
1+
colorlog==6.7.0
2+
homeassistant==2023.4.0
33
pip>=21.0,<24.1
44
ruff==0.1.14

0 commit comments

Comments
 (0)