Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions pyhilo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientResponseError
import backoff
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport

from pyhilo.const import (
ANDROID_CLIENT_ENDPOINT,
Expand Down Expand Up @@ -45,7 +43,6 @@
)
from pyhilo.device import DeviceAttribute, HiloDevice, get_device_attributes
from pyhilo.exceptions import InvalidCredentialsError, RequestError
from pyhilo.graphql import GraphQlHelper
from pyhilo.util.state import (
StateDict,
WebsocketDict,
Expand Down Expand Up @@ -513,22 +510,6 @@ async def get_devices(self, location_id: int) -> list[dict[str, Any]]:
devices.append(callback())
return devices

async def call_get_location_query(self, location_hilo_id: string) -> Dict[str, Any]:
access_token = await self.async_get_access_token()
transport = AIOHTTPTransport(
url="https://platform.hiloenergie.com/api/digital-twin/v3/graphql",
headers={"Authorization": f"Bearer {access_token}"},
)
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(GraphQlHelper.query_get_location())

async with client as session:
result = await session.execute(
query, variable_values={"locationHiloId": location_hilo_id}
)
LOG.info(result)
return result

async def _set_device_attribute(
self,
device: HiloDevice,
Expand Down
54 changes: 29 additions & 25 deletions pyhilo/device/graphql_value_mapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from datetime import datetime, timezone
from typing import Any, Dict, Generator, Union

from typing import Any, Dict
from pyhilo.device import DeviceReading


Expand All @@ -9,45 +7,50 @@ class GraphqlValueMapper:
A class to map GraphQL values to DeviceReading instances.
"""

def __init__(self, api: Any):
self._api = api

def map_values(self, values: Dict[str, Any]) -> list[DeviceReading]:
devices_values: list[any] = values["getLocation"]["devices"]
readings: list[DeviceReading] = []
for device in devices_values:
def map_query_values(self, values: Dict[str, Any]) -> list[Dict[str, Any]]:
readings: list[Dict[str, Any]] = []
for device in values:
if device.get("deviceType") is not None:
reading = self._map_devices_values(device)
readings.extend(reading)
return readings

def _map_devices_values(self, device: Dict[str, Any]) -> list[DeviceReading]:
def map_subscription_values(
self, device: list[Dict[str, Any]]
) -> list[Dict[str, Any]]:
readings: list[Dict[str, Any]] = []
if device.get("deviceType") is not None:
reading = self._map_devices_values(device)
readings.extend(reading)
return readings

def _map_devices_values(self, device: Dict[str, Any]) -> list[Dict[str, Any]]:
attributes: list[Dict[str, Any]] = self._map_basic_device(device)
match device["deviceType"]:
case "Tstat":
match device["deviceType"].lower():
case "tstat":
attributes.extend(self._build_thermostat(device))
case "CCE": # Water Heater
case "cce": # Water Heater
attributes.extend(self._build_water_heater(device))
case "CCR": # ChargeController
case "ccr": # ChargeController
attributes.extend(self._build_charge_controller(device))
case "HeatingFloor":
case "heatingfloor":
attributes.extend(self._build_floor_thermostat(device))
# case "LowVoltageTstat":
case "ChargingPoint":
# case "lowvoltagetstat":
case "chargingpoint":
attributes.extend(self._build_charging_point(device))
case "Meter":
case "meter":
attributes.extend(self._build_smart_meter(device))
case "Hub": # Gateway
case "hub": # Gateway
attributes.extend(self._build_gateway(device))
case "ColorBulb":
case "colorbulb":
attributes.extend(self._build_light(device))
case "Dimmer":
case "dimmer":
attributes.extend(self._build_dimmer(device))
case "Switch":
case "switch":
attributes.extend(self._build_switch(device))
case _:
pass
return self._map_to_device_reading(attributes)
return attributes

def _map_to_device_reading(
self, attributes: list[Dict[str, Any]]
Expand Down Expand Up @@ -398,7 +401,8 @@ def build_attribute(
) -> Dict[str, Any]:
return {
"hilo_id": hilo_id,
"device_attribute": self._api.dev_atts(device_attribute, "null"),
"attribute": device_attribute,
"valueType": "null",
"value": value,
"timeStampUTC": datetime.now(timezone.utc).isoformat(),
}
Expand Down
8 changes: 1 addition & 7 deletions pyhilo/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from pyhilo.const import HILO_DEVICE_TYPES, LOG
from pyhilo.device import DeviceReading, HiloDevice
from pyhilo.device.climate import Climate # noqa
from pyhilo.device.graphql_value_mapper import GraphqlValueMapper
from pyhilo.device.light import Light # noqa
from pyhilo.device.sensor import Sensor # noqa
from pyhilo.device.switch import Switch # noqa
from pyhilo.device.switch import Switch


class Devices:
Expand Down Expand Up @@ -119,8 +118,3 @@ async def async_init(self) -> None:
self.location_id = location_ids[0]
self.location_hilo_id = location_ids[1]
await self.update()
# TODO AA - trouver ou placer ça pour que ça fasse du sens, pas sûre que c'est ici
values = await self._api.call_get_location_query(self.location_hilo_id)
mapper = GraphqlValueMapper(self._api)
readings = mapper.map_values(values)
self._map_readings_to_devices(readings)
Loading
Loading