Skip to content

Commit

Permalink
perf: memoize notification state
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Oct 2, 2020
1 parent 196e41e commit 4b71769
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
import datetime
import logging
from typing import List, Text # noqa pylint: disable=unused-import
from typing import Callable, List, Optional, Text # noqa pylint: disable=unused-import

from homeassistant.const import (
DEVICE_CLASS_TIMESTAMP,
Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(
self._active = []
self._next = None
self._prior_value = None
self._timestamp: datetime.datetime = None
self._state: Optional[datetime.datetime] = None
self._process_raw_notifications()

def _process_raw_notifications(self):
Expand All @@ -177,6 +177,7 @@ def _process_raw_notifications(self):
else []
)
self._next = self._active[0][1] if self._active else None
self._state = self._process_state(self._next)

def _fix_alarm_date_time(self, value):
if (
Expand Down Expand Up @@ -326,9 +327,9 @@ def should_poll(self):
)

@property
def state(self):
def state(self) -> datetime.datetime:
"""Return the state of the sensor."""
return self._process_state(self._next)
return self._state

def _process_state(self, value):
return (
Expand Down Expand Up @@ -434,11 +435,6 @@ def __init__(self, client, n_json, account):
else "mdi:timer",
)

@property
def state(self) -> datetime.datetime:
"""Return the state of the sensor."""
return self._process_state(self._next)

def _process_state(self, value):
return (
dt.as_local(
Expand Down Expand Up @@ -480,11 +476,6 @@ def __init__(self, client, n_json, account):
client, n_json, "alarmTime", account, f"next {self._type}", "mdi:reminder"
)

@property
def state(self):
"""Return the state of the sensor."""
return self._process_state(self._next)

def _process_state(self, value):
return (
dt.as_local(
Expand Down

0 comments on commit 4b71769

Please sign in to comment.