Skip to content

Commit

Permalink
add support for muliple baskets and make time until human readable
Browse files Browse the repository at this point in the history
FaserF committed Jun 20, 2022
1 parent 4fd49c8 commit 52f5764
Showing 4 changed files with 56 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ This integration provides the following informations within one sensor with a re

- How many baskets are available within your distance range

- Basket ID, Description and Available until time (not in human readable time yet)
- Basket ID, Description and Available until time (human readable) and a picture link

## Installation
### 1. Using HACS (recommended way)
@@ -53,10 +53,10 @@ To use a basket URL in automations you can use the following code for example:

```yaml
message: >
There are {{ states.sensor.foodsharing_48_088588.state }} Foodsharing baskets available.
Newest one: {{ states.sensor.foodsharing_48_088588.attributes.Description }}
There are {{ states.sensor.foodsharing_latitudecoordinate.state }} foodsharing baskets available.
Newest one: {{ state_attr('sensor.foodsharing_latitudecoordinate', 'baskets')[-1]['description'] }} available until {{ state_attr('sensor.foodsharing_latitudecoordinate', 'baskets')[-1]['available until'] }}
Link: https://foodsharing.de/essenskoerbe/{{ states.sensor.foodsharing_48_088588.attributes.id }}
Link: https://foodsharing.de/essenskoerbe/{{ state_attr('sensor.foodsharing_latitudecoordinate', 'baskets')[-1]['id'] }}
```
## Bug reporting
8 changes: 5 additions & 3 deletions custom_components/foodsharing/const.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
DOMAIN = "foodsharing"
ATTRIBUTION = "Data provided by foodsharing.de api"

ATTR_ID = "ID"
ATTR_DESCRIPTION = "Description"
ATTR_UNTIL = "Available until"
ATTR_BASKETS = "baskets"
ATTR_ID = "id"
ATTR_DESCRIPTION = "description"
ATTR_UNTIL = "available until"
ATTR_PICTURE = "picture"

CONF_EMAIL = "Foodsharing.de E-Mail Adress"
CONF_PASSWORD = "Foodsharing.de Password"
62 changes: 47 additions & 15 deletions custom_components/foodsharing/sensor.py
Original file line number Diff line number Diff line change
@@ -27,9 +27,11 @@
CONF_LATITUDE_FS,
CONF_LONGITUDE_FS,
CONF_DISTANCE,
ATTR_BASKETS,
ATTR_ID,
ATTR_DESCRIPTION,
ATTR_UNTIL,
ATTR_PICTURE,

DOMAIN,
)
@@ -117,7 +119,7 @@ async def async_update(self):
response = await aiohttp_client.async_get_clientsession(self.hass).get(url)

_LOGGER.debug(f"Getting Baskets: '{response.status}' {response.text} - {response.headers}")
_LOGGER.debug(f"Fetching URL: '{url}")
_LOGGER.debug(f"Fetching URL: '{url}'")

if response.status == 200:
raw_html = await response.text()
@@ -132,15 +134,30 @@ async def async_update(self):
baskets_count = len(json_data['baskets'])
if baskets_count > 0:
#_LOGGER.debug(f"JSON first basket id: '{json_data['baskets'][0]['id']}'")
baskets = []
count = 0
for id in json_data['baskets']:
#self.attrs[ATTR_ID][count] = json_data['baskets'][count]['id'],
#self.attrs[ATTR_DESCRIPTION][count] = json_data['baskets'][count]['description'],
#self.attrs[ATTR_UNTIL][count] = json_data['baskets'][count]['until']
self.attrs[ATTR_ID] = json_data['baskets'][count]['id'],
self.attrs[ATTR_DESCRIPTION] = json_data['baskets'][count]['description'],
self.attrs[ATTR_UNTIL] = json_data['baskets'][count]['until']
#Convert Time to human readable time
json_data['baskets'][count]['until'] = datetime.fromtimestamp(json_data['baskets'][count]['until']).strftime('%c')
if not json_data['baskets'][count]['picture']:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_UNTIL: json_data['baskets'][count]['until'],
ATTR_PICTURE: f"https://foodsharing.de/images/basket/medium-{json_data['baskets'][count]['picture']}"
}
)
else:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_UNTIL: json_data['baskets'][count]['until'],
}
)
count += 1
self.attrs[ATTR_BASKETS] = baskets

self.attrs[ATTR_ATTRIBUTION] = f"last updated {self.updated.strftime('%d %b, %Y %H:%M:%S')} \n{ATTRIBUTION}"
self._state = baskets_count
@@ -162,7 +179,7 @@ async def async_update(self):
response = await aiohttp_client.async_get_clientsession(self.hass).get(url)

_LOGGER.debug(f"Getting Baskets: '{response.status}' {response.text} - {response.headers}")
_LOGGER.debug(f"Fetching URL: '{url}")
_LOGGER.debug(f"Fetching URL: '{url}'")

if response.status == 200:
raw_html = await response.text()
@@ -173,16 +190,31 @@ async def async_update(self):
baskets_count = len(json_data['baskets'])
if baskets_count > 0:
#_LOGGER.debug(f"JSON first basket id: '{json_data['baskets'][0]['id']}'")
baskets = []
count = 0
for id in json_data['baskets']:
#self.attrs[ATTR_ID][count] = json_data['baskets'][count]['id'],
#self.attrs[ATTR_DESCRIPTION][count] = json_data['baskets'][count]['description'],
#self.attrs[ATTR_UNTIL][count] = json_data['baskets'][count]['until']
self.attrs[ATTR_ID] = json_data['baskets'][count]['id'],
self.attrs[ATTR_DESCRIPTION] = json_data['baskets'][count]['description'],
self.attrs[ATTR_UNTIL] = json_data['baskets'][count]['until']
#Convert Time to human readable time
json_data['baskets'][count]['until'] = datetime.fromtimestamp(json_data['baskets'][count]['until']).strftime('%c')
if not json_data['baskets'][count]['picture']:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_UNTIL: json_data['baskets'][count]['until'],
ATTR_PICTURE: f"https://foodsharing.de/images/basket/medium-{json_data['baskets'][count]['picture']}"
}
)
else:
baskets.append(
{
ATTR_ID: json_data['baskets'][count]['id'],
ATTR_DESCRIPTION: json_data['baskets'][count]['description'],
ATTR_UNTIL: json_data['baskets'][count]['until'],
}
)
count += 1

self.attrs[ATTR_BASKETS] = baskets

self.attrs[ATTR_ATTRIBUTION] = f"last updated {self.updated.strftime('%d %b, %Y %H:%M:%S')} \n{ATTRIBUTION}"
self._state = baskets_count
self._available = True
Binary file modified images/sensor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 52f5764

Please sign in to comment.