Add recent context (#15989)

* Add recent context

* Add async_set_context to components not using new services
This commit is contained in:
Paulus Schoutsen
2018-08-20 17:39:53 +02:00
committed by GitHub
parent d1e1b9b38a
commit 1be61df9c0
11 changed files with 211 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
"""An abstract class for entities."""
import asyncio
from datetime import timedelta
import logging
import functools as ft
from timeit import default_timer as timer
@@ -16,6 +17,7 @@ from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.util import ensure_unique_string, slugify
from homeassistant.util.async_ import run_callback_threadsafe
from homeassistant.util import dt as dt_util
_LOGGER = logging.getLogger(__name__)
SLOW_UPDATE_WARNING = 10
@@ -85,6 +87,10 @@ class Entity:
# Hold list for functions to call on remove.
_on_remove = None
# Context
_context = None
_context_set = None
@property
def should_poll(self) -> bool:
"""Return True if entity has to be polled for state.
@@ -173,13 +179,24 @@ class Entity:
"""Flag supported features."""
return None
@property
def context_recent_time(self):
"""Time that a context is considered recent."""
return timedelta(seconds=5)
# DO NOT OVERWRITE
# These properties and methods are either managed by Home Assistant or they
# are used to perform a very specific function. Overwriting these may
# produce undesirable effects in the entity's operation.
@callback
def async_set_context(self, context):
"""Set the context the entity currently operates under."""
self._context = context
self._context_set = dt_util.utcnow()
@asyncio.coroutine
def async_update_ha_state(self, force_refresh=False, context=None):
def async_update_ha_state(self, force_refresh=False):
"""Update Home Assistant with current state of entity.
If force_refresh == True will update entity before setting state.
@@ -278,8 +295,14 @@ class Entity:
# Could not convert state to float
pass
if (self._context is not None and
dt_util.utcnow() - self._context_set >
self.context_recent_time):
self._context = None
self._context_set = None
self.hass.states.async_set(
self.entity_id, state, attr, self.force_update, context)
self.entity_id, state, attr, self.force_update, self._context)
def schedule_update_ha_state(self, force_refresh=False):
"""Schedule an update ha state change task.