Skip to content

Commit

Permalink
Ignore first update if bad per input entity (#12)
Browse files Browse the repository at this point in the history
Bump version to 2.1.0
  • Loading branch information
pnbruckner authored Jan 27, 2020
1 parent 8793269 commit e8d871d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion custom_components/composite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from .const import CONF_TIME_AS, DOMAIN, TZ_DEVICE_LOCAL, TZ_DEVICE_UTC

__version__ = '2.0.0'
__version__ = '2.1.0'

CONF_TZ_FINDER = 'tz_finder'
DEFAULT_TZ_FINDER = 'timezonefinderL==4.0.2'
Expand Down
22 changes: 10 additions & 12 deletions custom_components/composite/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
ATTR_LAST_ENTITY_ID = 'last_entity_id'
ATTR_TIME_ZONE = 'time_zone'

INACTIVE = 'inactive'
ACTIVE = 'active'
WARNED = 'warned'
STATUS = 'status'
SEEN = 'seen'
SOURCE_TYPE = ATTR_SOURCE_TYPE
DATA = 'data'
Expand Down Expand Up @@ -79,7 +82,7 @@ def __init__(self, hass, config, see):
self._entities = {}
for entity_id in entities:
self._entities[entity_id] = {
WARNED: False,
STATUS: INACTIVE,
SEEN: None,
SOURCE_TYPE: None,
DATA: None}
Expand All @@ -91,40 +94,35 @@ def __init__(self, hass, config, see):
self._req_movement = config[CONF_REQ_MOVEMENT]
self._lock = threading.Lock()
self._prev_seen = None
self._init_complete = False

self._remove = track_state_change(
hass, entities, self._update_info)

for entity_id in entities:
self._update_info(entity_id, None, hass.states.get(entity_id))

def init_complete(event):
self._init_complete = True

hass.bus.listen_once(EVENT_HOMEASSISTANT_START, init_complete)

def _bad_entity(self, entity_id, message):
msg = '{} {}'.format(entity_id, message)
# Has there already been a warning for this entity?
if self._entities[entity_id][WARNED]:
if self._entities[entity_id][STATUS] == WARNED:
_LOGGER.error(msg)
self._remove()
self._entities.pop(entity_id)
# Are there still any entities to watch?
if len(self._entities):
self._remove = track_state_change(
self._hass, self._entities.keys(), self._update_info)
# Don't warn during init.
elif self._init_complete:
# Only warn if this is not the first state change for the entity.
elif self._entities[entity_id][STATUS] == ACTIVE:
_LOGGER.warning(msg)
self._entities[entity_id][WARNED] = True
self._entities[entity_id][STATUS] = WARNED
else:
_LOGGER.debug(msg)
self._entities[entity_id][STATUS] = ACTIVE

def _good_entity(self, entity_id, seen, source_type, data):
self._entities[entity_id].update({
WARNED: False,
STATUS: ACTIVE,
SEEN: seen,
SOURCE_TYPE: source_type,
DATA: data})
Expand Down

0 comments on commit e8d871d

Please sign in to comment.