diff --git a/README.md b/README.md index 09bb1a1..50d96e5 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/custom_components/foodsharing/const.py b/custom_components/foodsharing/const.py index a1b2802..fec98e0 100644 --- a/custom_components/foodsharing/const.py +++ b/custom_components/foodsharing/const.py @@ -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" diff --git a/custom_components/foodsharing/sensor.py b/custom_components/foodsharing/sensor.py index 8d65e21..bc4cafd 100644 --- a/custom_components/foodsharing/sensor.py +++ b/custom_components/foodsharing/sensor.py @@ -33,6 +33,7 @@ ATTR_UNTIL, ATTR_PICTURE, ATTR_ADRESS, + ATTR_MAPS_LINK, DOMAIN, ) @@ -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 = { @@ -168,12 +168,15 @@ 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( @@ -181,6 +184,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'] } ) @@ -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}" } @@ -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 = { @@ -260,12 +264,15 @@ 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( @@ -273,6 +280,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'] } ) @@ -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}" } )