Skip to content

Commit

Permalink
Get gateway using helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Aug 8, 2024
1 parent 4438d84 commit a041cff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 1 addition & 4 deletions custom_components/zha_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,7 @@ async def toolkit_service(service):

zha = hass_ref.data["zha"]
zha_gw: Optional[ZHAGateway] = None
if isinstance(zha, dict):
zha_gw = zha.get("zha_gateway", None)
else:
zha_gw = zha.gateway
zha_gw = u.get_zha_gateway(hass)

if zha_gw is None:
LOGGER.error(
Expand Down
12 changes: 12 additions & 0 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

import aiofiles
import zigpy
from homeassistant.core import HomeAssistant

try:
from homeassistant.components.zha import Gateway as ZHAGateway
except ImportError:
from homeassistant.components.zha.core.gateway import ZHAGateway

from homeassistant.components import zha
from homeassistant.components.zha import helpers as zha_helpers
from homeassistant.util import dt as dt_util
from pkg_resources import get_distribution, parse_version
from zigpy import types as t
Expand Down Expand Up @@ -50,6 +53,15 @@
MANIFEST: dict[str, str | list[str]] = {}


def get_zha_gateway(hass: HomeAssistant) -> ZHAGateway:
"""Get the ZHA gateway object."""
if parse_version(HA_VERSION) >= parse_version("2024.8"):
return zha_helpers.get_zha_gateway(hass)
if isinstance(zha, dict):
return zha.get("zha_gateway", None)
return zha.gateway


def getHaVersion() -> str:
"""Get HA Version"""
return HA_VERSION
Expand Down

0 comments on commit a041cff

Please sign in to comment.