├── custom_components └── hydro_imgw │ ├── __init__.py │ ├── services.yaml │ ├── manifest.json │ └── sensor.py ├── example.png ├── .github ├── FUNDING.yml └── workflows │ ├── hassfest.yaml │ ├── hacs.yaml │ ├── automerge.yaml │ └── release.yaml ├── hacs.json ├── LICENSE └── README.md /custom_components/hydro_imgw/__init__.py: -------------------------------------------------------------------------------- 1 | """Hydro IMGW""" -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/HEAD/example.png -------------------------------------------------------------------------------- /custom_components/hydro_imgw/services.yaml: -------------------------------------------------------------------------------- 1 | reload: 2 | name: Reload 3 | description: Reload all entities of Hydro IMGW platform -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | ko_fi: piotrmachowski 2 | custom: ["buycoffee.to/piotrmachowski", "paypal.me/PiMachowski", "revolut.me/314ma"] 3 | -------------------------------------------------------------------------------- /hacs.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Hydro IMGW", 3 | "render_readme": true, 4 | "zip_release": true, 5 | "filename": "hydro_imgw.zip" 6 | } -------------------------------------------------------------------------------- /.github/workflows/hassfest.yaml: -------------------------------------------------------------------------------- 1 | name: Validate with hassfest 2 | 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | validate: 9 | runs-on: "ubuntu-latest" 10 | steps: 11 | - uses: "actions/checkout@v2" 12 | - uses: home-assistant/actions/hassfest@master 13 | -------------------------------------------------------------------------------- /.github/workflows/hacs.yaml: -------------------------------------------------------------------------------- 1 | name: Validate HACS 2 | on: 3 | push: 4 | pull_request: 5 | jobs: 6 | ci: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | name: Download repo 11 | with: 12 | fetch-depth: 0 13 | 14 | - name: HACS Action 15 | uses: hacs/action@main 16 | with: 17 | CATEGORY: integration 18 | ignore: brands -------------------------------------------------------------------------------- /custom_components/hydro_imgw/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "domain": "hydro_imgw", 3 | "name": "Hydro IMGW", 4 | "codeowners": ["@PiotrMachowski"], 5 | "dependencies": [], 6 | "documentation": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW", 7 | "iot_class": "cloud_polling", 8 | "issue_tracker": "https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/issues", 9 | "requirements": ["requests"], 10 | "version": "v0.0.0" 11 | } 12 | -------------------------------------------------------------------------------- /.github/workflows/automerge.yaml: -------------------------------------------------------------------------------- 1 | name: 'Automatically merge master -> dev' 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build: 10 | name: Automatically merge master to dev 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | name: Git checkout 15 | with: 16 | fetch-depth: 0 17 | - name: Merge master -> dev 18 | run: | 19 | git config user.name "GitHub Actions" 20 | git config user.email "PiotrMachowski@users.noreply.github.com" 21 | if (git checkout dev) 22 | then 23 | git merge --ff-only master || git merge --no-commit master 24 | git commit -m "Automatically merge master -> dev" || echo "No commit needed" 25 | git push origin dev 26 | else 27 | echo "No dev branch" 28 | fi -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Piotr Machowski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | env: 8 | COMPONENT_NAME: hydro_imgw 9 | 10 | jobs: 11 | release: 12 | name: Prepare release 13 | runs-on: ubuntu-latest 14 | permissions: 15 | contents: write 16 | id-token: write 17 | steps: 18 | - name: Download repo 19 | uses: actions/checkout@v4.2.2 20 | 21 | - name: Adjust version number 22 | shell: bash 23 | run: | 24 | version="${{ github.event.release.tag_name }}" 25 | yq e -P -o=json \ 26 | -i ".version = \"${version}\"" \ 27 | "${{ github.workspace }}/custom_components/${{ env.COMPONENT_NAME }}/manifest.json" 28 | 29 | - name: Zip ${{ env.COMPONENT_NAME }} dir 30 | run: | 31 | cd "${{ github.workspace }}/custom_components/${{ env.COMPONENT_NAME }}" 32 | zip ${{ env.COMPONENT_NAME }}.zip -r ./ 33 | 34 | 35 | - name: Upload zip to release 36 | uses: softprops/action-gh-release@v2.1.0 37 | with: 38 | files: ${{ github.workspace }}/custom_components/${{ env.COMPONENT_NAME }}/${{ env.COMPONENT_NAME }}.zip 39 | -------------------------------------------------------------------------------- /custom_components/hydro_imgw/sensor.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from datetime import timedelta 3 | 4 | import homeassistant.helpers.config_validation as cv 5 | import requests 6 | import voluptuous as vol 7 | from homeassistant.components.sensor import (PLATFORM_SCHEMA, ENTITY_ID_FORMAT, SensorEntity, SensorStateClass) 8 | from homeassistant.const import CONF_NAME, UnitOfLength, ATTR_ATTRIBUTION 9 | from homeassistant.helpers.entity import async_generate_entity_id 10 | from homeassistant.helpers.reload import async_setup_reload_service 11 | 12 | _LOGGER = logging.getLogger(__name__) 13 | 14 | SCAN_INTERVAL = timedelta(minutes=10) 15 | 16 | DEFAULT_NAME = 'Hydro IMGW' 17 | CONF_STATION_ID = "station_id" 18 | DOMAIN = "hydro_imgw" 19 | PLATFORMS = ["sensor"] 20 | ATTRIBUTION = 'Data provided by hydro.imgw.pl.' 21 | 22 | PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ 23 | vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, 24 | vol.Required(CONF_STATION_ID): cv.string 25 | }) 26 | 27 | 28 | async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): 29 | await async_setup_reload_service(hass, DOMAIN, PLATFORMS) 30 | station_id = config.get(CONF_STATION_ID) 31 | name = config.get(CONF_NAME) 32 | uid = '{}_{}'.format(DEFAULT_NAME, station_id) 33 | entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, uid, hass=hass) 34 | async_add_entities([HydroImgwSensor(entity_id, name, station_id)], True) 35 | 36 | 37 | class HydroImgwSensor(SensorEntity): 38 | def __init__(self, entity_id, name, station_id): 39 | self.entity_id = entity_id 40 | self._platform_name = name 41 | self._station_id = station_id 42 | self._data = None 43 | self._state = None 44 | 45 | @property 46 | def name(self): 47 | name = self._station_id if self._data is None else self.extra_state_attributes["name"] 48 | return '{} - {}'.format(self._platform_name, name) 49 | 50 | @property 51 | def icon(self): 52 | return "mdi:waves" 53 | 54 | @property 55 | def state(self): 56 | if self._data is not None: 57 | try: 58 | self._state = HydroImgwSensor.extractor(self._data, "status.currentState.value") 59 | except: 60 | pass 61 | return self._state 62 | 63 | @property 64 | def extra_state_attributes(self): 65 | attr_paths = { 66 | "current_date": "status.currentState.date", 67 | "previous_date": "status.previousState.date", 68 | "alarm_value": "status.alarmValue", 69 | "warning_value": "status.warningValue", 70 | "trend": "status.trend", 71 | "name": "status.description", 72 | "level": "stateCode", 73 | "river": "status.river" 74 | } 75 | attributes = {ATTR_ATTRIBUTION: ATTRIBUTION} 76 | for name, json_path in attr_paths.items(): 77 | attr_value = HydroImgwSensor.extractor(self._data, json_path) 78 | if attr_value is not None: 79 | attr_value = attr_value if ".date" not in name else datetime.datetime.fromisoformat(attr_value) 80 | attributes[name] = attr_value 81 | return attributes 82 | 83 | @property 84 | def unit_of_measurement(self): 85 | return UnitOfLength.CENTIMETERS 86 | 87 | def update(self): 88 | try: 89 | address = f"https://hydro-back.imgw.pl/station/hydro/status?id={self._station_id}" 90 | headers = { 91 | "User-Agent": "Chrome/131.0.0.0" 92 | } 93 | request = requests.get(address, timeout=240, headers=headers) 94 | if request.status_code == 200 and request.content.__len__() > 0: 95 | self._data = request.json() 96 | except: 97 | pass 98 | 99 | @staticmethod 100 | def extractor(json, path): 101 | def extractor_arr(json_obj, path_array): 102 | if path_array[0] not in json_obj: 103 | return None 104 | if len(path_array) > 1: 105 | return extractor_arr(json_obj[path_array[0]], path_array[1:]) 106 | return json_obj[path_array[0]] 107 | 108 | try: 109 | return extractor_arr(json, path.split(".")) 110 | except: 111 | return None 112 | 113 | @property 114 | def state_class(self) -> SensorStateClass: 115 | return SensorStateClass.MEASUREMENT 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![HACS Custom][hacs_shield]][hacs] 2 | [![GitHub Latest Release][releases_shield]][latest_release] 3 | [![GitHub All Releases][downloads_total_shield]][releases] 4 | [![Ko-Fi][ko_fi_shield]][ko_fi] 5 | [![buycoffee.to][buycoffee_to_shield]][buycoffee_to] 6 | [![PayPal.Me][paypal_me_shield]][paypal_me] 7 | [![Revolut.Me][revolut_me_shield]][revolut_me] 8 | 9 | 10 | 11 | [hacs_shield]: https://img.shields.io/static/v1.svg?label=HACS&message=Custom&style=popout&color=orange&labelColor=41bdf5&logo=HomeAssistantCommunityStore&logoColor=white 12 | [hacs]: https://hacs.xyz/docs/faq/custom_repositories 13 | 14 | [latest_release]: https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/releases/latest 15 | [releases_shield]: https://img.shields.io/github/release/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW.svg?style=popout 16 | 17 | [releases]: https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/releases 18 | [downloads_total_shield]: https://img.shields.io/github/downloads/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/total 19 | 20 | 21 | > [!IMPORTANT] 22 | > 23 | > Since Home Assistant 2024.6 it is recommended to use built-in [IMGW-PIB](https://home-assistant.io/integrations/imgw_pib/) integration instead. 24 | 25 | # Hydro IMGW Integration 26 | 27 | This custom integration retrieves data from hydro stations from [hydro.imgw.pl](https://hydro.imgw.pl/#map/19.5,51.5,7,true,false,0). 28 | 29 |  30 | 31 | ## Installation 32 | 33 | ### Using [HACS](https://hacs.xyz/) (recommended) 34 | 35 | This integration can be added to HACS as a [custom repository](https://hacs.xyz/docs/faq/custom_repositories): 36 | * URL: `https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW` 37 | * Category: `Integration` 38 | 39 | After adding a custom repository you can use HACS to install this integration using user interface. 40 | 41 | ### Manual 42 | 43 | To install this integration manually you have to download [*hydro_imgw.zip*](https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/releases/latest/download/hydro_imgw.zip) and extract its contents to `config/custom_components/hydro_imgw` directory: 44 | ```bash 45 | mkdir -p custom_components/hydro_imgw 46 | cd custom_components/hydro_imgw 47 | wget https://github.com/PiotrMachowski/Home-Assistant-custom-components-Hydro-IMGW/releases/latest/download/hydro_imgw.zip 48 | unzip hydro_imgw.zip 49 | rm hydro_imgw.zip 50 | ``` 51 | 52 | ## Configuration 53 | 54 | | Key | Type | Required | Value | Description | 55 | |---|---|---|---|---| 56 | | `platform` | string | true | `hydro_imgw` | Name of a platform | 57 | | `name` | string | false | | Desired name of a entity | 58 | | `station_id` | string | true | | ID of a station to monitor (a number from URL) | 59 | 60 | ## Example configuration 61 | 62 | ```yaml 63 | sensor: 64 | - platform: hydro_imgw 65 | station_id: "152210170" 66 | ``` 67 | 68 | 69 | 70 | 71 | 72 | ## Support 73 | 74 | If you want to support my work with a donation you can use one of the following platforms: 75 | 76 |
| Platform | 79 |Payment methods | 80 |Link | 81 |Comment | 82 |
|---|---|---|---|
| Ko-fi | 85 |
86 | |
89 |
90 |
91 | |
92 |
93 | |
96 |
| buycoffee.to | 99 |
100 | |
103 |
104 | |
106 | 107 | |
| PayPal | 110 |
111 | |
113 |
114 |
115 | |
116 |
117 | |
119 |
| Revolut | 122 |
123 | |
126 |
127 | |
129 |
130 | |
132 |