Skip to content

Commit

Permalink
Fix for broken events API (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustas2 committed Sep 19, 2023
1 parent f02be0b commit 5292676
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
5 changes: 4 additions & 1 deletion custom_components/eldes_alarm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
DATA_COORDINATOR,
DATA_DEVICES,
DEFAULT_SCAN_INTERVAL,
DEFAULT_EVENTS_LIST_SIZE,
CONF_EVENTS_LIST_SIZE,
DOMAIN
)
from .core.eldes_cloud import EldesCloud
Expand Down Expand Up @@ -95,6 +97,7 @@ async def async_update_data():

async def async_get_devices(hass: HomeAssistant, entry: ConfigEntry, eldes_client: EldesCloud):
"""Fetch data from Eldes API."""
events_list_size = entry.options.get(CONF_EVENTS_LIST_SIZE, DEFAULT_EVENTS_LIST_SIZE)

await eldes_client.renew_token()

Expand All @@ -106,7 +109,7 @@ async def async_get_devices(hass: HomeAssistant, entry: ConfigEntry, eldes_clien
device["partitions"] = await eldes_client.get_device_partitions(device["imei"])
device["outputs"] = await eldes_client.get_device_outputs(device["imei"])
device["temp"] = await eldes_client.get_temperatures(device["imei"])
device["events"] = await eldes_client.get_events(device["imei"])
device["events"] = await eldes_client.get_events(events_list_size)

hass.data[DOMAIN][entry.entry_id][DATA_DEVICES] = devices

Expand Down
6 changes: 5 additions & 1 deletion custom_components/eldes_alarm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, CONF_SCAN_INTERVAL

from .core.eldes_cloud import EldesCloud
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL
from .const import DOMAIN, DEFAULT_SCAN_INTERVAL, DEFAULT_EVENTS_LIST_SIZE, CONF_EVENTS_LIST_SIZE

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,6 +101,10 @@ async def async_step_init(self, user_input=None):
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
): int,
vol.Required(
CONF_EVENTS_LIST_SIZE,
default=self.config_entry.options.get(CONF_EVENTS_LIST_SIZE, DEFAULT_EVENTS_LIST_SIZE)
): int,
}
)
)
2 changes: 2 additions & 0 deletions custom_components/eldes_alarm/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
DATA_CLIENT = "eldes_client"
DATA_COORDINATOR = "coordinator"
DATA_DEVICES = "devices"
CONF_EVENTS_LIST_SIZE = "events_list_size"

DEFAULT_SCAN_INTERVAL = 30
DEFAULT_EVENTS_LIST_SIZE = 10

API_URL = "https://cloud.eldesalarms.com:8083/api/"

Expand Down
12 changes: 9 additions & 3 deletions custom_components/eldes_alarm/core/eldes_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,17 @@ async def get_temperatures(self, imei):

return temperatures

async def get_events(self, imei):
async def get_events(self, size):
"""Gets device events."""
url = f"{API_URL}{API_PATHS['DEVICE']}event/list?imei={imei}"
data = {
"": "",
"size": size,
"start": 0
}

response = await self._api_call(url, "POST", {})
url = f"{API_URL}{API_PATHS['DEVICE']}event/list"

response = await self._api_call(url, "POST", data)
result = await response.json()
events = result.get("eventDetails", [])

Expand Down
3 changes: 2 additions & 1 deletion custom_components/eldes_alarm/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"title": "Configure Eldes integration",
"description": "Update the scan interval to poll for data more often.",
"data": {
"scan_interval": "Scan Interval (seconds)"
"scan_interval": "Scan Interval (seconds)",
"events_list_size": "Events list size"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/eldes_alarm/translations/lt.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"title": "Eldes integracijos nustatymai",
"description": "Pakeiskite atnaujinimo intervalą, norėdami dažniau arba rečiau atnaujinti duomenis.",
"data": {
"scan_interval": "Atnaujinimo intervalas (sekundės)"
"scan_interval": "Atnaujinimo intervalas (sekundės)",
"events_list_size": "Įvykių sąrašo ilgis"
}
}
}
Expand Down

0 comments on commit 5292676

Please sign in to comment.