Skip to content

Commit b9ad966

Browse files
Merge pull request #3 from thesilentwave/thesilentwave-patch-4
Create coordinator.py
2 parents e526e6f + cd4278d commit b9ad966

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Coordinator for TheSilentWave integration."""
2+
from datetime import timedelta
3+
import logging
4+
5+
import aiohttp
6+
7+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
8+
9+
_LOGGER = logging.getLogger(__name__)
10+
11+
class TheSilentWaveCoordinator(DataUpdateCoordinator):
12+
"""Class to manage fetching the data from the API."""
13+
14+
def __init__(self, hass, name, url, scan_interval):
15+
"""Initialize the coordinator."""
16+
self._name = name
17+
self._url = url
18+
super().__init__(
19+
hass,
20+
_LOGGER,
21+
name=name,
22+
update_interval=timedelta(seconds=scan_interval),
23+
)
24+
25+
async def _async_update_data(self):
26+
"""Fetch data from the API."""
27+
async with aiohttp.ClientSession() as session:
28+
try:
29+
async with session.get(self._url) as response:
30+
response.raise_for_status()
31+
data = await response.text()
32+
# Convert "1" to "on" and "0" to "off"
33+
return "on" if data.strip() == "1" else "off"
34+
except aiohttp.ClientError as err:
35+
raise UpdateFailed(f"Error fetching data from API: {err}") from err

0 commit comments

Comments
 (0)