Skip to content

Commit 63fac6b

Browse files
committed
Add support for filtering pollens
1 parent c61b0ba commit 63fac6b

5 files changed

Lines changed: 32 additions & 10 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ Define sensor with the following configuration parameters:<br />
2525
| :---- | :---- | :------- | :----------- |
2626
| name | **Y** | `pollen_hu` | name of the sensor |
2727
| all_dominant | **Y** | `False` | gather all dominant pollens. By the default it will show the first dominant pollen even if there are others too. |
28+
| pollens | **Y** | `` | list of pollen names to filter on. |
2829
---
2930

3031
#### Example
3132
```
3233
platform: pollen_hu
3334
name: 'Pollen adatok'
35+
pollens:
36+
- Parlagfű
37+
- Alternaria
3438
```
3539

3640
#### Lovelace UI

custom_components/pollen_hu/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Pollen Information Hungary",
44
"documentation": "https://github.com/amaximus/pollen_hu",
55
"issue_tracker": "https://github.com/amaximus/pollen_hu/issues",
6-
"version": "0.3.0",
6+
"version": "0.4.0",
77
"dependencies": [],
88
"codeowners": ["@amaximus"],
99
"iot_class": "cloud_polling",

custom_components/pollen_hu/sensor.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CONF_ALLDOMINANT = 'all_dominant'
1919
CONF_ATTRIBUTION = "Data provided by antsz.hu"
2020
CONF_NAME = 'name'
21+
CONF_POLLENS = 'pollens'
2122

2223
DEFAULT_ALLDOMINANT = False
2324
DEFAULT_ICON = 'mdi:blur'
@@ -28,17 +29,20 @@
2829
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
2930
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
3031
vol.Optional(CONF_ALLDOMINANT, default=DEFAULT_ALLDOMINANT): cv.boolean,
32+
vol.Optional(CONF_POLLENS, default=[]): vol.All(cv.ensure_list, [cv.string]),
3133
})
3234

3335
async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
3436
name = config.get(CONF_NAME)
3537
alldominant = config.get(CONF_ALLDOMINANT)
38+
pollens = config.get(CONF_POLLENS)
3639

3740
async_add_devices(
38-
[PollenHUSensor(hass, name, alldominant )],update_before_add=True)
41+
[PollenHUSensor(hass, name, alldominant, pollens )],update_before_add=True)
3942

4043
async def async_get_pdata(self):
41-
pjson = {}
44+
pjson = {"pollens": []}
45+
pjson1 = {}
4246

4347
url = 'https://efop180.antsz.hu/polleninformaciok/'
4448
async with self._session.get(url) as response:
@@ -59,18 +63,29 @@ async def async_get_pdata(self):
5963
.replace(" \"","\",\"") \
6064
.replace("\",\"name","\"},{\"name") \
6165
.replace(">", "{\"pollens\":[{") + "\"}]}"
62-
pjson = json.loads(p3)
66+
pjson1 = json.loads(p3)
67+
if len(self._pollens) != 0:
68+
i = 0
69+
for item in pjson1['pollens']:
70+
if item.get('name') not in self._pollens:
71+
continue
72+
else:
73+
pjson['pollens'].append({"name": item.get('name'),"value": item.get('value')})
74+
i += 1;
75+
else:
76+
pjson = pjson1
6377
return pjson
6478

6579
class PollenHUSensor(Entity):
6680

67-
def __init__(self, hass, name, alldominant):
81+
def __init__(self, hass, name, alldominant, pollens):
6882
"""Initialize the sensor."""
6983
self._hass = hass
7084
self._name = name
7185
self._alldominant = alldominant
7286
self._state = None
7387
self._pdata = []
88+
self._pollens = pollens
7489
self._icon = DEFAULT_ICON
7590
self._session = async_get_clientsession(hass)
7691

@@ -105,9 +120,8 @@ def extra_state_attributes(self):
105120
async def async_update(self):
106121
dominant_value = 0
107122

108-
pdata = await async_get_pdata(self)
123+
self._pdata = await async_get_pdata(self)
109124

110-
self._pdata = pdata
111125
if 'pollens' in self._pdata:
112126
for item in self._pdata['pollens']:
113127
val = item.get('value')

info.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ Define sensor with the following configuration parameters:<br />
2525
| :---- | :---- | :------- | :----------- |
2626
| name | **Y** | `pollen_hu` | name of the sensor |
2727
| all_dominant | **Y** | `False` | gather all dominant pollens. By the default it will show the first dominant pollen even if there are others too. |
28+
| pollens | **Y** | `` | list of pollen names to filter on. |
2829
---
2930

3031
#### Example
3132
```
3233
platform: pollen_hu
3334
name: 'Pollen adatok'
35+
pollens:
36+
- Parlagfű
37+
- Alternaria
3438
```
3539

3640
#### Lovelace UI

tracker.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"pollen_hu": {
3-
"version": "0.3.0",
4-
"updated_at": "2023-06-08",
3+
"version": "0.4.0",
4+
"updated_at": "2023-07-10",
55
"visit_repo": "https://github.com/amaximus/pollen_hu",
6-
"changelog": "https://github.com/amaximus/pollen_hu/releases/0.3.0"
6+
"changelog": "https://github.com/amaximus/pollen_hu/releases/0.4.0"
77
}
88
}

0 commit comments

Comments
 (0)