Skip to content

Commit

Permalink
Add support for setting use_timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
badguy99 committed Mar 28, 2020
1 parent b69c844 commit c1791c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ octo_block_1hour:
class: OctoBlock
region: H
hour: 1
use_timezone: False

octo_block_90minutes:
module: octoblock
class: OctoBlock
region: H
hour: 1.5
start_period: now
use_timezone: True
```
The module and class sections need to remain as above, other sections should be changed as required.
Expand All @@ -38,6 +40,7 @@ The module and class sections need to remain as above, other sections should be
| region | Yes | H |
| hour | Yes | 1 |
| start_period | Yes | today |
| use_timezone | Yes | True |
You can have multiple blocks with different time periods (`hour` setting) or starting points (`start_period` setting) as needed. It will work with whole hour or half hour blocks in the `hour` setting.

Expand All @@ -59,6 +62,8 @@ Using `now` start_period this has turned on and off a few times within the day a

Using `today` start_period this has only turned on once during the day

`use_timezone` can be set to True or False, and defaults to False, it allows you to specify if the date/time should be displayed in UTC (False), or using Europe/London (True) as the timezone. For example, `2020-03-29T02:00:00Z` or `2020-03-29T03:00:00 BST` respectively.

### Home Assistant Automation

The created start time sensors can then be used to trigger automations within Home Assistant.
Expand Down
10 changes: 10 additions & 0 deletions apps/octoblock/octoblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import requests
import json
import datetime
import dateutil.parser
import pytz


class OctoBlock(hass.Hass):
Expand All @@ -15,6 +17,8 @@ def get_best_period_and_cost(self, kwargs):
region = self.args.get('region', 'H')
start_period = self.args.get('start_period', 'now')
start_period = str(start_period).lower()
use_timezone = self.args.get('use_timezone', False)

if start_period == 'today':
d = datetime.date.today().isoformat() + 'T00:00:00'
elif start_period == 'now':
Expand Down Expand Up @@ -57,6 +61,12 @@ def get_best_period_and_cost(self, kwargs):
for period in tariffresults:
if period[str(hours) + '_hour_average'] == self.minprice:
self.time = period[u'valid_from']
if use_timezone:
fmt = '%Y-%m-%dT%H:%M:%S %Z'
greenwich = pytz.timezone('Europe/London')
date_time = dateutil.parser.parse(self.time)
local_datetime = date_time.astimezone(greenwich)
self.time = local_datetime.strftime(fmt)
self.log('Lowest priced {} hour period'.format(str(hours)) +
' starts at: {}'.format(self.time))

Expand Down

0 comments on commit c1791c6

Please sign in to comment.