Addressed the comments in the issue. pep8+pylint seem fine now. (still works, too.)

This commit is contained in:
Markus Stenberg 2014-04-24 17:13:57 +03:00
parent 39d1fad8cc
commit 88fd75b4c7
2 changed files with 18 additions and 26 deletions

View File

@ -150,8 +150,8 @@ def from_config_file(config_path, enable_logging=True):
if has_section("process"): if has_section("process"):
process = load_module('process') process = load_module('process')
kwargs = dict(config.items('process')) processes = dict(config.items('process'))
add_status("process", process.setup(bus, statemachine, **kwargs)) add_status("process", process.setup(hass, processes))
# Light control # Light control
if has_section("light.hue"): if has_section("light.hue"):

View File

@ -9,8 +9,8 @@
# Copyright (c) 2014 Markus Stenberg # Copyright (c) 2014 Markus Stenberg
# #
# Created: Wed Apr 23 23:33:26 2014 mstenber # Created: Wed Apr 23 23:33:26 2014 mstenber
# Last modified: Wed Apr 23 23:48:13 2014 mstenber # Last modified: Thu Apr 24 17:13:04 2014 mstenber
# Edit time: 13 min # Edit time: 19 min
# #
""" """
@ -20,40 +20,32 @@ The arguments are <subentityname>=<substring to find in process list>
""" """
import homeassistant as ha
from homeassistant.components import (STATE_ON, STATE_OFF) from homeassistant.components import (STATE_ON, STATE_OFF)
import os import os
DOMAIN = 'process' DOMAIN = 'process'
ENTITY_ID_FORMAT = DOMAIN + '.{}' ENTITY_ID_FORMAT = DOMAIN + '.{}'
PS_STRING = 'ps awx' PS_STRING = 'ps awx'
INTERVAL=30
def setup(bus, statemachine, **processes): def setup(hass, processes):
_states = {} """ Track local processes. """
def _update_process_state(t, force_reload=False): # pylint: disable=unused-argument
with os.popen(PS_STRING, 'r') as f: def _update_process_state(time):
lines = list(iter(f)) """ Check ps for currently running processes. """
for e, s in processes.items(): with os.popen(PS_STRING, 'r') as psfile:
lines = list(iter(psfile))
for pname, pstring in processes.items():
found = False found = False
for line in lines: for line in lines:
if s in line: if pstring in line:
found = True found = True
break break
if _states.get(e, None) == found: entity_id = ENTITY_ID_FORMAT.format(pname)
continue
_states[e] = found
entity_id = ENTITY_ID_FORMAT.format(e)
state = found and STATE_ON or STATE_OFF state = found and STATE_ON or STATE_OFF
statemachine.set_state(entity_id, state) hass.states.set(entity_id, state)
_update_process_state(None, True) _update_process_state(None)
kwargs = {} hass.track_time_change(_update_process_state, second=[0, 30])
if INTERVAL != ha.TIMER_INTERVAL:
kwargs['second'] = [0, INTERVAL]
assert INTERVAL > ha.TIMER_INTERVAL
ha.track_time_change(bus, _update_process_state)
return True return True