From 7b1f35238ddd8722fd914b452ae186cf9e9bfefb Mon Sep 17 00:00:00 2001 From: lukas-heilgenbrunner Date: Wed, 1 Oct 2025 17:54:37 +0200 Subject: [PATCH] create example --- .../temp_station_integration/__init__.py | 4 ++ .../temp_station_integration/manifest.json | 7 +++ .../temp_station_integration/sensor.py | 56 +++++++++++++++++++ main.py | 16 ++++++ 4 files changed, 83 insertions(+) create mode 100644 custom_components/temp_station_integration/__init__.py create mode 100644 custom_components/temp_station_integration/manifest.json create mode 100644 custom_components/temp_station_integration/sensor.py create mode 100644 main.py diff --git a/custom_components/temp_station_integration/__init__.py b/custom_components/temp_station_integration/__init__.py new file mode 100644 index 0000000..50f0447 --- /dev/null +++ b/custom_components/temp_station_integration/__init__.py @@ -0,0 +1,4 @@ +DOMAIN = "temp_station_integration" + +async def async_setup(hass, config): + return True \ No newline at end of file diff --git a/custom_components/temp_station_integration/manifest.json b/custom_components/temp_station_integration/manifest.json new file mode 100644 index 0000000..67c4109 --- /dev/null +++ b/custom_components/temp_station_integration/manifest.json @@ -0,0 +1,7 @@ +{ + "domain": "temp_station_integration", + "name": "Temp Station integration", + "version": "0.1", + "requirements": [], + "codeowners": [] +} \ No newline at end of file diff --git a/custom_components/temp_station_integration/sensor.py b/custom_components/temp_station_integration/sensor.py new file mode 100644 index 0000000..1f333c6 --- /dev/null +++ b/custom_components/temp_station_integration/sensor.py @@ -0,0 +1,56 @@ +import logging +import aiohttp +import async_timeout +from datetime import timedelta +from homeassistant.components.sensor import SensorEntity +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, + UpdateFailed, +) + +_LOGGER = logging.getLogger(__name__) +SCAN_INTERVAL = timedelta(minutes=1) # refresh every 1 min + +API_URL = "https://example.com/api" # <-- change to your API +POST_PAYLOAD = {"key": "value"} # <-- change payload +JSON_KEY = "result" # <-- key to extract + +async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): + session = aiohttp.ClientSession() + + async def async_update_data(): + try: + with async_timeout.timeout(10): + async with session.post(API_URL, json=POST_PAYLOAD) as resp: + if resp.status != 200: + raise UpdateFailed(f"Error {resp.status}") + data = await resp.json() + return data + except Exception as err: + raise UpdateFailed(f"Error communicating with API: {err}") + + coordinator = DataUpdateCoordinator( + hass, + _LOGGER, + name="my_api_integration", + update_method=async_update_data, + update_interval=SCAN_INTERVAL, + ) + + await coordinator.async_config_entry_first_refresh() + async_add_entities([MyAPISensor(coordinator)], True) + + +class MyAPISensor(CoordinatorEntity, SensorEntity): + def __init__(self, coordinator): + super().__init__(coordinator) + self._attr_name = "My API Sensor" + self._attr_unique_id = "my_api_sensor" + + @property + def native_value(self): + data = self.coordinator.data + if data is None: + return None + return data.get(JSON_KEY) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..5596b44 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/