forked from foxcris/appdaemon-telegrambot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.py
91 lines (81 loc) · 3.38 KB
/
Helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import inspect
import appdaemon.plugins.hass.hassapi as hass
import re
class BaseClass(hass.Hass):
def _log_info(self, msg, prefix=None):
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
callername = calframe[1][3]
if prefix is not None and prefix != "":
self.log("%s: %s: %s: %s" %
(self.__class__.__name__, prefix, callername, msg))
else:
self.log("%s: %s: %s" % (self.__class__.__name__, callername, msg))
def _log_debug(self, msg, prefix=None):
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
callername = calframe[1][3]
if self.args["debug"]:
if prefix is not None and prefix != "":
self.log("DEBUG: %s: %s: %s: %s" %
(self.__class__.__name__, prefix, callername, msg))
else:
self.log("DEBUG: %s: %s: %s" %
(self.__class__.__name__, callername, msg))
def _log_error(self, msg, prefix=None):
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
callername = calframe[1][3]
if prefix is not None and prefix != "":
self.log("ERROR: %s: %s: %s: %s" %
(self.__class__.__name__, prefix, callername, msg))
else:
self.log("ERROR: %s: %s: %s" % (self.__class__.__name__, callername, msg))
def _getattribute(self, statedict, entity, atr):
return statedict.get(entity).get("attributes").get(atr, None)
def _convertname(self, name):
if name is not None and name != "":
return name.lower().replace(" ", "_")
else:
return None
def _getid(self, statedict, entity):
idlist = ['friendly_name', 'id', 'value_id']
count = 0
id = None
while id is None and count < len(idlist):
self._log_debug("idlist: %s" % idlist[count])
id = self._convertname(self._getattribute(
statedict, entity, idlist[count]))
count += 1
if id is None:
# id is still None. We have to clarify where to get the id
self._log_debug("Could not detect id of the item. Values %s" %
self.get_state(entity, attribute="all"))
return id
def _anyone_home(self, regex='^person.*'):
statedict = self.get_state()
anyonehome = False
for entity in statedict:
if re.match(regex, entity, re.IGNORECASE):
id = self._getid(statedict, entity)
state = self.get_state(entity)
self._log_debug("Person %s is %s" % (id, state))
if state == "home":
anyonehome = True
return anyonehome
def import_install_module(self, package):
import subprocess
import sys
import importlib
importedmodule = None
try:
importedmodule = importlib.import_module(package)
except ImportError:
self._log_debug(sys.executable)
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
except Exception as e:
self._log_error(e)
finally:
importedmodule = importlib.import_module(package)
return importedmodule