PK!jp55python_nhc/__init__.py__version__ = '0.2.0' from .python_nhc import NhcHub PK!python_nhc/nhc_action.py# -*- coding: utf-8 -*- class NhcAction: actionTypes = {'1': 'lights', '4': 'shutters'} def __init__(self, hub, id, name, type, state): """NhcAction constructor""" self._hub = hub self._id = id self._name = str(name) self._type = str(type) self._state = str(state) def __repr__(self): """Object representation""" return "NhcAction {}({}) -> {}".format( self._name, self._id, self._state) def setState(self, newState): self._state = str(newState) return 1 def runNewStateCommand(self, expectedNewState): TCP_Message = '{"cmd":"executeactions","id":"' + str(self._id) + '","value1":"' + str(expectedNewState) + '"}' self._hub.sendHubCommand(TCP_Message) return 1 def getNewStateCommand(self, expectedNewState): TCP_Message = '{"cmd":"executeactions","id":"' + str(self._id) + '","value1":"' + str(expectedNewState) + '"}' return TCP_Message def getState(self): return self._state def getType(self): return self._type def isLight(self): if self._type == '1': return True return False def isShutter(self): if self._type == '4': return True return False class NhcLight(NhcAction): def __repr__(self): """Object representation""" return "NhcLight {}({}) -> {}".format( self._name, self._id, self._state) def isOn(self): if super().getState() == '100': return True else: return False def turnOn(self): super().runNewStateCommand("100") return def turnOff(self): super().runNewStateCommand("0") return PK!q<<python_nhc/nhc_connection.py# -*- coding: utf-8 -*- import nclib NHC_PORT = 8000 class NhcConnection: def __init__(self, host, port = NHC_PORT): self._socket = nclib.Netcat((host, port), udp=False) def __del__(self): self._socket.shutdown(1) self._socket.close() def _receive_until(self, s): return self._socket.recv_until(s) def receive(self): return self._socket.recv().decode() def read(self): return self._receive_until(b'\r') def send(self, msg): self._socket.send(msg.encode()) return self.read() PK!\0%>>python_nhc/python_nhc.py# -*- coding: utf-8 -*- import json import logging import threading from .nhc_connection import NhcConnection from .nhc_action import NhcAction, NhcLight logger = logging.getLogger() logger.setLevel(logging.INFO) class NhcHub: def __init__(self, host, port = 8000): self._host = str(host) self._port = int(port) self._actions = self.listActions() daemon = threading.Thread(name='daemon', target=self.run) daemon.setDaemon(True) daemon.start() return None def run(self): connection = NhcConnection(self._host, self._port) logger.debug(">Connection on {}:{}".format(self._host, self._port)) connection.send("{\"cmd\": \"startevents\"}") while True: data = connection.receive() if not data: break elif not data.isspace(): d = json.loads(data) logger.debug(">> Data recieved: {}".format(d)) # Modify status of action when event recieved if 'event' in d.keys(): d_ev = json.loads(json.dumps(d['data'][0])) actionId = d_ev['id'] newVal = d_ev['value1'] logger.debug(">>new event: id:{} , val:{}".format( actionId, newVal)) a = self.getAction(actionId) a.setState(newVal) return None def getActions(self): if self._actions: return self._actions return None def getAction(self, actionId): if self._actions[actionId]: return self._actions[actionId] return None def getLights(self): lights = [] for k in self.getActions().keys(): a = self.getAction(k) if a.isLight(): lights.append(a) return lights def getCovers(self): covers = [] for k in self.getActions().keys(): a = self.getAction(k) if a.isCover(): covers.append(a) return covers def listActions(self): actions = {} connection = NhcConnection(self._host, self._port) data = connection.send("{\"cmd\": \"listactions\"}") logger.debug(">> Data recieved: {}".format(data)) data = json.loads(data) for a in data['data']: actionId = a['id'] actionName = a['name'] actionType = a['type'] actionState = a['value1'] if str(actionType) == '1': actions[actionId] = NhcLight(self, actionId, actionName, actionType, actionState) else: actions[actionId] = NhcAction(self, actionId, actionName, actionType, actionState) logger.debug(">> Action created: {}".format(actions[actionId])) return actions def listLocations(self): connection = NhcConnection(self._host, self._port) data = connection.send("{\"cmd\": \"listlocations\"}") logger.debug(">> Data recieved: {}".format(data)) return None def modifyActionState(self, actionId, newState): a = self.getAction(actionId) command = a.getNewStateCommand(newState) connection = NhcConnection(self._host, self._port) data = connection.send(command) logger.debug(">> Data recieved: {}".format(data)) return None def sendHubCommand(self, command): connection = NhcConnection(self._host, self._port) data = connection.send(command) logger.debug(">> Data recieved: {}".format(data)) return None PK!J}''"python_nhc-0.2.0.dist-info/LICENSEMIT License Copyright (c) 2018 gawood Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTU python_nhc-0.2.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!HҀn#python_nhc-0.2.0.dist-info/METADATANB1}yo`0iDP&VI-ɜXe}>z>0--͟U̹F_}Hu=ȩ2FɹP>0 python_nhc/python_nhc.pyPK!J}''"python_nhc-0.2.0.dist-info/LICENSEPK!HڽTU  python_nhc-0.2.0.dist-info/WHEELPK!HҀn#python_nhc-0.2.0.dist-info/METADATAPK!HZֈt!python_nhc-0.2.0.dist-info/RECORDPKXp