Skip to content

Commit

Permalink
Add handling for Christmas Trees and separate recycling collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
JRInge committed Jul 16, 2024
1 parent b6d4791 commit d796751
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 29 deletions.
139 changes: 114 additions & 25 deletions packages/recycling/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,93 @@ recycling_template:
sensor:
- name: 'Green bin'
icon: 'mdi:trash-can-outline'
availability: |-
{{ state_attr("sensor.bin_day", "bins") is not none
and state_attr("sensor.bin_day", "bins")["Green"]["last"]
!= "Unknown" }}
state: >
{% set colour = 'Green' %}
{% set last_ts =
state_attr('sensor.bin_day', 'bins')[colour]['last_date']
{% if state_attr("sensor.bin_day", "bins")[colour]["last"]
!= "Unknown" %}
{% set last_ts =
state_attr('sensor.bin_day', 'bins')[colour]['last']
| as_timestamp() -%}
{% set next_ts =
state_attr('sensor.bin_day', 'bins')[colour]['next_date']
{% set next_ts =
state_attr('sensor.bin_day', 'bins')[colour]['next']
| as_timestamp() -%}
{% if next_ts == None %}
Unknown
{%- else %}
{% set now_ts = now() | as_timestamp %}
{% if next_ts - now_ts < 0 -%}
{% if next_ts | timestamp_custom('%d', True)
== now_ts | timestamp_custom('%d', True) %}
{% if next_ts == None %}
Unknown
{%- else %}
{% set now_ts = now() | as_timestamp %}
{% if next_ts - now_ts < 0 -%}
{% if next_ts | timestamp_custom('%d', True)
== now_ts | timestamp_custom('%d', True) %}
Today
{%- else %}
{%- else %}
Overdue
{%- endif %}
{%- elif now_ts - last_ts < 86400 %}
Today
{%- elif next_ts - now_ts < 86400 -%}
Tomorrow
{%- elif next_ts - now_ts < 518400 -%}
{{ next_ts | timestamp_custom('%A', True) }}
{%- else -%}
{{ next_ts | timestamp_custom('%a %b %-d', True) }}
{%- endif %}
{%- elif now_ts - last_ts < 86400 %}
Today
{%- elif next_ts - now_ts < 86400 -%}
Tomorrow
{%- elif next_ts - now_ts < 518400 -%}
{{ next_ts | timestamp_custom('%A', True) }}
{%- else -%}
{{ next_ts | timestamp_custom('%a %b %-d', True) }}
{%- endif %}
{%- endif %}
{%- else -%}
unknown
{%- endif %}
- name: 'Black bin'
icon: 'mdi:trash-can'
availability: |-
{{ state_attr("sensor.bin_day", "bins") is not none
and state_attr("sensor.bin_day", "bins")["Black"]["last"]
!= "Unknown" }}
state: >
{% set colour = 'Black' %}
{% if state_attr("sensor.bin_day", "bins")[colour]["last"]
!= "Unknown" %}
{% set last_ts =
state_attr('sensor.bin_day', 'bins')[colour]['last']
| as_timestamp() -%}
{% set next_ts =
state_attr('sensor.bin_day', 'bins')[colour]['next']
| as_timestamp() -%}
{% if next_ts == None %}
Unknown
{%- else %}
{% set now_ts = now() | as_timestamp %}
{% if next_ts - now_ts < 0 -%}
{% if next_ts | timestamp_custom('%d', True)
== now_ts | timestamp_custom('%d', True) %}
Today
{%- else %}
Overdue
{%- endif %}
{%- elif now_ts - last_ts < 86400 %}
Today
{%- elif next_ts - now_ts < 86400 -%}
Tomorrow
{%- elif next_ts - now_ts < 518400 -%}
{{ next_ts | timestamp_custom('%A', True) }}
{%- else -%}
{{ next_ts | timestamp_custom('%a %b %-d', True) }}
{%- endif %}
{%- endif %}
{%- else -%}
unknown
{%- endif %}
- name: 'Recycling box'
icon: 'mdi:recycle'
state: >
{% set colour = 'Box' %}
{% set last_ts =
state_attr('sensor.bin_day', 'bins')[colour]['last_date']
state_attr('sensor.bin_day', 'bins')[colour]['last']
| as_timestamp() -%}
{% set next_ts =
state_attr('sensor.bin_day', 'bins')[colour]['next_date']
state_attr('sensor.bin_day', 'bins')[colour]['next']
| as_timestamp() -%}
{% if next_ts == None %}
Unknown
Expand All @@ -76,14 +125,54 @@ recycling_template:
{{ next_ts | timestamp_custom('%a %b %-d', True) }}
{%- endif %}
{%- endif %}
- name: 'Tree recycling'
icon: 'mdi:pine-tree'
availability: |-
{{ state_attr("sensor.bin_day", "bins") is not none
and state_attr("sensor.bin_day", "bins")["Tree"]["last"]
!= "Unknown" }}
state: >
{% set colour = 'Tree' %}
{% if state_attr("sensor.bin_day", "bins")[colour]["last"]
!= "Unknown" %}
{% set last_ts =
state_attr('sensor.bin_day', 'bins')[colour]['last']
| as_timestamp() -%}
{% set next_ts =
state_attr('sensor.bin_day', 'bins')[colour]['next']
| as_timestamp() -%}
{% if next_ts == None %}
Unknown
{%- else %}
{% set now_ts = now() | as_timestamp %}
{% if next_ts - now_ts < 0 -%}
{% if next_ts | timestamp_custom('%d', True)
== now_ts | timestamp_custom('%d', True) %}
Today
{%- else %}
Overdue
{%- endif %}
{%- elif now_ts - last_ts < 86400 %}
Today
{%- elif next_ts - now_ts < 86400 -%}
Tomorrow
{%- elif next_ts - now_ts < 518400 -%}
{{ next_ts | timestamp_custom('%A', True) }}
{%- else -%}
{{ next_ts | timestamp_custom('%a %b %-d', True) }}
{%- endif %}
{%- endif %}
{%- else -%}
unknown
{%- endif %}
- name: greeen_or_black
icon: 'mdi:trash-can'
state: >
{%- set next_gr =
state_attr('sensor.bin_day', 'bins')['Green']['next_date']
state_attr('sensor.bin_day', 'bins')['Green']['next']
| as_timestamp() -%}
{%- set next_bk =
state_attr('sensor.bin_day', 'bins')['Black']['next_date']
state_attr('sensor.bin_day', 'bins')['Black']['next']
| as_timestamp() -%}
{%- if next_gr < next_bk -%}
Green
Expand Down
16 changes: 12 additions & 4 deletions scripts/fetch_bin_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def parse(r: Dict[str, Any]) -> Dict[str, Dict[str, str]]:
{
'name': 'Green',
'description': '240L Garden Waste Bin'
},
{
'name': 'Box',
'description': '55L Green Recycling Box'
},
{
'name': 'Tree',
'description': 'Christmas Tree'
}
]

Expand All @@ -53,13 +61,13 @@ def find_collection(description: str) -> Dict[str, str]:
if container != []:
collection = list(container)[0]['collection'][0]
return {
'last_date': check_date(collection['lastCollectionDate'][:10]),
'next_date': check_date(collection['nextCollectionDate'][:10])
'last': check_date(collection['lastCollectionDate'][:10]),
'next': check_date(collection['nextCollectionDate'][:10])
}
else:
return {
'last_date': 'Unknown',
'next_date': 'Unknown'
'last': 'Unknown',
'next': 'Unknown'
}

return {bin['name']: find_collection(bin['description']) for bin in bins}
Expand Down

0 comments on commit d796751

Please sign in to comment.