def test_02_Decode(self): # l_json = json_tools.encode_json(self.m_pyhouse_obj.Computer) l_json = json_tools.encode_json(MSG_DICT) print(PrettyFormatAny.form(l_json, 'A1-02-A - Encoded Info')) l_dict = json_tools.decode_json_unicode(l_json) print(l_dict) print(PrettyFormatAny.form(l_dict, 'A1-02-B - Decoded Info'))
def decode(self, p_topic, p_message): """ Decode Mqtt message ==> pyhouse/<house name>/house/lighting/controller/<action> @param p_topic: is the topic after 'controller' @return: a message to be logged as a Mqtt message """ l_logmsg = '\tLighting/Controllers: {}\n\t'.format(p_topic) if p_topic[0] == 'control': l_logmsg += 'Controller Control: {}'.format( PrettyFormatAny.form(p_message, 'Controller Control')) LOG.debug( 'MqttLightingControllersDispatch Control Topic:{}\n\tMsg: {}'. format(p_topic, p_message)) elif p_topic[0] == 'status': # The status is contained in LightData() above. l_logmsg += 'Controller Status: {}'.format( PrettyFormatAny.form(p_message, 'Controller Status')) LOG.debug( 'MqttLightingControllersDispatch Status Topic:{}\n\tMsg: {}'. format(p_topic, p_message)) else: l_logmsg += '\tUnknown Lighting/Controller sub-topic:{}\n\t{}'.format( p_topic, PrettyFormatAny.form(p_message, 'Controller Status')) LOG.warn('Unknown Controllers Topic: {}'.format(p_topic[0])) return l_logmsg
def test_01_Read(self): """ Basic _test to read in data and update it so we can check for new data in output. """ self.m_working_rooms[0].Comment = 'After mods' print(PrettyFormatAny.form(self.m_working_rooms[0], 'C2-01-A - WorkingRooms')) print(PrettyFormatAny.form(self.m_rooms[0], 'C2-01-B - ReadRooms')) self.assertEqual(self.m_working_rooms[0].Comment, 'After mods')
def test_02_Prep(self): """ """ self.m_working_rooms[0].Comment = 'After mods' l_ret = self.m_yaml._copy_to_yaml(self.m_pyhouse_obj) print(PrettyFormatAny.form(self.m_working_rooms[0], 'C2-02-A - Working Obj')) print(PrettyFormatAny.form(l_ret, 'C2-02-B - yaml staging'))
def decode(self, p_topic, p_message): """ Decode the computer specific portions of the message and append them to the log string. @param p-logmsg: is the partially decoded Mqtt message json @param p_topic: is a list of topic part strings ( pyhouse, housename have been dropped @param p_message: is the payload that is JSON """ l_logmsg = '\tComputer:\n' if p_topic[0] == 'browser': l_logmsg += '\tBrowser: Message {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) elif p_topic[0] == 'node' or p_topic[0] == 'nodes': l_logmsg += nodesMqtt(self.m_pyhouse_obj).decode(p_topic[1:], p_message, l_logmsg) # computer/ip elif p_topic[1] == 'ip': l_ip = extract_tools.get_mqtt_field(p_message, 'ExternalIPv4Address') l_logmsg += '\tIPv4: {}'.format(l_ip) # computer/startup elif p_topic[1] == 'startup': self._extract_node(p_message) l_logmsg += '\tStartup {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) if self.m_myname == self.m_sender: l_logmsg += '\tMy own startup of PyHouse\n' else: l_logmsg += '\tAnother computer started up: {}'.format(self.m_sender) # computer/shutdown elif p_topic[1] == 'shutdown': del self.m_pyhouse_obj.Computer.Nodes[self.m_name] l_logmsg += '\tSelf Shutdown {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) # computer/*** else: l_logmsg += '\tUnknown sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) return l_logmsg
def decode(self, p_topic, p_message): """ Decode Mqtt message ==> pyhouse/<house name>/house/lighting/light/<action> @param p_topic: is the topic after 'lighting' @return: a message to be logged as a Mqtt message """ if self.m_pyhouse_obj is None: LOG.error('Missing PyHouse obj') l_logmsg = '\tLighting/Lights: {}\n\t'.format(p_topic) # LOG.debug('LightingLightsDispatch Topic:{}\n\t{}'.format(p_topic, p_message)) if p_topic[0] == 'control': self._decode_control(p_message) l_logmsg += 'Light Control: {}'.format( PrettyFormatAny.form(p_message, 'Light Control')) LOG.debug( 'MqttLightingLightsDispatch Control Topic:{}\n\t{}'.format( p_topic, p_message)) elif p_topic[0] == 'status': # The status is contained in LightData() above. # l_logmsg += 'Light Status: {}'.format(PrettyFormatAny.form(p_message, 'Light Status')) l_logmsg += 'Light Status: {}'.format(p_message) LOG.debug( 'MqttLightingLightsDispatch Status Topic:{}\n\t{}'.format( p_topic, p_message)) else: l_logmsg += '\tUnknown Lighting/Light sub-topic:{}\n\t{}'.format( p_topic, PrettyFormatAny.form(p_message, 'Light Status')) LOG.warn('Unknown Lights Topic: {}'.format(p_topic[0])) return l_logmsg
def decode(self, p_topic, p_message): """ Decode the computer specific portions of the message and append them to the log string. @param p-logmsg: is the partially decoded Mqtt message json @param p_topic: is a list of topic part strings ( pyhouse, housename have been dropped @param p_message: is the payload that is JSON """ l_logmsg = '\tComputer:\n' # computer/browser/*** if p_topic[1] == 'browser': _l_name = 'unknown' l_logmsg += '\tBrowser: Message {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) # computer/ip elif p_topic[1] == 'ip': l_ip = self._get_field(p_message, 'ExternalIPv4Address') l_logmsg += '\tIPv4: {}'.format(l_ip) # computer/startup elif p_topic[1] == 'startup': self._extract_node(p_message) l_logmsg += '\tStartup {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) if self.m_myname == self.m_sender: l_logmsg += '\tMy own startup of PyHouse\n' else: l_logmsg += '\tAnother computer started up: {}'.format(self.m_sender) # computer/shutdown elif p_topic[1] == 'shutdown': del self.m_pyhouse_obj.Computer.Nodes[self.m_name] l_logmsg += '\tSelf Shutdown {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) # computer/node/??? elif p_topic[1] == 'node': l_logmsg += syncAPI(self.m_pyhouse_obj).DecodeMqttMessage(p_topic, p_message) # computer/*** else: l_logmsg += '\tUnknown sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Computer msg', 160)) return l_logmsg
def test_02_AllDevices(self): """ Read the xml and fill in the first room's dict """ l_obj = samsungXml.read_samsung_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_obj, 'C1-02-A - All Devices')) print( PrettyFormatAny.form(self.m_pyhouse_obj.House.Entertainment, 'C1-02-B - All Devices'))
def test_03_Lights(self): l_xml = self.m_apix.find_xml_section( self.m_pyhouse_obj, 'HouseDivision/LightingSection/LightSection') print(PrettyFormatAny.form(l_xml, 'J3-03-A Base')) print(PrettyFormatAny.form(l_xml[0], 'J3-03-B Base')) # self.assertEqual(l_xml.Name, '') self.assertEqual(l_xml[0].attrib['Name'], TESTING_LIGHT_NAME_0) pass
def test_03_Object(self): l_ret = debug_tools._formatObject('PyHouse', self.m_pyhouse_obj, maxlen=90, lindent=20) print('\nC2-03-A - Object:', l_ret) print(PrettyFormatAny.form(l_ret, "C2-03-B - Object")) l_ret = PrettyFormatAny.form(self.m_pyhouse_obj, 'XXX')
def test_03_Rate(self): """ """ l_obj = Serial_driver.SerialInterfaceInformation() print(PrettyFormatAny.form(l_obj, 'B1-03-A - Serial')) l_yaml = self.GetYaml()['Interface']['Baud'] print('B1-03-B - Yaml {}'.format(l_yaml)) l_ret = Serial_driver.Config()._extract_baud(l_yaml, l_obj) print(PrettyFormatAny.form(l_ret, 'B1-03-C - Serial'))
def test_01_All(self): """ Be sure that the XML contains the right stuff. """ l_controllers = self.m_ctlr_api.read_all_controllers_xml(self.m_pyhouse_obj) self.m_pyhouse_obj.House.Lighting.Controllers = l_controllers print(PrettyFormatAny.form(l_controllers[0], 'R1-01-A - Controller Obj')) print(PrettyFormatAny.form(self.m_xml.controller, 'R1-01-B - Controller Xml')) interfaceXml.read_interface_xml(l_controllers[0], self.m_xml.controller) print(PrettyFormatAny.form(l_controllers[0], 'R1-01-C - Controller Obj'))
def test_01_Family(self): """ Did we get everything set up for the rest of the tests of this class. """ l_xml = self.m_xml.light_sect[0] print(PrettyFormatAny.form(l_xml, 'C3-01-A - XML')) l_device = self.m_device_obj l_light = FamUtil.read_family_data(self.m_pyhouse_obj, l_device, l_xml) print(PrettyFormatAny.form(l_light, 'C3-01-B - Light')) self.assertEqual(l_device.Name, TESTING_LIGHT_NAME_0) self.assertEqual(l_light.InsteonAddress, conversions.dotted_hex2int(TESTING_INSTEON_ADDRESS_0))
def test_03_Add(self): """ Add a new o the config K,V """ print(PrettyFormatAny.form(self.m_rooms, 'C2-03-A - Rooms', 190)) print(PrettyFormatAny.form(self.m_rooms[4], 'C2-03-B - Room4', 190)) setattr(self.m_rooms[4], 'NewKey', 'A new value') print(PrettyFormatAny.form(self.m_rooms[4], 'C2-03-C - Room4', 190)) l_data = config_tools.Yaml(self.m_pyhouse_obj).dump_string(self.m_rooms) print(l_data) pass
def test_01_Family(self): """ Did we get everything set up for the rest of the tests of this class. """ l_xml = self.m_xml.light_sect[0] print(PrettyFormatAny.form(l_xml, 'C3-01-A - XML')) l_device = self.m_device_obj l_light = FamUtil.read_family_data(self.m_pyhouse_obj, l_device, l_xml) print(PrettyFormatAny.form(l_light, 'C3-01-B - Light')) self.assertEqual(l_device.Name, TESTING_LIGHT_NAME_0) self.assertEqual(l_light.InsteonAddress, convert.dotted_hex2int(TESTING_INSTEON_ADDRESS_0))
def test_04_All(self): """ _test reading of entire device set. """ l_obj = panasonicXML.read_panasonic_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_obj, 'C1-04-A - Plugins.')) print(PrettyFormatAny.form(l_obj.Devices, 'C1-04-B - Devices')) print(PrettyFormatAny.form(l_obj.Devices[0], 'C1-04-C - Device 0')) self.assertEqual(l_obj.Active, True) self.assertEqual(l_obj.DeviceCount, 2) self.assertEqual(str(l_obj.Devices[0].Name), TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(str(l_obj.Devices[1].Name), TESTING_PANASONIC_DEVICE_NAME_1)
def test_02_Light(self): """ Did we get everything set up for the rest of the tests of this class. """ l_xml = self.m_xml.light_sect[1] print(PrettyFormatAny.form(l_xml, 'C3-02-A - XML')) l_device = self.m_device_obj l_light = deviceXML.read_base_device_object_xml(self.m_pyhouse_obj, l_device, l_xml) print(PrettyFormatAny.form(l_light, 'C3-02-B - Light')) self.assertEqual(l_light.Name, TESTING_LIGHT_NAME_1) self.assertEqual(l_device.RoomName, TESTING_LIGHT_ROOM_NAME_1) self.assertEqual(l_light.UPBAddress, conversions.dotted_hex2int(TESTING_INSTEON_ADDRESS_0))
def test_02_ExtractXML(self): l_controllers = self.m_ctlr_api.read_all_controllers_xml( self.m_pyhouse_obj) print( PrettyFormatAny.form(l_controllers[0], 'A3-02-A - First Controller Obj')) l_interface = interfaceXml.read_interface_xml(self.m_controller_obj, l_controllers[0]) print( PrettyFormatAny.form(l_interface, 'A3-02-B - First Controller Obj'))
def test_01_GetObjFromMsg(self): self.m_ctrlr._Message = MSG_50_A self.m_controllers = self.m_cntl_api.read_all_controllers_xml( self.m_pyhouse_obj) self.m_pyhouse_obj.House.Lighting.Controllers = self.m_controllers print( PrettyFormatAny.form(self.m_pyhouse_obj.House.Lighting, 'B1-01-A Lighting')) l_ctlr = self.m_pyhouse_obj.House.Lighting.Controllers[0] print(PrettyFormatAny.form(l_ctlr, 'B1-01-B Controller')) self.assertEqual(l_ctlr.Name, TESTING_CONTROLLER_NAME_0)
def test_02_ReadFile(self): """ Read the rooms.yaml config file """ l_node = config_tools.Yaml(self.m_pyhouse_obj).read_yaml(self.m_filename) l_yaml = l_node.Yaml l_yamlrooms = l_yaml['Rooms'] print(PrettyFormatAny.form(l_node, 'C1-02-A - Node')) print(PrettyFormatAny.form(l_yaml, 'C1-02-B - Yaml')) print(PrettyFormatAny.form(l_yamlrooms, 'C1-02-C - YamlRooms')) self.assertEqual(l_yamlrooms[0]['Name'], 'Outside') self.assertEqual(len(l_yamlrooms), 5)
def test_04_All(self): """ test reading of entire device set. """ l_obj = panasonicXML.read_panasonic_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_obj, 'C1-04-A - Plugins.')) print(PrettyFormatAny.form(l_obj.Devices, 'C1-04-B - Devices')) print(PrettyFormatAny.form(l_obj.Devices[0], 'C1-04-C - Device 0')) self.assertEqual(l_obj.Active, True) self.assertEqual(l_obj.Count, 2) self.assertEqual(str(l_obj.Devices[0].Name), TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(str(l_obj.Devices[1].Name), TESTING_PANASONIC_DEVICE_NAME_1)
def eb_got_protocol(p_reason, p_device_obj, p_status): p_device_obj._Protocol = None p_device_obj._isRunning = False p_status.Type = 'UnConnected' p_status.Connected = False l_topic = 'house/entertainment/pioneer/status' self.m_pyhouse_obj._APIs.Core.MqttAPI.MqttPublish( l_topic, p_status) LOG.debug('Got an error connecting to Pioneer device - {}'.format( p_reason)) LOG.debug(PrettyFormatAny.form(p_device_obj, 'Device')) LOG.debug(PrettyFormatAny.form(p_device_obj.Host, 'Host'))
def test_02_Light(self): """ Did we get everything set up for the rest of the tests of this class. """ l_xml = self.m_xml.light_sect[1] print(PrettyFormatAny.form(l_xml, 'C3-02-A - XML')) l_device = self.m_device_obj l_light = deviceXML.read_base_device_object_xml(l_device, l_xml) print(PrettyFormatAny.form(l_light, 'C3-02-B - Light')) self.assertEqual(l_light.Name, TESTING_LIGHT_NAME_1) self.assertEqual(l_device.RoomName, TESTING_LIGHT_ROOM_NAME_1) self.assertEqual(l_light.UPBAddress, convert.dotted_hex2int(TESTING_INSTEON_ADDRESS_0))
def test_05_Data(self): """ test that the data structure is correct. """ l_obj = panasonicXML.read_panasonic_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_obj, 'C1-05-A - Read')) l_base = self.m_pyhouse_obj.House.Entertainment print(PrettyFormatAny.form(l_base, 'C1-05-B1 - Base')) print(PrettyFormatAny.form(l_base.Plugins, 'C1-05-B2 - Plugins')) print(PrettyFormatAny.form(l_base.Plugins[SECTION], 'C1-05-B2 - Plugins[SECTION]')) self.assertEqual(l_obj.Type, TESTING_PANASONIC_TYPE) self.assertEqual(l_base.Plugins[SECTION].Name, SECTION) self.assertEqual(l_base.Plugins[SECTION].Devices[0].Name, TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(l_base.Plugins[SECTION].Devices[1].Name, TESTING_PANASONIC_DEVICE_NAME_1)
def test_05_Data(self): """ _test that the data structure is correct. """ l_obj = panasonicXML.read_panasonic_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_obj, 'C1-05-A - Read')) l_base = self.m_pyhouse_obj.House.Entertainment print(PrettyFormatAny.form(l_base, 'C1-05-B1 - Base')) print(PrettyFormatAny.form(l_base.Plugins, 'C1-05-B2 - Plugins')) print(PrettyFormatAny.form(l_base.Plugins[SECTION], 'C1-05-B2 - Plugins[SECTION]')) self.assertEqual(l_obj.Type, TESTING_PANASONIC_TYPE) self.assertEqual(l_base.Plugins[SECTION].Name, SECTION) self.assertEqual(l_base.Plugins[SECTION].Devices[0].Name, TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(l_base.Plugins[SECTION].Devices[1].Name, TESTING_PANASONIC_DEVICE_NAME_1)
def test_04_Add(self): l_obj_0 = UuidData() l_obj_0.UUID = '12345678-dead-beef-dead-fedcba987654' l_obj_0.UuidType = 'House' Uuid.add_uuid(self.m_pyhouse_obj, l_obj_0) print(PrettyFormatAny.form(self.m_pyhouse_obj.Uuids, 'B2-04-A - one')) self.assertEqual(self.m_pyhouse_obj.Uuids.All[l_obj_0.UUID].UuidType, l_obj_0.UuidType) # l_obj_1 = UuidData() l_obj_1.UUID = '01234567-dead-beef-dead-fedcba987654' l_obj_1.UuidType = 'Room' Uuid.add_uuid(self.m_pyhouse_obj, l_obj_1) print(PrettyFormatAny.form(self.m_pyhouse_obj.Uuids.All, 'B2-04-B - two')) self.assertEqual(self.m_pyhouse_obj.Uuids.All[l_obj_1.UUID].UuidType, l_obj_1.UuidType)
def get_lights(self, p_body): """ See Docs/Design.md for the JSON returned. """ LOG.debug('{}'.format(p_body)) return try: # l_json = jsonpickle.decode(p_body) l_json = p_body except Exception as e_err: LOG.error('Error - {}\n{}'.format( e_err, PrettyFormatAny.form(l_json, "HUE ERROR", 190))) # LOG.debug('Got Lights {}'.format(PrettyFormatAny.form(l_json, 'Lights', 190))) for l_light_obj in l_json.items(): l_light = HueLightData() LOG.debug('Light: {}'.format( PrettyFormatAny.form(l_light_obj, 'Light', 190))) for l_key, l_value in l_light_obj[1].items(): l_light.HueLightIndex = l_light_obj[0] l_light.Key = l_light_obj[0] # l_light.Active = True l_light.Family.Name = 'Hue' l_light.DeviceType = 'Lighting' # Lighting l_light.DeviceSubType = 'Light' l_light.ControllerName = 'Hue Hub' l_light.LastUpdate = datetime.datetime.now() l_light.IsDimmable = True # LOG.debug('Add Light: {} {}'.format(l_key, PrettyFormatAny.form(l_value, 'Light', 190))) if l_key == 'name': l_light.Name = l_value # LOG.debug('Add Light {}'.format(PrettyFormatAny.form(l_light, 'Light', 190))) if l_key == 'type': l_light.Comment = l_value if l_key == 'uniqueid': l_light.HueUniqueId = l_value if l_key == 'state': l_state = False for l_st_key, l_st_val in l_value.items(): if l_st_key == 'on': l_state = l_st_val if l_st_key == 'bri': l_bri = l_st_val if l_state == True: l_light.BrightnessPct = int(l_bri / 2.54) else: l_light.BrightnessPct = 0 LOG.debug('Add Light {}'.format( PrettyFormatAny.form(l_light, 'Light', 190))) self._add_light(l_light)
def Start(self): """ Start the hub(bridge) and then get the hub data @param p_bridge_obj: is PyHouse_Obj.Computers.Bridges.xxx with xxx being a HueHub """ LOG.debug(PrettyFormatAny.form(self.m_pyhouse_obj, 'PyHouse', 190)) LOG.debug( PrettyFormatAny.form(self.m_pyhouse_obj.Computer, 'Computer', 190)) LOG.debug(PrettyFormatAny.form(self.m_pyhouse_obj.House, 'House', 190)) for l_bridge_obj in self.m_pyhouse_obj: l_bridge_obj._Queue = Queue(32) self.m_bridge_obj = l_bridge_obj self._get_all_config() LOG.debug('Started')
def test_02_ExtractXML(self): l_controllers = self.m_ctlr_api.read_all_controllers_xml( self.m_pyhouse_obj) l_interface = interfaceXml.read_interface_xml(self.m_controller_obj, l_controllers[0]) print( PrettyFormatAny.form(l_controllers[0], 'W1-02-A - Controller Obj')) print(PrettyFormatAny.form(l_interface, 'W1-02-B - Controller Obj')) self.assertEqual(l_xml.attrib['Name'], TESTING_LIGHT_NAME_0) self.assertEqual(l_xml.attrib['Key'], TESTING_LIGHT_KEY_0) self.assertEqual(l_xml.attrib['Active'], TESTING_LIGHT_ACTIVE_0) self.assertEqual(l_xml.find('UUID').text, TESTING_LIGHT_UUID_0) self.assertEqual(l_xml.find('Comment').text, TESTING_LIGHT_COMMENT_0) self.assertEqual( l_xml.find('DeviceFamily').text, TESTING_DEVICE_FAMILY_INSTEON)
def test_02_Xml1(self): """ Did we get the XML correctly """ l_xml = self.m_xml.light_sect[1] print(PrettyFormatAny.form(l_xml, 'C1-02-A - XML')) self.assertEqual(l_xml.attrib['Name'], TESTING_LIGHT_NAME_1) self.assertEqual(l_xml.find('DeviceFamily').text, TESTING_DEVICE_FAMILY_UPB)
def startedConnecting(self, p_connector): """ *1 Called when we are connecting to the device. Provides access to the connector. """ LOG.debug(PrettyFormatAny.form(p_connector, 'Connector')) self.m_pioneer_device_obj._Connector = p_connector
def test_03_AllDevices(self): """ Write the entire PandoraSection XML """ l_xml = pandoraXml.write_pandora_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_xml, 'D1-03-A - All Devices')) self.assertEqual(l_xml.attrib['Active'], TESTING_PANDORA_ACTIVE) self.assertEqual(l_xml.find('Type').text, TESTING_PANDORA_DEVICE_TYPE_0)
def _import_all_found_modules(self): """ Now we know what we need, load and run just those modules. """ for l_module in self.m_module_needed: if l_module in self.m_debugging_skip: LOG.warn('Skip import (for debugging) of module "{}"'.format( l_module)) continue # LOG.debug('Starting import of Module: "{}"'.format(l_module)) l_package = 'Modules.House.' + l_module.capitalize( ) # p_family_obj.PackageName # contains e.g. 'Modules.Families.Insteon' l_name = l_package + '.' + l_module.lower() try: l_ret = importlib.import_module(l_name, package=l_package) except ImportError as e_err: l_msg = 'ERROR importing module: {}\n\tErr:{}.'.format( l_module, e_err) LOG.error(l_msg) l_ret = None try: l_api = l_ret.API(self.m_pyhouse_obj) except Exception as e_err: LOG.error( 'ERROR - Initializing Module: "{}"\n\tError: {}'.format( l_module, e_err)) LOG.error('Ref: {}'.format( PrettyFormatAny.form(l_ret, 'ModuleRef', 190))) l_api = None # LOG.debug('Imported: {}'.format(l_ret)) l_api_name = l_module.capitalize() + 'API' l_house = self.m_pyhouse_obj._APIs.House setattr(l_house, l_api_name, l_api) # LOG.debug(PrettyFormatAny.form(l_house, 'House')) # LOG.debug(PrettyFormatAny.form(self.m_module_needed, 'Modules', 190)) LOG.info('Loaded Modules: {}'.format(self.m_module_needed))
def test_01_Import(self): l_mod_name = 'Insteon_device' l_mod_ref = None l_obj = familyUtil()._create_api_instance(self.m_pyhouse_obj, l_mod_name, l_mod_ref) print(PrettyFormatAny.form(l_obj, 'C2-01-A - Module')) self.assertNotEqual(l_obj, None)
def __init__(self, p_pyhouse_obj, p_device_obj): self.m_pyhouse_obj = p_pyhouse_obj self.m_pioneer_device_obj = p_device_obj LOG.debug('Factory init for {}'.format( PrettyFormatAny.form(self.m_pioneer_device_obj, 'PioneerFactory-'))) LOG.info('PioneerProtocol Init - Version:{}'.format(__version__))
def decode(self, p_topic, p_message): """ Decode the Mqtt message ==> pyhouse/<house name>/house/entertainment/onkyo/<type> <type> = control, status @param p_topic: is the topic with pyhouse/housename/entertainment/onkyo stripped off. @param p_message: is the body of the json message string. """ LOG.debug('Decode called:\n\tTopic:{}\n\tMessage:{}'.format( p_topic, p_message)) l_logmsg = ' Onkyo-{}'.format(p_topic[0]) self.m_sender = extract_tools.get_mqtt_field(p_message, 'Sender') self.m_model = extract_tools.get_mqtt_field(p_message, 'Model') # self.m_device = self._find_model(SECTION, self.m_model) if p_topic[0].lower() == 'control': l_logmsg += '\tControl: {}\n'.format( self._decode_control(p_topic, p_message)) elif p_topic[0].lower() == 'status': l_logmsg += '\tStatus: {}\n'.format( self._decode_status(p_topic, p_message)) else: l_logmsg += '\tUnknown Onkyo sub-topic: {} Message: {}'.format( p_topic, PrettyFormatAny.form(p_message, 'Entertainment msg', 160)) LOG.warn('Unknown Onkyo Topic: {}'.format(p_topic[0])) return l_logmsg
def _copy_to_yaml(self, p_pyhouse_obj): """ Create or Update the yaml information. The information in the YamlTree is updated to be the same as the running pyhouse_obj info. The running info is a dict and the yaml is a list! @return: the updated yaml ready information. """ try: l_node = p_pyhouse_obj._Config.YamlTree[CONFIG_FILE_NAME] l_config = l_node.Yaml['Lighting'] except: l_node = config_tools.Yaml(p_pyhouse_obj).create_yaml_node( 'Lighting') l_config = l_node.Yaml['Lighting'] LOG.debug(PrettyFormatAny.form(p_pyhouse_obj.House, 'PyHouseObj', 190)) l_working = p_pyhouse_obj.House.Lighting.Lights for l_key in [ l_attr for l_attr in dir(l_working) if not l_attr.startswith('_') and not callable(getattr(l_working, l_attr)) ]: l_val = getattr(l_working, l_key) setattr(l_config, l_key, l_val) p_pyhouse_obj._Config.YamlTree[CONFIG_FILE_NAME].Yaml[ 'Lighting'] = l_config l_ret = {'Lighting': l_config} return l_ret
def test_03_ExtractSched(self): """ Extract one room info from the yaml """ l_node = config_tools.Yaml(self.m_pyhouse_obj).read_yaml(CONFIG_FILE_NAME) l_yaml = l_node.Yaml l_sched = self.m_config._extract_light_schedule(l_yaml['Schedules'][0]['Light']) print(PrettyFormatAny.form(l_sched, 'F1-03-A - Sched', 190))
def _save_light(self, p_obj, p_json): LOG.info(PrettyFormatAny.form(p_json, 'JSON')) p_obj.Level = int(p_json['Level']) p_obj.LightName = p_json['LightName'] p_obj.Rate = p_json['Rate'] p_obj.RoomName = p_json['RoomName'] return p_obj
def test_01_House(self): l_house_obj = houseXml.read_house_xml(self.m_pyhouse_obj) self.m_pyhouse_obj.House = l_house_obj l_xml = houseXml.write_house_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_xml, 'C3-01-A - XML')) self.assertEqual(l_xml.tag, 'HouseDivision') self.assertEqual(l_xml.attrib['Name'], TESTING_HOUSE_NAME) self.assertEqual(l_xml.find('UUID').text, TESTING_HOUSE_UUID)
def test_01_Init(self): """ Test that the data structure is correct. """ self.m_pyhouse_obj.House.Entertainment.Plugins[SECTION] = EntertainmentPluginData() print(PrettyFormatAny.form(self.m_pyhouse_obj.House.Entertainment.Plugins[SECTION], 'E1-01-D - Section', 180)) l_base = self.m_pyhouse_obj.House.Entertainment.Plugins[SECTION] self.assertIsNone(l_base._API) self.assertEqual(l_base.Active, False) self.assertEqual(l_base.Count, 0)
def test_01_Data(self): """ test that the data structure is correct. """ l_base = self.m_pyhouse_obj.House.Entertainment.Plugins[SECTION] print(PrettyFormatAny.form(l_base, 'C1-05-B1 - Base')) self.assertEqual(l_base.Type, TESTING_PANASONIC_TYPE) self.assertEqual(l_base.Name, SECTION) self.assertEqual(l_base.Devices[0].Name, TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(l_base.Devices[1].Name, TESTING_PANASONIC_DEVICE_NAME_1)
def _open_find_device(p_USB_obj): """First step in opening a USB device. @return: None if no such device or a pyusb device object """ l_vpn = Utility.format_names(p_USB_obj) l_device = None try: l_device = usb.core.find(idVendor=p_USB_obj.Vendor, idProduct=p_USB_obj.Product) except (usb.USBError, ValueError): LOG.error("ERROR no such USB device for {}".format(l_vpn)) return None if l_device == None: LOG.error('ERROR - USB device not found {}'.format(l_vpn)) return None LOG.debug(PrettyFormatAny.form(l_device, 'Device')) LOG.debug(PrettyFormatAny.form(p_USB_obj, 'pUSB_obj')) p_USB_obj.UsbDevice = API._save_find_device(p_USB_obj, l_device) LOG.info('Found a device - HID: {}'.format(l_vpn)) return l_device
def test_03_Pool1(self): """ Write one entire pool XML """ l_xml = poolXml._write_one_pool(self.m_pools[1]) print(PrettyFormatAny.form(l_xml, 'W1-03-A - Pool 1')) self.assertEqual(l_xml.attrib['Name'], TESTING_POOL_NAME_1) self.assertEqual(l_xml.attrib['Key'], TESTING_POOL_KEY_1) self.assertEqual(l_xml.attrib['Active'], TESTING_POOL_ACTIVE_1) self.assertEqual(l_xml.find('Comment').text, TESTING_POOL_COMMENT_1) self.assertEqual(l_xml.find('PoolType').text, TESTING_POOL_TYPE_1)
def decode(self, p_topic, p_message): """ Decode the Mqtt message ==> pyhouse/<house name>/hvac/<type>/<Name>/... <type> = thermostat, ... """ l_logmsg = '\tHVAC:\n' if p_topic[1] == 'Thermostat': l_logmsg += '\tThermostat: {}\n'.format(self._get_field(p_message, 'Name')) else: l_logmsg += '\tUnknown sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Security msg', 160)) return l_logmsg
def test_02_Line(self): """ Test that the data structure is correct. """ l_obj = PandoraStatusData() l_res = PianoBarProcessControl(self.m_pyhouse_obj)._extract_nowplaying(l_obj, PLAY_LN) print(PrettyFormatAny.form(l_obj, 'F2-02-A - Status', 180)) self.assertEqual(l_res.Album, 'Greatest Hits') self.assertEqual(l_res.Artist, 'Dave Koz') self.assertEqual(l_res.Likability, '3') self.assertEqual(l_res.Song, 'Love Is On The Way') self.assertEqual(l_res.Station, 'Smooth Jazz Radio')
def test_04_AllPools(self): """ Write Pool Section with all pools. """ l_xml, l_count = poolXml.write_all_pools_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_xml, 'W1-04-A - Pool')) # l_xml1 = l_xml.find('Pool') l_xml2 = l_xml[0] self.assertEqual(l_xml2.attrib['Name'], TESTING_POOL_NAME_0) self.assertEqual(l_xml2.attrib['Key'], TESTING_POOL_KEY_0) self.assertEqual(l_xml2.attrib['Active'], TESTING_POOL_ACTIVE_0) self.assertEqual(l_xml2.find('Comment').text, TESTING_POOL_COMMENT_0) self.assertEqual(l_xml2.find('PoolType').text, TESTING_POOL_TYPE_0)
def test_04_All(self): """Test the write for proper XML elements """ l_xml = panasonicXML.write_panasonic_section_xml(self.m_pyhouse_obj) print(PrettyFormatAny.form(l_xml, 'D1-03-A - XML')) self.assertEqual(l_xml.find('Type').text, TESTING_PANASONIC_TYPE) self.assertEqual(l_xml[1].attrib['Name'], TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(l_xml[1].attrib['Key'], TESTING_PANASONIC_DEVICE_KEY_0) self.assertEqual(l_xml[1].attrib['Active'], TESTING_PANASONIC_DEVICE_ACTIVE_0) self.assertEqual(l_xml[2].attrib['Name'], TESTING_PANASONIC_DEVICE_NAME_1) self.assertEqual(l_xml[2].attrib['Key'], TESTING_PANASONIC_DEVICE_KEY_1) self.assertEqual(l_xml[2].attrib['Active'], TESTING_PANASONIC_DEVICE_ACTIVE_1)
def _initilaize_pim(p_controller_obj): """Initialize a new UPBData object. """ l_pim = UPBData() l_pim.InterfaceType = p_controller_obj.InterfaceType l_pim.Name = p_controller_obj.Name l_pim.UPBAddress = p_controller_obj.UPBAddress l_pim.UPBPassword = p_controller_obj.UPBPassword l_pim.UPBNetworkID = p_controller_obj.UPBNetworkID LOG.info('Initializing UPB PIM named: {}, Type={}'.format(l_pim.Name, l_pim.InterfaceType)) LOG.debug(PrettyFormatAny.form(l_pim, 'PIM data')) return l_pim
def decode(self, p_topic, p_message): """ Decode the Mqtt message ==> pyhouse/<house name>/security/<type>/<Name> <type> = garage door, motion sensor, camera """ l_logmsg = '\tSecurity:\n' if p_topic[1] == 'garage_door': l_logmsg += '\tGarage Door: {}\n'.format(self._get_field(p_message, 'Name')) elif p_topic[1] == 'motion_sensor': l_logmsg += '\tMotion Sensor:{}\n\t{}'.format(self._get_field(p_message, 'Name'), self._get_field(p_message, 'Status')) else: l_logmsg += '\tUnknown sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Security msg', 160)) return l_logmsg
def test_01_Device0(self): """ Did we get the Device correctly (Insteon) """ self.m_device_obj.DeviceFamily = TESTING_FAMILY_NAME_1 l_device = self.m_device_obj print(PrettyFormatAny.form(l_device, 'C2-01-A - Device')) self.assertEqual(l_device.Name, TESTING_LIGHT_NAME_0) self.assertEqual(l_device.Key, TESTING_LIGHT_KEY_0) self.assertEqual(l_device.Active, TESTING_LIGHT_ACTIVE_0) self.assertEqual(l_device.DeviceFamily, TESTING_DEVICE_FAMILY_INSTEON) self.assertEqual(str(l_device.DeviceType), TESTING_LIGHT_DEVICE_TYPE_0) self.assertEqual(str(l_device.DeviceSubType), TESTING_LIGHT_DEVICE_SUBTYPE_0) self.assertEqual(l_device.RoomName, TESTING_LIGHT_ROOM_NAME_0)
def test_02_Device1(self): """ Did we get the Device correctly (UPB) """ self.m_device_obj.DeviceFamily = TESTING_FAMILY_NAME_2 self.m_api = FamUtil._get_family_device_api(self.m_pyhouse_obj, self.m_device_obj) print(PrettyFormatAny.form(self.m_device_obj, 'C2-02-A - Device')) self.assertEqual(self.m_device_obj.Name, TESTING_LIGHT_NAME_1) self.assertEqual(self.m_device_obj.Key, TESTING_LIGHT_KEY_1) self.assertEqual(self.m_device_obj.Active, TESTING_LIGHT_ACTIVE_1) self.assertEqual(self.m_device_obj.DeviceFamily, TESTING_DEVICE_FAMILY_UPB) self.assertEqual(str(self.m_device_obj.DeviceType), TESTING_LIGHT_DEVICE_TYPE_0) self.assertEqual(str(self.m_device_obj.DeviceSubType), TESTING_LIGHT_DEVICE_SUBTYPE_0) self.assertEqual(self.m_device_obj.RoomName, TESTING_LIGHT_ROOM_NAME_0)
def _decode_room(self, p_topic, p_message): l_logmsg = '\tRooms:\n' if p_topic[1] == 'add': l_logmsg += '\tName: {}\n'.format(self._get_field(p_message, 'Name')) elif p_topic[1] == 'delete': l_logmsg += '\tName: {}\n'.format(self._get_field(p_message, 'Name')) elif p_topic[1] == 'sync': l_logmsg += '\tName: {}\n'.format(self._get_field(p_message, 'Name')) elif p_topic[1] == 'update': l_logmsg += '\tName: {}\n'.format(self._get_field(p_message, 'Name')) else: l_logmsg += '\tUnknown sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Rooms msg', 160)) return l_logmsg
def decode(self, p_topic, p_message): """ Decode the Mqtt message ==> pyhouse/<house name>/entertainment/samsung/<type>/<Name>/... <type> = ? """ if self.m_API == None: # LOG.debug('Decoding initializing') self.m_API = API(self.m_pyhouse_obj) l_logmsg = '' if p_topic[2] == 'control': l_logmsg += '\tSamsung: {}\n'.format(self._decode_control(p_topic, p_message)) else: l_logmsg += '\tUnknown Samsung sub-topic {}'.format(PrettyFormatAny.form(p_message, 'Entertainment msg', 160)) return l_logmsg
def Start(self): """ Start all the Onkyo factories if we have any Onkyo devices. """ l_count = 0 l_mfg = self.m_pyhouse_obj.House.Entertainment.Plugins[SECTION] print(PrettyFormatAny.form(l_mfg, 'onkyo.Start() Plugins')) for l_onkyo_obj in l_mfg.Devices.values(): if not l_onkyo_obj.Active: continue l_host = l_onkyo_obj.IPv4 l_port = l_onkyo_obj.Port l_onkyo_obj._Factory = OnkyoFactory(self.m_pyhouse_obj, l_onkyo_obj) _l_connector = self.m_pyhouse_obj.Twisted.Reactor.connectTCP(l_host, l_port, l_onkyo_obj._Factory) LOG.info("Started Onkyo {} {}".format(l_host, l_port)) LOG.info("Started {} Onkyo devices".format(l_count))
def test_03_Base(self): """Test the write for proper XML elements """ l_obj = self.m_pyhouse_obj.House.Entertainment print(PrettyFormatAny.form(self.m_pyhouse_obj.House, 'E1-01-A - House')) print(PrettyFormatAny.form(l_obj, 'E1-01-B - Entertainment')) print(PrettyFormatAny.form(l_obj.Plugins, 'E1-01-C - Plugins')) print(PrettyFormatAny.form(l_obj.Plugins[SECTION], "E1-01-D - Plugins['panasonic]")) print(PrettyFormatAny.form(l_obj.Plugins[SECTION].Devices, "E1-01-E - Devices")) print(PrettyFormatAny.form(l_obj.Plugins[SECTION].Devices[0], "E1-01-F - Devices"))
def _get_family_obj(p_pyhouse_obj, p_device_obj): """ Given some device object, extract the Family object using .DeviceFamily """ l_family_name = p_device_obj.DeviceFamily try: l_family_obj = p_pyhouse_obj.House.FamilyData[l_family_name] except KeyError as e_err: l_msg = PrettyFormatAny.form(p_pyhouse_obj.House, ' House Information', 40) LOG.error('Could not get family object for:\n\tDevice Name:\t{}\n\tFamily:\t\t{}\n\tKey Error:\t{}{}' .format(p_device_obj.Name, l_family_name, e_err, l_msg)) if l_family_name == 'Null': p_pyhouse_obj.House.FamilyData['Null'] = FamilyData() p_pyhouse_obj.House.FamilyData['Null'].Name = 'Null' l_family_obj = p_pyhouse_obj.House.FamilyData['Null'] return l_family_obj
def test_02_Base(self): """Test the write for proper XML elements """ # print(PrettyFormatAny.form(self.m_panasonic, 'D1-01-A - Plugin')) l_xml = panasonicXML._write_device(self.m_panasonic.Devices[0]) print(PrettyFormatAny.form(l_xml, 'D1-01-B - XML')) self.assertEqual(l_xml.attrib['Name'], TESTING_PANASONIC_DEVICE_NAME_0) self.assertEqual(l_xml.attrib['Key'], TESTING_PANASONIC_DEVICE_KEY_0) self.assertEqual(l_xml.attrib['Active'], TESTING_PANASONIC_DEVICE_ACTIVE_0) self.assertEqual(l_xml.find('UUID').text, TESTING_PANASONIC_DEVICE_UUID_0) self.assertEqual(l_xml.find('Comment').text, TESTING_PANASONIC_DEVICE_COMMENT_0) # self.assertEqual(l_xml.find('IPv4').text, TESTING_PANASONIC_DEVICE_IPV4_0) self.assertEqual(l_xml.find('Port').text, TESTING_PANASONIC_DEVICE_PORT_0) self.assertEqual(l_xml.find('RoomName').text, TESTING_PANASONIC_DEVICE_ROOM_NAME_0) self.assertEqual(l_xml.find('RoomUUID').text, TESTING_PANASONIC_DEVICE_ROOM_UUID_0) self.assertEqual(l_xml.find('Type').text, TESTING_PANASONIC_DEVICE_TYPE_0) self.assertEqual(l_xml.find('Volume').text, TESTING_PANASONIC_DEVICE_VOLUME_0)