Skip to content

Commit

Permalink
Fix #151
Browse files Browse the repository at this point in the history
Since we already have a value to indicate if timeout is set, use that and change connection_timeout to be a positive_float.  This fixes the serialization issues, which led to calendars disappearing on restart of HA, and other kinds of havoc.

Also fixed name and unique id properties
  • Loading branch information
franc6 committed Sep 11, 2024
1 parent 477efad commit aef8606
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
18 changes: 15 additions & 3 deletions custom_components/ics_calendar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@

from .const import (
CONF_ACCEPT_HEADER,
CONF_ADV_CONNECT_OPTS,
CONF_CALENDARS,
CONF_CONNECTION_TIMEOUT,
CONF_DAYS,
CONF_DOWNLOAD_INTERVAL,
CONF_INCLUDE_ALL_DAY,
CONF_OFFSET_HOURS,
CONF_PARSER,
CONF_REQUIRES_AUTH,
CONF_SET_TIMEOUT,
CONF_USER_AGENT,
DOMAIN,
)
Expand Down Expand Up @@ -89,8 +92,8 @@
CONF_ACCEPT_HEADER, default=""
): cv.string,
vol.Optional(
CONF_CONNECTION_TIMEOUT, default=None
): cv.socket_timeout,
CONF_CONNECTION_TIMEOUT, default=300
): cv.positive_float,
}
)
]
Expand Down Expand Up @@ -205,15 +208,19 @@ def add_missing_defaults( # noqa: C901,R701 # pylint: disable=R0912,R0915
data = {}
data[CONF_NAME] = entry.data[CONF_NAME]
data[CONF_URL] = entry.data[CONF_URL]
data[CONF_ADV_CONNECT_OPTS] = False
data[CONF_REQUIRES_AUTH] = False
if CONF_INCLUDE_ALL_DAY in entry.data:
data[CONF_INCLUDE_ALL_DAY] = entry.data[CONF_INCLUDE_ALL_DAY]
else:
data[CONF_INCLUDE_ALL_DAY] = False
if CONF_USERNAME in entry.data:
data[CONF_REQUIRES_AUTH] = True
data[CONF_USERNAME] = entry.data[CONF_USERNAME]
else:
data[CONF_USERNAME] = ""
if CONF_PASSWORD in entry.data:
data[CONF_REQUIRES_AUTH] = True
data[CONF_PASSWORD] = entry.data[CONF_PASSWORD]
else:
data[CONF_PASSWORD] = ""
Expand All @@ -234,6 +241,7 @@ def add_missing_defaults( # noqa: C901,R701 # pylint: disable=R0912,R0915
else:
data[CONF_DOWNLOAD_INTERVAL] = 15
if CONF_USER_AGENT in entry.data:
data[CONF_ADV_CONNECT_OPTS] = True
data[CONF_USER_AGENT] = entry.data[CONF_USER_AGENT]
else:
data[CONF_USER_AGENT] = ""
Expand All @@ -250,13 +258,17 @@ def add_missing_defaults( # noqa: C901,R701 # pylint: disable=R0912,R0915
else:
data[CONF_OFFSET_HOURS] = 0
if CONF_ACCEPT_HEADER in entry.data:
data[CONF_ADV_CONNECT_OPTS] = True
data[CONF_ACCEPT_HEADER] = entry.data[CONF_ACCEPT_HEADER]
else:
data[CONF_ACCEPT_HEADER] = ""
if CONF_CONNECTION_TIMEOUT in entry.data:
data[CONF_ADV_CONNECT_OPTS] = True
data[CONF_SET_TIMEOUT] = True
data[CONF_CONNECTION_TIMEOUT] = entry.data[CONF_CONNECTION_TIMEOUT]
else:
data[CONF_CONNECTION_TIMEOUT] = None
data[CONF_SET_TIMEOUT] = False
data[CONF_CONNECTION_TIMEOUT] = 300.0

return data

Expand Down
22 changes: 13 additions & 9 deletions custom_components/ics_calendar/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CONF_INCLUDE_ALL_DAY,
CONF_OFFSET_HOURS,
CONF_PARSER,
CONF_SET_TIMEOUT,
CONF_USER_AGENT,
DOMAIN,
)
Expand Down Expand Up @@ -143,9 +144,9 @@ def __init__(self, entity_id: str, device_data, unique_id: str = None):
)
self.data = ICSCalendarData(device_data)
self.entity_id = entity_id
self.unique_id = unique_id
self._attr_unique_id = f"ICSCalendar.{unique_id}"
self._event = None
self._name = device_data[CONF_NAME]
self._attr_name = device_data[CONF_NAME]
self._last_call = None

@property
Expand All @@ -158,12 +159,7 @@ def event(self) -> Optional[CalendarEvent]:
return self._event

@property
def name(self):
"""Return the name of the calendar."""
return self._name

@property
def should_poll(self):
def should_poll(self) -> bool:
"""Indicate if the calendar should be polled.
If the last call to update or get_api_events was not within the minimum
Expand Down Expand Up @@ -269,7 +265,15 @@ def __init__(self, device_data):
device_data[CONF_ACCEPT_HEADER],
)

self._calendar_data.set_timeout(device_data[CONF_CONNECTION_TIMEOUT])
if CONF_SET_TIMEOUT in device_data:
if device_data[CONF_SET_TIMEOUT]:
self._calendar_data.set_timeout(
device_data[CONF_CONNECTION_TIMEOUT]
)
else:
self._calendar_data.set_timeout(
device_data[CONF_CONNECTION_TIMEOUT]
)

async def async_get_events(
self, hass: HomeAssistant, start_date: datetime, end_date: datetime
Expand Down
9 changes: 4 additions & 5 deletions custom_components/ics_calendar/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@

from . import (
CONF_ACCEPT_HEADER,
CONF_ADV_CONNECT_OPTS,
CONF_CONNECTION_TIMEOUT,
CONF_DAYS,
CONF_DOWNLOAD_INTERVAL,
CONF_INCLUDE_ALL_DAY,
CONF_OFFSET_HOURS,
CONF_PARSER,
CONF_REQUIRES_AUTH,
CONF_SET_TIMEOUT,
CONF_USER_AGENT,
)
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

CONF_REQUIRES_AUTH = "requires_auth"
CONF_ADV_CONNECT_OPTS = "advanced_connection_options"
CONF_SET_TIMEOUT = "set_connection_timeout"

CALENDAR_NAME_SCHEMA = vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
Expand Down Expand Up @@ -81,7 +80,7 @@
)

TIMEOUT_OPTS_SCHEMA = vol.Schema(
{vol.Optional(CONF_CONNECTION_TIMEOUT, default=None): cv.positive_int}
{vol.Optional(CONF_CONNECTION_TIMEOUT, default=None): cv.positive_float}
)


Expand Down
5 changes: 4 additions & 1 deletion custom_components/ics_calendar/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Constants for ics_calendar platform."""

VERSION = "5.0.0"
VERSION = "5.0.1"
DOMAIN = "ics_calendar"

CONF_DEVICE_ID = "device_id"
Expand All @@ -13,3 +13,6 @@
CONF_OFFSET_HOURS = "offset_hours"
CONF_ACCEPT_HEADER = "accept_header"
CONF_CONNECTION_TIMEOUT = "connection_timeout"
CONF_SET_TIMEOUT = "set_connection_timeout"
CONF_REQUIRES_AUTH = "requires_auth"
CONF_ADV_CONNECT_OPTS = "advanced_connection_options"

0 comments on commit aef8606

Please sign in to comment.