#!/usr/local/bin/python3 -u
"""
    Author:     Oliver Ratzesberger <https://github.com/fxstein>
    Copyright:  Copyright (C) 2016 Oliver Ratzesberger
    License:    Apache License, Version 2.0
"""

# Make sure we have access to SentientHome commons
import os
import sys
try:
    sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
except:
    exit(1)

from collections import defaultdict, deque

from cement.core import hook
from cement.utils.misc import init_defaults

try:
    sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
                    '/../dependencies/ISYlib-python')

    from ISY.IsyClass import Isy
except:
    exit(1)

defaults = init_defaults('plugin.isy')


def extend_app(app):

    isy_addr = app.config.get('isy', 'isy_addr')
    isy_user = app.config.get('isy', 'isy_user')
    isy_pass = app.config.get('isy', 'isy_pass')

    app.log.debug('Connecting to ISY controller @%s' % isy_addr, __name__)

    try:
        isy = Isy(addr=isy_addr, userl=isy_user, userp=isy_pass)
    except Exception as e:
        app.log.error('Unable to connect to ISY controller @%s' % isy_addr,
                      __name__)
        app.log.error(e)
        return

    # extend the event engine app object with an ``isy`` member
    app.extend('isy', isy)

    # Make sure we pre populate all internal isy structures
    isy.load_nodes()


def event_state(app, event_type, event):
    app.log.debug('event_state() Event: %s %s' %
                  (event_type, event), __name__)
    try:
        if event_type == 'isy':
            # Initialize state event cache if it does not exist yet
            if not app._memory.state[event_type]:
                app._memory.state[event_type] = defaultdict(deque)

            # Populate state memory
            state = dict()
            state['time'] = event['shtime1']

            state['control'] = event['Event.control']
            state['action'] = event['Event.action']

            actions = ['DON', 'DFON', 'DOF', 'DFOF', 'ST', 'OL',
                       'RR', 'BMAN', 'SMAN', 'DIM', 'BRT']

            if state['control'] in actions:
                # Initialize state event cache if it does not exist yet
                if not isinstance(app._memory.state[event_type]
                                  [event['Event.node']], deque):
                    app._memory.state[event_type][event['Event.node']] =\
                        deque(maxlen=100)

                app.log.info('Node: %s Data: %s' %
                             (event['Event.node'], state), __name__)
                app._memory.state[event_type][event['Event.node']].\
                    appendleft(state)
    except Exception as e:
        app.log.error('Error appending state: %s' % e, __name__)


def load(app):
    hook.register('post_run', extend_app)
    hook.register('event_state', event_state)

    app.log.info('Succeful ISY Plugin registration', __name__)

#
# Do nothing
# (syntax check)
#
if __name__ == "__main__":
    import __main__
    print(__main__.__file__)
    print("syntax ok")

    exit(0)

@@ -25,20 +25,55 @@
  -u%0A%22%22%22%0A
-    
+SentientHome ISY event engine plugin.%0A%0A
 Author: 
@@ -125,20 +125,16 @@
 xstein%3E%0A
-    
 Copyrigh
@@ -158,9 +158,9 @@
  201
-6
+7
  Oli
@@ -176,20 +176,16 @@
 sberger%0A
-    
 License:
@@ -372,24 +372,34 @@
 /..')%0Aexcept
+ Exception
 :%0A    exit(1
@@ -436,17 +436,40 @@
 aultdict
-,
+%0Afrom collections import
  deque%0A%0A
@@ -706,16 +706,26 @@
 y%0Aexcept
+ Exception
 :%0A    ex
@@ -793,16 +793,66 @@
 p(app):%0A
+    %22%22%22Extened event engine app through plugin.%22%22%22
 %0A    isy
@@ -1534,16 +1534,69 @@
 event):%0A
+    %22%22%22Extend event state processing with plugin.%22%22%22%0A
     app.
@@ -2947,24 +2947,59 @@
  load(app):%0A
+    %22%22%22Register plugin on load.%22%22%22%0A
     hook.reg
@@ -3139,16 +3139,17 @@
 ame__)%0A%0A
+%0A
 #%0A# Do n

Update event engine plugin to latest PEP8 standard