Skip to content

Commit

Permalink
new attribute for google maps link
Browse files Browse the repository at this point in the history
  • Loading branch information
FaserF committed Jul 7, 2022
1 parent 43e952d commit ee24cda
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ This integration provides the following informations with a refresh rate of 30 m

Sensors:

- How many baskets are available within your distance range
- sensor.foodsharing_latitudeCoordinate: How many baskets are available within your distance range

Sensor Attributes:

- Basket ID, Description, adress, available until time and a picture link
- id: Basket ID
- description: Description text about the basket
- adress: Human readable detailed adress, fetched from coordinates
- maps: Google Maps Link to basket
- available until: time until basket creator says it could be available
- picture: link

## Installation
### 1. Using HACS (recommended way)
Expand Down Expand Up @@ -77,13 +82,15 @@ Newest one: {{ state_attr('sensor.foodsharing_latitude', 'baskets')[0]['descript

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

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

{% 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')[0]['id'] }}

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

Google Maps Link: {{ state_attr('sensor.foodsharing_48_076690', 'baskets')[0]['maps'] }}
```
## Bug reporting
Expand Down
3 changes: 3 additions & 0 deletions custom_components/foodsharing/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
ATTR_UNTIL = "available until"
ATTR_PICTURE = "picture"
ATTR_ADRESS = "adress"
ATTR_LATITUDE = "latitude"
ATTR_LONGITUDE = "longitude"
ATTR_MAPS_LINK = "maps"

CONF_EMAIL = "Foodsharing.de E-Mail Adress"
CONF_PASSWORD = "Foodsharing.de Password"
Expand Down
25 changes: 17 additions & 8 deletions custom_components/foodsharing/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
ATTR_UNTIL,
ATTR_PICTURE,
ATTR_ADRESS,
ATTR_MAPS_LINK,

DOMAIN,
)
Expand Down Expand Up @@ -149,7 +150,6 @@ async def async_update(self):
picture = json_data['baskets'][count]['picture']

#Convert to human readable location adress
location_human_readable = ""
if json_data['baskets'][count]['lat']:
try:
headers = {
Expand All @@ -168,19 +168,23 @@ async def async_update(self):
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']
maps_link = f"https://www.google.de/maps/place/{json_data_nominatim['features'][0]['properties']['address']['road']}+{json_data_nominatim['features'][0]['properties']['address']['house_number']}+{json_data_nominatim['features'][0]['properties']['address']['postcode']}+{json_data_nominatim['features'][0]['properties']['address']['city']}"
#_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']}"
location_human_readable = "unavailable"
maps_link = "unavailable"
except:
location_human_readable = f"Lat: {json_data['baskets'][count]['lat']} / Long: {json_data['baskets'][count]['lon']}"
_LOGGER.debug(f"Error on recieving human readable adress via OpenMap API.")
location_human_readable = "unavailable"
maps_link = "unavailable"
_LOGGER.debug(f"Error on recieving human readable adress via OpenMap API for {json_data['baskets'][count]['lat']}, {json_data['baskets'][count]['lat']}.")

if not picture:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_ADRESS: location_human_readable,
ATTR_MAPS_LINK: maps_link,
ATTR_UNTIL: json_data['baskets'][count]['until']
}
)
Expand All @@ -190,6 +194,7 @@ async def async_update(self):
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_ADRESS: location_human_readable,
ATTR_MAPS_LINK: maps_link,
ATTR_UNTIL: json_data['baskets'][count]['until'],
ATTR_PICTURE: f"https://foodsharing.de/images/basket/medium-{picture}"
}
Expand Down Expand Up @@ -241,7 +246,6 @@ async def async_update(self):
picture = json_data['baskets'][count]['picture']

#Convert to human readable location adress
location_human_readable = ""
if json_data['baskets'][count]['lat']:
try:
headers = {
Expand All @@ -260,19 +264,23 @@ async def async_update(self):
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']
maps_link = f"https://www.google.de/maps/place/{json_data_nominatim['features'][0]['properties']['address']['road']}+{json_data_nominatim['features'][0]['properties']['address']['house_number']}+{json_data_nominatim['features'][0]['properties']['address']['postcode']}+{json_data_nominatim['features'][0]['properties']['address']['city']}"
#_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']}"
location_human_readable = "unavailable"
maps_link = "unavailable"
except:
location_human_readable = f"Lat: {json_data['baskets'][count]['lat']} / Long: {json_data['baskets'][count]['lon']}"
_LOGGER.debug(f"Error on recieving human readable adress via OpenMap API.")
location_human_readable = "unavailable"
maps_link = "unavailable"
_LOGGER.debug(f"Error on recieving human readable adress via OpenMap API for {json_data['baskets'][count]['lat']}, {json_data['baskets'][count]['lat']}.")

if not picture:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_ADRESS: location_human_readable,
ATTR_MAPS_LINK: maps_link,
ATTR_UNTIL: json_data['baskets'][count]['until']
}
)
Expand All @@ -283,6 +291,7 @@ async def async_update(self):
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_ADRESS: location_human_readable,
ATTR_UNTIL: json_data['baskets'][count]['until'],
ATTR_MAPS_LINK: maps_link,
ATTR_PICTURE: f"https://foodsharing.de/images/basket/medium-{picture}"
}
)
Expand Down

0 comments on commit ee24cda

Please sign in to comment.