Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed global variable usage to fix new holiday creation #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions custom_components/calendarific/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from homeassistant import config_entries
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -37,7 +38,6 @@

_LOGGER = logging.getLogger(__name__)

holiday_list = []

def setup(hass, config):
"""Set up platform using YAML."""
Expand All @@ -53,15 +53,13 @@ def setup(hass, config):
return True


async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
return True

async def async_unload_entry(hass, entry):
"""Unload a config entry."""
return await hass.config_entries.async_forward_entry_unload(entry, "sensor")
return await hass.config_entries.async_forward_entry_unload(entry, Platform.SENSOR)

class CalendarificApiReader:

Expand Down Expand Up @@ -97,6 +95,9 @@ def get_description(self,holiday_name):
except:
return "NOT FOUND"

def get_holidays(self):
return [item['name'] for item in self._holidays]

def update(self):
if self._lastupdated == datetime.now().date():
return
Expand All @@ -121,10 +122,6 @@ def update(self):
return
self._error_logged = False
self._next_holidays = response['response']['holidays']
global holiday_list
holiday_list = []
for holiday in self._holidays:
holiday_list.append(holiday['name'])

return True

Expand Down
8 changes: 5 additions & 3 deletions custom_components/calendarific/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant import core
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback

Expand All @@ -27,7 +28,6 @@
CONF_UNIT_OF_MEASUREMENT,
)

from . import holiday_list

_LOGGER = logging.getLogger(__name__)

Expand All @@ -46,9 +46,11 @@ def __init__(self) -> None:
self._errors = {}
self._data = {}
self._data["unique_id"] = str(uuid.uuid4())
hass = core.async_get_hass()
self._holiday_list = hass.data[DOMAIN]["apiReader"].get_holidays()

async def async_step_user(self, user_input=None):
if holiday_list == []:
if self._holiday_list == []:
return self.async_abort(reason="no_holidays_found")
self._errors = {}
if user_input is not None:
Expand Down Expand Up @@ -84,7 +86,7 @@ async def _show_user_form(self, user_input):
if CONF_UNIT_OF_MEASUREMENT in user_input:
unit_of_measurement = user_input[CONF_UNIT_OF_MEASUREMENT]
data_schema = OrderedDict()
data_schema[vol.Required(CONF_HOLIDAY, default=holiday)] = vol.In(holiday_list)
data_schema[vol.Required(CONF_HOLIDAY, default=holiday)] = vol.In(self._holiday_list)
data_schema[vol.Optional(CONF_NAME, default=name)] = str
data_schema[vol.Required(CONF_UNIT_OF_MEASUREMENT, default=unit_of_measurement)] = str
data_schema[vol.Required(CONF_ICON_NORMAL, default=icon_normal)] = str
Expand Down