Skip to content

Commit

Permalink
Reading of tommekalender failed when there were only one date for fra…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
eyesoft committed Oct 24, 2022
1 parent 5b75a5d commit 698dbc1
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions custom_components/min_renovasjon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
})
}, extra=vol.ALLOW_EXTRA)


async def async_setup(hass: HomeAssistant, config: dict):
if DOMAIN not in config:
return True

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN]["calendar_list"] = None

street_name = config[DOMAIN][CONF_STREET_NAME]
street_code = config[DOMAIN][CONF_STREET_CODE]
house_no = config[DOMAIN][CONF_HOUSE_NO]
Expand All @@ -56,26 +57,28 @@ async def async_setup(hass: HomeAssistant, config: dict):

return True

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN]["calendar_list"] = None
street_name = config_entry.data.get(CONF_STREET_NAME, "")
street_code = config_entry.data.get(CONF_STREET_CODE, "")
house_no = config_entry.data.get(CONF_HOUSE_NO, "")
county_id = config_entry.data.get(CONF_COUNTY_ID, "")
date_format = config_entry.options.get(CONF_DATE_FORMAT, DEFAULT_DATE_FORMAT)

min_renovasjon = MinRenovasjon(hass, street_name, street_code, house_no, county_id, date_format)

hass.data[DOMAIN]["data"] = min_renovasjon
hass.config_entries.async_setup_platforms(config_entry, ["sensor"])

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
return True


class MinRenovasjon:
def __init__(self, hass, gatenavn, gatekode, husnr, kommunenr, date_format):
self._hass = hass
Expand All @@ -94,7 +97,7 @@ def _url_encode(string):

async def _get_tommekalender_from_web_api(self):
_LOGGER.debug("_get_tommekalender_from_web_api")

header = {CONST_KOMMUNE_NUMMER: self._kommunenr, CONST_APP_KEY: CONST_APP_KEY_VALUE}
url = CONST_URL_TOMMEKALENDER
url = url.replace('[gatenavn]', self.gatenavn)
Expand All @@ -113,7 +116,7 @@ async def _get_tommekalender_from_web_api(self):

async def _get_fraksjoner_from_web_api(self):
_LOGGER.debug("_get_fraksjoner_from_web_api")

header = {CONST_KOMMUNE_NUMMER: self._kommunenr, CONST_APP_KEY: CONST_APP_KEY_VALUE}
url = CONST_URL_FRAKSJONER

Expand Down Expand Up @@ -177,8 +180,9 @@ def _check_for_refresh_of_data(kalender_list):
for entry in kalender_list:
_, _, _, tommedato_forste, tommedato_neste = entry

if tommedato_forste is None or tommedato_neste is None or tommedato_forste.date() < date.today() or tommedato_neste.date() < date.today():
_LOGGER.debug("Data needs refresh")
if tommedato_forste is None or tommedato_forste.date() < date.today() or (
tommedato_neste is not None and tommedato_neste.date() < date.today()):
_LOGGER.debug("Data needs refresh 1")
return True

return False
Expand All @@ -197,16 +201,17 @@ async def get_calender_for_fraction(self, fraksjon_id):
if entry is not None:
entry_fraksjon_id, _, _, tommedato_forste, tommedato_neste = entry
if int(fraksjon_id) == int(entry_fraksjon_id):
if tommedato_forste is None or tommedato_neste is None or tommedato_forste.date() < date.today() or tommedato_neste.date() < date.today():
_LOGGER.debug("Data needs refresh")
if tommedato_forste is None or tommedato_forste.date() < date.today() or (
tommedato_neste is not None and tommedato_neste.date() < date.today()):
_LOGGER.debug("Data needs refresh 2")
self._hass.data[DOMAIN]["calendar_list"] = None
entry = await self.get_calender_for_fraction(fraksjon_id)
return entry
return None

async def get_calendar_list(self, refresh=False):
data = self._hass.data[DOMAIN]["calendar_list"]

if refresh or data is None:
tommekalender, fraksjoner = await self._get_from_web_api()
kalender_list = self._parse_calendar_list(tommekalender, fraksjoner)
Expand All @@ -224,7 +229,7 @@ async def get_calendar_list(self, refresh=False):
kalender_list = await self.get_calendar_list(refresh=True)

self._hass.data[DOMAIN]["calendar_list"] = kalender_list

return kalender_list

def format_date(self, date):
Expand Down

0 comments on commit 698dbc1

Please sign in to comment.