Skip to content

Commit 063eb8e

Browse files
authored
Merge pull request #440 from dvd-dev/deprecation/replace-dsn-with-hiloid
Deprecation/replace dsn with hiloid
2 parents ba61be2 + 0059e41 commit 063eb8e

3 files changed

Lines changed: 56 additions & 46 deletions

File tree

pyhilo/api.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -715,38 +715,6 @@ async def get_seasons(self, location_id: int) -> list[dict[str, Any]]:
715715

716716
return all_seasons
717717

718-
async def get_gateway(self, location_id: int) -> dict[str, Any]:
719-
"""Gets info about the Hilo hub (gateway)"""
720-
url = self._get_url("Gateways/Info", location_id=location_id)
721-
LOG.debug("Gateway URL is %s", url)
722-
req = await self.async_request("get", url)
723-
saved_attrs = [
724-
"zigBeePairingActivated",
725-
"zigBeeChannel",
726-
"firmwareVersion",
727-
"onlineStatus",
728-
"lastStatusTime",
729-
"disconnected",
730-
]
731-
732-
gw = {
733-
"name": "Hilo Gateway",
734-
"Disconnected": {"value": not req[0].get("onlineStatus") == "Online"},
735-
"type": "Gateway",
736-
"category": "Gateway",
737-
"supportedAttributes": ", ".join(saved_attrs),
738-
"settableAttributes": "",
739-
"id": 1,
740-
"identifier": req[0].get("dsn"),
741-
"sdi": req[0].get("sdi"),
742-
"provider": 1,
743-
"model_number": "EQ000017",
744-
"sw_version": req[0].get("firmwareVersion"),
745-
}
746-
for attr in saved_attrs:
747-
gw[attr] = {"value": req[0].get(attr)}
748-
return gw
749-
750718
async def get_weather(self, location_id: int) -> dict[str, Any]:
751719
"""This will return the current weather like in the app
752720
https://api.hiloenergie.com/Automation/v1/api/Locations/XXXX/Weather

pyhilo/devices.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def generate_device(self, device: dict) -> HiloDevice:
9999
return dev
100100

101101
async def update(self) -> None:
102-
"""Update device list from SignalR cache + gateway from REST."""
102+
"""Update device list from SignalR cache"""
103103
# Get devices from SignalR cache (already populated by DeviceListInitialValuesReceived)
104104
cached_devices = self._api.get_device_cache(self.location_id)
105105
generated_devices = []
@@ -110,18 +110,6 @@ async def update(self) -> None:
110110
if dev not in self.devices:
111111
self.devices.append(dev)
112112

113-
# Append gateway from REST API (still available)
114-
try:
115-
gw = await self._api.get_gateway(self.location_id)
116-
LOG.debug("Generating gateway device %s", gw)
117-
gw_dev = self.generate_device(gw)
118-
generated_devices.append(gw_dev)
119-
if gw_dev not in self.devices:
120-
self.devices.append(gw_dev)
121-
except Exception as err:
122-
LOG.error("Failed to get gateway: %s", err)
123-
124-
# Now add devices from external sources (e.g. unknown source tracker)
125113
for callback in self._api._get_device_callbacks:
126114
try:
127115
cb_device = callback()

pyhilo/graphql.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,16 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
590590
return
591591

592592
if "data" in response_json:
593+
devices = (
594+
response_json["data"].get("getLocation", {}).get("devices", [])
595+
)
596+
gateways = [
597+
d for d in devices if d.get("deviceType") in ("Gateway", "Hub")
598+
]
599+
LOG.debug(
600+
"Gateway devices in getLocation response: %s",
601+
json.dumps(gateways, indent=2),
602+
)
593603
self._handle_query_result(response_json["data"])
594604

595605
async def subscribe_to_device_updated(
@@ -790,12 +800,55 @@ async def _get_access_token(self) -> str:
790800
return await self._api.async_get_access_token()
791801

792802
def _handle_query_result(self, result: Dict[str, Any]) -> None:
793-
"""This receives query results and maps them to the proper device."""
803+
"""Handle the result of the GraphQL query for location and devices."""
794804
devices_values: List[Dict[str, Any]] = result["getLocation"]["devices"]
805+
806+
for raw_device in devices_values:
807+
if raw_device.get("deviceType") in ("Gateway", "Hub"):
808+
if self._devices.find_device(1) is None:
809+
gw = self._build_gateway_dict(raw_device)
810+
LOG.debug("Creating gateway device from GraphQL: %s", gw)
811+
gw_dev = self._devices.generate_device(gw)
812+
if gw_dev not in self._devices.devices:
813+
self._devices.devices.append(gw_dev)
814+
795815
attributes = self.mapper.map_query_values(devices_values)
796816
self._devices.parse_values_received(attributes)
797817

818+
def _build_gateway_dict(self, raw_device: Dict[str, Any]) -> Dict[str, Any]:
819+
"""Build a dictionary representing the gateway device from raw GraphQL data."""
820+
hilo_id = raw_device.get("hiloId", "")
821+
parts = hilo_id.split(":")
822+
mac = parts[3] if len(parts) > 3 else None
823+
if mac is None:
824+
LOG.warning("Unable to extract MAC from hiloId: %s", hilo_id)
825+
826+
connection_status = raw_device.get("connectionStatus")
827+
return {
828+
"name": "Hilo Gateway",
829+
"type": "Gateway",
830+
"category": "Gateway",
831+
"id": 1,
832+
"identifier": mac,
833+
"sdi": mac,
834+
"provider": 1,
835+
"model_number": "EQ000017",
836+
"sw_version": raw_device.get("controllerSoftwareVersion"),
837+
"supportedAttributes": "zigBeePairingActivated, zigBeeChannel, firmwareVersion, onlineStatus, lastStatusTime, disconnected",
838+
"settableAttributes": "",
839+
"Disconnected": {"value": connection_status != "CONNECTED"},
840+
"zigBeePairingActivated": {
841+
"value": bool(raw_device.get("zigBeePairingModeEnhanced"))
842+
},
843+
"zigBeeChannel": {"value": raw_device.get("zigBeeChannel")},
844+
"firmwareVersion": {"value": raw_device.get("controllerSoftwareVersion")},
845+
"onlineStatus": {"value": connection_status},
846+
"lastStatusTime": {"value": raw_device.get("lastConnectionTime")},
847+
"disconnected": {"value": connection_status != "CONNECTED"},
848+
}
849+
798850
def _handle_device_subscription_result(self, result: Dict[str, Any]) -> str:
851+
"""Handle the result of the GraphQL subscription for device updates."""
799852
device_value: Dict[str, Any] = result["onAnyDeviceUpdated"]["device"]
800853
attributes = self.mapper.map_device_subscription_values(device_value)
801854
updated_device = self._devices.parse_values_received(attributes)
@@ -804,6 +857,7 @@ def _handle_device_subscription_result(self, result: Dict[str, Any]) -> str:
804857
return str(device_value.get("hiloId"))
805858

806859
def _handle_location_subscription_result(self, result: Dict[str, Any]) -> str:
860+
"""Handle the result of the GraphQL subscription for location updates."""
807861
location_value: Dict[str, Any] = result["onAnyLocationUpdated"]["location"]
808862
attributes = self.mapper.map_location_subscription_values(location_value)
809863
updated_device = self._devices.parse_values_received(attributes)

0 commit comments

Comments
 (0)