Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files operation async #436

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
4 changes: 2 additions & 2 deletions custom_components/hilo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"documentation": "https://github.com/dvd-dev/hilo",
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/dvd-dev/hilo/issues",
"requirements": ["python-hilo>=2024.4.1"],
"version": "2024.6.1"
"requirements": ["python-hilo>=2024.6.1"],
"version": "2024.6.2"
}
20 changes: 11 additions & 9 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime, timedelta, timezone
from os.path import isfile

import aiofiles
from homeassistant.components.integration.sensor import METHOD_LEFT, IntegrationSensor
from homeassistant.components.sensor import (
SensorDeviceClass,
Expand Down Expand Up @@ -587,7 +588,7 @@ def __init__(self, hilo, device, scan_interval):
self._history_state_yaml: str = "hilo_eventhistory_state.yaml"
self.scan_interval = timedelta(seconds=REWARD_SCAN_INTERVAL)
self._state = 0
self._history = self._load_history()
self._history = []
self.async_update = Throttle(self.scan_interval)(self._async_update)

@property
Expand Down Expand Up @@ -619,7 +620,7 @@ async def async_added_to_hass(self):
async def _async_update(self):
seasons = await self._hilo._api.get_seasons(self._hilo.devices.location_id)
if seasons:
current_history = self._history
current_history = await self._load_history()
new_history = []

for idx, season in enumerate(seasons):
Expand Down Expand Up @@ -666,20 +667,21 @@ async def _async_update(self):
season["events"] = events
new_history.append(season)
self._history = new_history
self._save_history(new_history)
await self._save_history(new_history)

def _load_history(self) -> list:
async def _load_history(self) -> list:
history: list = []
if isfile(self._history_state_yaml):
with open(self._history_state_yaml) as yaml_file:
async with aiofiles.open(self._history_state_yaml, mode="r") as yaml_file:
LOG.debug("Loading history state from yaml")
history = yaml.load(yaml_file, Loader=yaml.Loader)
content = await yaml_file.read()
history = yaml.load(content, Loader=yaml.Loader)
return history

def _save_history(self, history: list):
with open(self._history_state_yaml, "w") as yaml_file:
async def _save_history(self, history: list):
async with aiofiles.open(self._history_state_yaml, mode="w") as yaml_file:
LOG.debug("Saving history state to yaml file")
yaml.dump(history, yaml_file, Dumper=yaml.RoundTripDumper)
await yaml_file.write(yaml.dump(history, Dumper=yaml.RoundTripDumper))


class HiloChallengeSensor(HiloEntity, RestoreEntity, SensorEntity):
Expand Down