Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
FaserF committed Jul 4, 2022
1 parent a2650e3 commit 43e952d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Go to Configuration -> Integrations and click on "add integration". Then search
### Basket URL
To use a basket URL in automations you can use the following code for example:

```yaml
Link: https://foodsharing.de/essenskoerbe/{{ state_attr('sensor.foodsharing_latitude', 'baskets')[0]['id'] }}
```
A full automation example for HA would be:
```yaml
message: >
{% if is_state('sensor.foodsharing_latitude', '1') %}
Expand All @@ -65,15 +71,19 @@ There is {{ states.sensor.foodsharing_latitude.state }} foodsharing basket avail
There are {{ states.sensor.foodsharing_latitude.state }} foodsharing baskets available.
{% endif %}

Newest one: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[-1]['description'] }}
Newest one: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[0]['description'] }}

------------

Available until: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[-1]['available until'] }}
Available until: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[0]['available until'] }}

Adress: {{ state_attr('sensor.foodsharing_48_076690', 'baskets')[0]['adress'] }}

Picture: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[-1]['picture'] }}
{% if not state_attr('sensor.foodsharing_48_076690', 'baskets')[0]['picture'] == '' %}
Picture: {{ state_attr('sensor.foodsharing_48_076690', 'baskets')[0]['picture'] }}
{% endif %}

Link: https://foodsharing.de/essenskoerbe/{{ state_attr('sensor.foodsharing_latitude', 'baskets')[-1]['id'] }}
Link: https://foodsharing.de/essenskoerbe/{{ state_attr('sensor.foodsharing_latitude', 'baskets')[0]['id'] }}
```
## Bug reporting
Expand Down
2 changes: 0 additions & 2 deletions custom_components/foodsharing/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
CONF_LATITUDE_FS,
CONF_LONGITUDE_FS,
CONF_DISTANCE,
#CONF_GOOLEMAPS_API,
CONF_SCAN_INTERVAL,
#DOMAIN,
)
Expand Down Expand Up @@ -53,7 +52,6 @@ async def async_step_user(self, user_input=None):
vol.Required(CONF_DISTANCE, default=7): cv.positive_int,
vol.Required(CONF_EMAIL, default="[email protected]"): str,
vol.Required(CONF_PASSWORD, default="mySecretPassword"): str,
#vol.Required(CONF_GOOLEMAPS_API, default="false"): str,
vol.Required(CONF_SCAN_INTERVAL, default=30): cv.positive_int,
},
)
Expand Down
1 change: 0 additions & 1 deletion custom_components/foodsharing/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
CONF_LATITUDE_FS = "Latitude Coordinate"
CONF_LONGITUDE_FS = "Longitude Coordinate"
CONF_DISTANCE = "Distance in km"
#CONF_GOOLEMAPS_API = "Google Maps API Key to resolve coordinates"
CONF_SCAN_INTERVAL = "Scan interval in minutes"
16 changes: 5 additions & 11 deletions custom_components/foodsharing/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import re
import json
from typing import Any, Callable, Dict, Optional
#import geopy
#from geopy.geocoders import GoogleV3
#from geopy.geocoders import Nominatim
#from geopy.extra.rate_limiter import RateLimiter

import async_timeout

Expand All @@ -30,7 +26,6 @@
CONF_LATITUDE_FS,
CONF_LONGITUDE_FS,
CONF_DISTANCE,
#CONF_GOOLEMAPS_API,
CONF_SCAN_INTERVAL,
ATTR_BASKETS,
ATTR_ID,
Expand Down Expand Up @@ -67,13 +62,12 @@ class FoodsharingSensor(Entity):
def __init__(self, config, hass: HomeAssistantType):
super().__init__()

#self.update_interval=timedelta(minutes=config[CONF_SCAN_INTERVAL]),
self.update_interval=timedelta(minutes=config[CONF_SCAN_INTERVAL]),
self.email = config[CONF_EMAIL]
self.password = config[CONF_PASSWORD]
self.latitude_fs = config[CONF_LATITUDE_FS]
self.longitude_fs = config[CONF_LONGITUDE_FS]
self.distance = config[CONF_DISTANCE]
#self.gmapsapi = config[CONF_GOOLEMAPS_API]
self.hass = hass
self.attrs: Dict[str, Any] = {CONF_LONGITUDE_FS: self.longitude_fs}
self.updated = datetime.now()
Expand Down Expand Up @@ -168,13 +162,13 @@ async def async_update(self):
("format", "geojson"),
)
response_nominatim = await aiohttp_client.async_get_clientsession(self.hass).get(NOMINATIM_URL, params=params, headers=headers)
_LOGGER.debug(f"Nominatim Request: '{params}' '{response_nominatim.status}' {response_nominatim.text} - {response_nominatim.headers}")
#_LOGGER.debug(f"Nominatim Request: '{params}' '{response_nominatim.status}' {response_nominatim.text} - {response_nominatim.headers}")

if response.status == 200:
raw_html_nominatim = await response_nominatim.text()
json_data_nominatim = json.loads(raw_html_nominatim)
location_human_readable = json_data_nominatim['features'][0]['properties']['display_name']
_LOGGER.debug(f"Nominatim Answer: '{json_data_nominatim}'")
#_LOGGER.debug(f"Nominatim Answer: '{json_data_nominatim}'")
else:
location_human_readable = f"Lat: {json_data['baskets'][count]['lat']} / Long: {json_data['baskets'][count]['lon']}"
except:
Expand Down Expand Up @@ -260,13 +254,13 @@ async def async_update(self):
("format", "geojson"),
)
response_nominatim = await aiohttp_client.async_get_clientsession(self.hass).get(NOMINATIM_URL, params=params, headers=headers)
_LOGGER.debug(f"Nominatim Request: '{params}' '{response_nominatim.status}' {response_nominatim.text} - {response_nominatim.headers}")
#_LOGGER.debug(f"Nominatim Request: '{params}' '{response_nominatim.status}' {response_nominatim.text} - {response_nominatim.headers}")

if response.status == 200:
raw_html_nominatim = await response_nominatim.text()
json_data_nominatim = json.loads(raw_html_nominatim)
location_human_readable = json_data_nominatim['features'][0]['properties']['display_name']
_LOGGER.debug(f"Nominatim Answer: '{json_data_nominatim}'")
#_LOGGER.debug(f"Nominatim Answer: '{json_data_nominatim}'")
else:
location_human_readable = f"Lat: {json_data['baskets'][count]['lat']} / Long: {json_data['baskets'][count]['lon']}"
except:
Expand Down

0 comments on commit 43e952d

Please sign in to comment.