-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoBlindCliCommands.py
77 lines (70 loc) · 3.37 KB
/
AutoBlindCliCommands.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
#!/usr/bin/env python3
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#########################################################################
# Copyright 2014- Thomas Ernst [email protected]
#########################################################################
# Finite state machine plugin for SmartHomeNG
#
# This plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this plugin. If not, see <http://www.gnu.org/licenses/>.
#########################################################################
import logging
# noinspection PyUnresolvedReferences
from lib.model.smartplugin import SmartPlugin
class AbCliCommands:
def __init__(self, smarthome, items):
self.__items = items
self._sh = smarthome
self.logger = logging.getLogger(__name__)
# Add additional cli commands if cli is active (and functionality to add own cli commands is available)
try:
cli = self._get_cli_plugin()
if cli is None:
self.logger.info("AutoBlind: Additional CLI commands not registered because CLI plugin is not active")
elif not isinstance(cli, SmartPlugin):
self.logger.info("AutoBlind: Additional CLI commands not registered because CLI plugin is to old")
else:
cli.add_command("as_list", self.cli_list, "as_list: list AutoState items")
cli.add_command("as_detail", self.cli_detail, "as_detail [asItem]: show details on AutoState item [asItem]")
self.logger.info("AutoBlind: Two additional CLI commands registered")
except AttributeError as err:
self.logger.error("AutoBlind: Additional CLI commands not registered because error occured.")
self.logger.exception(err)
# CLI command as_list
# noinspection PyUnusedLocal
def cli_list(self, handler, parameter, source):
handler.push("Items for AutoState Plugin\n")
handler.push("==========================\n")
for name in sorted(self.__items):
self.__items[name].cli_list(handler)
# CLI command as_detail
# noinspection PyUnusedLocal
def cli_detail(self, handler, parameter, source):
item = self.__cli_getitem(handler, parameter)
if item is not None:
item.cli_detail(handler)
# get item from parameter
def __cli_getitem(self, handler, parameter):
if parameter not in self.__items:
handler.push("no AutoState item \"{0}\" found.\n".format(parameter))
return None
return self.__items[parameter]
def _get_cli_plugin(self):
# noinspection PyBroadException
try:
for plugin in self._sh.return_plugins():
if plugin.__module__ == 'plugins.cli':
return plugin
return None
except Exception:
return None