Skip to content

Commit

Permalink
maint: Remove use of DEFAULT_TIME_ZONE constant
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Jun 6, 2024
1 parent 35abd41 commit 66d8767
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
36 changes: 21 additions & 15 deletions custom_components/o365/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util import dt
from requests.exceptions import HTTPError, RetryError, ConnectionError
from homeassistant.util import dt as dt_util
from requests.exceptions import HTTPError, RetryError

from .const import (
ATTR_ALL_DAY,
Expand Down Expand Up @@ -302,8 +302,8 @@ async def async_update(self):
self._offset_reached = is_offset_reached(start, offset)
results = await self.data.async_o365_get_events(
self.hass,
dt.utcnow() + timedelta(hours=self._start_offset),
dt.utcnow() + timedelta(hours=self._end_offset),
dt_util.utcnow() + timedelta(hours=self._start_offset),
dt_util.utcnow() + timedelta(hours=self._end_offset),
)

if results is not None:
Expand Down Expand Up @@ -588,7 +588,9 @@ def _sort_events(self, events):
for event in events:
event.start_sort = event.start
if event.is_all_day:
event.start_sort = dt.as_utc(dt.start_of_local_day(event.start))
event.start_sort = dt_util.as_utc(
dt_util.start_of_local_day(event.start)
)

events.sort(key=attrgetter("start_sort"))

Expand Down Expand Up @@ -661,7 +663,7 @@ async def async_get_events(self, hass, start_date, end_date):

async def async_update(self, hass):
"""Do the update."""
start_of_day_utc = dt.as_utc(dt.start_of_local_day())
start_of_day_utc = dt_util.as_utc(dt_util.start_of_local_day())
results = await self.async_o365_get_events(
hass,
start_of_day_utc,
Expand Down Expand Up @@ -740,31 +742,35 @@ def is_all_day(vevent):
@staticmethod
def is_started(vevent):
"""Is it over."""
return dt.utcnow() >= O365CalendarData.to_datetime(get_start_date(vevent))
return dt_util.utcnow() >= O365CalendarData.to_datetime(get_start_date(vevent))

@staticmethod
def is_finished(vevent):
"""Is it over."""
return dt.utcnow() >= O365CalendarData.to_datetime(get_end_date(vevent))
return dt_util.utcnow() >= O365CalendarData.to_datetime(get_end_date(vevent))

@staticmethod
def to_datetime(obj):
"""To datetime."""
if isinstance(obj, datetime):
date_obj = (
obj.replace(tzinfo=dt.DEFAULT_TIME_ZONE) if obj.tzinfo is None else obj
obj.replace(tzinfo=dt_util.get_default_time_zone())
if obj.tzinfo is None
else obj
)
elif isinstance(obj, date):
date_obj = dt.start_of_local_day(
dt.dt.datetime.combine(obj, dt.dt.time.min)
date_obj = dt_util.start_of_local_day(
dt_util.dt.datetime.combine(obj, dt_util.dt.time.min)
)
elif "date" in obj:
date_obj = dt.start_of_local_day(
dt.dt.datetime.combine(dt.parse_date(obj["date"]), dt.dt.time.min)
date_obj = dt_util.start_of_local_day(
dt_util.dt.datetime.combine(
dt_util.parse_date(obj["date"]), dt_util.dt.time.min
)
)
else:
date_obj = dt.as_local(dt.parse_datetime(obj["dateTime"]))
return dt.as_utc(date_obj)
date_obj = dt_util.as_local(dt_util.parse_datetime(obj["dateTime"]))
return dt_util.as_utc(date_obj)


class CalendarServices:
Expand Down
10 changes: 7 additions & 3 deletions custom_components/o365/helpers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from requests.exceptions import HTTPError

from ..classes.mailsensor import build_inbox_query, build_query_query
Expand Down Expand Up @@ -85,7 +85,9 @@ def __init__(self, hass, config):
self._account_name = config[CONF_ACCOUNT_NAME]
self._keys = []
self._data = {}
self._zero_date = datetime(1, 1, 1, 0, 0, 0, tzinfo=dt.DEFAULT_TIME_ZONE)
self._zero_date = datetime(
1, 1, 1, 0, 0, 0, tzinfo=dt_util.get_default_time_zone()
)
self._chat_members = {}
self._ent_reg = entity_registry.async_get(hass)

Expand Down Expand Up @@ -395,7 +397,9 @@ def __init__(self, hass, config):
self._account_name = config[CONF_ACCOUNT_NAME]
self._keys = []
self._data = {}
self._zero_date = datetime(1, 1, 1, 0, 0, 0, tzinfo=dt.DEFAULT_TIME_ZONE)
self._zero_date = datetime(
1, 1, 1, 0, 0, 0, tzinfo=dt_util.get_default_time_zone()
)
self._chat_members = {}
self._ent_reg = entity_registry.async_get(hass)

Expand Down
22 changes: 12 additions & 10 deletions custom_components/o365/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.const import CONF_ENABLED, CONF_NAME, CONF_UNIQUE_ID
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_platform
from homeassistant.util import dt
from homeassistant.util import dt as dt_util

from .classes.entity import O365Entity
from .const import (
Expand Down Expand Up @@ -156,9 +156,11 @@ def __init__(
self.todolist = o365_task_folder
self._show_completed = yaml_task_list.get(CONF_SHOW_COMPLETED)

self.todo_last_created = dt.utcnow() - timedelta(minutes=5)
self.todo_last_completed = dt.utcnow() - timedelta(minutes=5)
self._zero_date = datetime(1, 1, 1, 0, 0, 0, tzinfo=dt.DEFAULT_TIME_ZONE)
self.todo_last_created = dt_util.utcnow() - timedelta(minutes=5)
self.todo_last_completed = dt_util.utcnow() - timedelta(minutes=5)
self._zero_date = datetime(
1, 1, 1, 0, 0, 0, tzinfo=dt_util.get_default_time_zone()
)
self._state = None
self._todo_items = None
self._extra_attributes = None
Expand Down Expand Up @@ -259,7 +261,7 @@ def _update_extra_state_attributes(self, todos):
if item.due:
due = item.due.date()
todo[ATTR_DUE] = due
if due < dt.utcnow().date():
if due < dt_util.utcnow().date():
overdue_todo = {
ATTR_SUBJECT: item.subject,
ATTR_TODO_ID: item.task_id,
Expand Down Expand Up @@ -393,7 +395,7 @@ async def _async_complete_task(self, o365_task, todo_id):
o365_task.mark_completed()
self.hass.async_add_executor_job(o365_task.save)
self._raise_event(EVENT_COMPLETED_TODO, todo_id)
self.todo_last_completed = dt.utcnow()
self.todo_last_completed = dt_util.utcnow()

async def _async_uncomplete_task(self, o365_task, todo_id):
if not o365_task.completed:
Expand All @@ -418,9 +420,9 @@ async def _async_save_task(
if isinstance(due, str):
try:
if len(due) > 10:
o365_task.due = dt.parse_datetime(due).date()
o365_task.due = dt_util.parse_datetime(due).date()
else:
o365_task.due = dt.parse_date(due)
o365_task.due = dt_util.parse_date(due)
except ValueError:
raise ServiceValidationError( # pylint: disable=raise-missing-from
translation_domain=DOMAIN,
Expand Down Expand Up @@ -469,12 +471,12 @@ def build_todo_query(key, todo):
start_offset = o365_task.get(CONF_DUE_HOURS_BACKWARD_TO_GET)
end_offset = o365_task.get(CONF_DUE_HOURS_FORWARD_TO_GET)
if start_offset:
start = dt.utcnow() + timedelta(hours=start_offset)
start = dt_util.utcnow() + timedelta(hours=start_offset)
query.chain("and").on_attribute("due").greater_equal(
start.strftime("%Y-%m-%dT%H:%M:%S")
)
if end_offset:
end = dt.utcnow() + timedelta(hours=end_offset)
end = dt_util.utcnow() + timedelta(hours=end_offset)
query.chain("and").on_attribute("due").less_equal(
end.strftime("%Y-%m-%dT%H:%M:%S")
)
Expand Down

0 comments on commit 66d8767

Please sign in to comment.