Skip to content

Commit 285c0d2

Browse files
committed
Get gateway using helper function
1 parent 780292e commit 285c0d2

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

custom_components/zha_toolkit/__init__.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,7 @@ async def toolkit_service(service):
678678

679679
zha = hass_ref.data["zha"]
680680
zha_gw: Optional[ZHAGateway] = None
681-
if isinstance(zha, dict):
682-
zha_gw = zha.get("zha_gateway", None)
683-
else:
684-
zha_gw = zha.gateway
681+
zha_gw = u.get_zha_gateway(hass)
685682

686683
if zha_gw is None:
687684
LOGGER.error(

custom_components/zha_toolkit/utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
import aiofiles
1313
import zigpy
14+
from homeassistant.core import HomeAssistant
1415

1516
try:
1617
from homeassistant.components.zha import Gateway as ZHAGateway
1718
except ImportError:
1819
from homeassistant.components.zha.core.gateway import ZHAGateway
1920

21+
from homeassistant.components import zha
22+
from homeassistant.components.zha import helpers as zha_helpers
2023
from homeassistant.util import dt as dt_util
2124
from pkg_resources import get_distribution, parse_version
2225
from zigpy import types as t
@@ -50,6 +53,15 @@
5053
MANIFEST: dict[str, str | list[str]] = {}
5154

5255

56+
def get_zha_gateway(hass: HomeAssistant) -> ZHAGateway:
57+
"""Get the ZHA gateway object."""
58+
if parse_version(HA_VERSION) >= parse_version("2024.8"):
59+
return zha_helpers.get_zha_gateway(hass)
60+
if isinstance(zha, dict):
61+
return zha.get("zha_gateway", None)
62+
return zha.gateway
63+
64+
5365
def getHaVersion() -> str:
5466
"""Get HA Version"""
5567
return HA_VERSION

0 commit comments

Comments
 (0)