Skip to content

Commit

Permalink
Fix turnover_hour bug
Browse files Browse the repository at this point in the history
The turnover_parameter should now work as expected.
  • Loading branch information
remimikalsen committed Oct 8, 2021
1 parent eb04f84 commit e73b780
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
12 changes: 7 additions & 5 deletions custom_components/remiks_renovasjon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ def setup(hass, config):
streets = config[DOMAIN][CONF_STREETS]
following = config[DOMAIN][CONF_FOLLOWING]
days_notice = config[DOMAIN][CONF_DAYS_NOTICE]
turnover_hour = config[DOMAIN][CONF_TURNOVER_HOUR]

remiks_renovasjon = RemiksRenovasjon(
streets, following, days_notice
streets, following, days_notice, turnover_hour
)
hass.data[DATA_REMIKS_RENOVASJON] = remiks_renovasjon

return True


class RemiksRenovasjon:
def __init__(self, streets, following, days_notice):
def __init__(self, streets, following, days_notice, turnover_hour):
self.streets = streets
self.following = following
self.days_notice = days_notice
self.turnover_hour = turnover_hour
self._parsed_data = self._fetch_parsed_data()

def update_parsed_data(self):
needed = self._parsed_data_needs_update(self._parsed_data)
needed = self._parsed_data_needs_update(self._parsed_data, self.turnover_hour)
if needed:
self._parsed_data = self._fetch_parsed_data()

Expand Down Expand Up @@ -106,15 +108,15 @@ def _fetch_parsed_data(self):
return parsed_data

@staticmethod
def _parsed_data_needs_update(parsed_data):
def _parsed_data_needs_update(parsed_data, turnover_hour):

for item in parsed_data:
entity_code, _, _, _, next_date, _ = item

if next_date is None:
_LOGGER.info("No data for " + entity_code + ". Refreshing data.")
return True
if next_date.date() < datetime.today().date() or (next_date.date() == datetime.today().date() and datetime.today().time().hour >= CONF_TURNOVER_HOUR):
if next_date.date() < datetime.today().date() or (next_date.date() == datetime.today().date() and datetime.today().time().hour >= int(turnover_hour)):
_LOGGER.info("Data for " + entity_code + " has expired. Refreshing data.")
return True

Expand Down
7 changes: 6 additions & 1 deletion custom_components/remiks_renovasjon/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ def is_on(self):
"""Return the boolean state of the entity."""
item = self._remiks_renovasjon.get_parsed_data(self._entity_code)
if item is not None:
state = (item[4].date() - datetime.now().date()).days <= int(self._remiks_renovasjon.days_notice)
state = True
days_until_next = (item[4].date() - datetime.now().date()).days
if days_until_next == 0 and datetime.today().time().hour >= int(self._remiks_renovasjon.turnover_hour):
state = False
elif days_until_next > int(self._remiks_renovasjon.days_notice):
state = False
_LOGGER.debug("State of entity code: Remiks boolean " + self._entity_code + ": " + str(state))
return state
else:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/remiks_renovasjon/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"requirements": [],
"dependencies": [],
"iot-class" : "cloud_polling",
"version": "0.3.3"
"version": "0.3.4"
}

0 comments on commit e73b780

Please sign in to comment.