我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用json.htm()。
def switch(self, state, idx): """Switch the device in Domoticz.""" try: f = urllib.urlopen(self.url + "/json.htm?type=command¶m=switchlight&idx=" + str(idx) + "&switchcmd=" + str(state).title()) response = f.read() LOGGER.debug(str(response)) return response except IOError as e: LOGGER.error(str(e) + ' : ' + str(e.read()))
def get(self, idx): """Get the device's data in Domoticz.""" try: f = urllib.urlopen(self.url + "/json.htm?type=devices&rid=" + str(idx)) response = f.read() return json.loads(response) except IOError as e: LOGGER.error(str(e) + ' : ' + str(e.read()))
def volgendeZonondergang(self): # sunrise from domoticz... But I don't know how to retrieve it.... try: domoticzurl = 'https://127.0.0.1:8443/json.htm?type=command¶m=getSunRiseSet' encoding = 'utf-8' inlog = '%s:%s' % (self.domoticzusername, self.domoticzpassword) base64string = base64.b64encode(inlog.encode(encoding)).decode(encoding) request = urllib.request.Request(domoticzurl) request.add_header("Authorization", "Basic %s" % base64string) response = urllib.request.urlopen(request) data = response.read() JSON_object = json.loads(data.decode(encoding)) time = JSON_object['Sunset'].split(':') now = datetime.now() ret = datetime(now.year, now.month, now.day, int(time[0]), int(time[1]), 0) # when started after sunset use 'now' now = now + timedelta(minutes = int(Parameters["Mode4"])) if (now > ret): ret = ret + timedelta(days = 1) return ret except Exception as e: self.LogError("Error retrieving Sunset: "+ str(e)) now = datetime.now() return datetime(now.year, now.month, now.day, 22, 0, 0)
def reportBell(): domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=switchlight&idx=" + str(domoticzidx) + "&switchcmd=On") time.sleep(mintimebetweenrings) domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=switchlight&idx=" + str(domoticzidx) + "&switchcmd=Off")
def test_get_all_variables_returns_five_vars(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars))) actual_vars = domo.get_all_variables() self.assertEqual(5, len(actual_vars)) self.assertListEqual(actual_vars, self.all_vars)
def test_get_variable_by_idx_returns_one(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=1', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([self.var_integer]))) var = domo.get_user_variable(idx=1) self.assertTrue(isinstance(var, int)) self.assertEqual(var, 1)
def test_get_variable_by_name_returns_dummyval(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars))) m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=3', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([self.var_str]))) var = domo.get_user_variable(name='var_string') self.assertTrue(isinstance(var, str)) self.assertEqual(var, 'dummyval')
def test_get_variable_float_returns_float(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=2', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([self.var_float]))) var = domo.get_user_variable(idx=2) self.assertTrue(isinstance(var, float)) self.assertEqual(var, 3.142)
def test_get_variable_date_returns_date(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=4', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([self.var_date]))) var = domo.get_user_variable(idx=4) self.assertTrue(isinstance(var, date)) self.assertEqual(var, date(2016,1,1))
def test_create_or_update_user_variable_returns_new_ok(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars))) m.get('http://mydomoticz:8080/json.htm?type=command¶m=saveuservariable&vname=var_new&vtype=2&vvalue=newvalue', text='{"status":"OK"}') result = domo.create_or_update_user_variable('var_new','newvalue') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=6', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([self.var_new]))) var = domo.get_user_variable(idx=6) self.assertTrue(isinstance(var, str)) self.assertEqual(var, 'newvalue')
def test_create_or_update_user_variable_returns_updates_value(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars))) m.get('http://mydomoticz:8080/json.htm?type=command¶m=saveuservariable&vname=var_str&vtype=2&vvalue=newvalue', text='{"status":"OK"}') m.get('http://mydomoticz:8080/json.htm?type=command¶m=updateuservariable&vname=var_str&vtype=2&vvalue=newvalue', text='{"status":"OK"}') result = domo.create_or_update_user_variable('var_str','newvalue') var_string = self.var_str.copy() var_string['Value'] = 'newvalue' m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=3', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps([var_string]))) var = domo.get_user_variable(idx=3) self.assertTrue(isinstance(var, str)) self.assertEqual(var, 'newvalue')
def test_delete_user_variable_returns_ok(self, m): domo = domoticz.Domoticz('mydomoticz') m.get('http://mydomoticz:8080/json.htm?type=command¶m=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars[1:]))) m.get('http://mydomoticz:8080/json.htm?type=command¶m=deleteuservariable&idx=1', text='{"status":"OK"}') result = domo.delete_user_variable(idx=1) self.assertEqual(result, 'OK') allvars = domo.get_all_variables() self.assertEqual(len(allvars), 4)
def test_base_url_returns_full_endpoint(self, m): domo = domoticz.Domoticz('mydomoticz') self.assertEqual(domo.base_url, 'http://mydomoticz:8080/json.htm?', 'Generated base URL is incorrect')
def test_device_id_map_returns_dict(self, m): m.get('http://mydomoticz:8080/json.htm?type-devices&filter=all&used=true&order=Name', text=json.dumps(self.device_data)) domo = domoticz.Domoticz('mydomoticz')
def test_select_device_idx_returns_device1(self, m): m.get('http://mydomoticz:8080/json.htm?type-devices&filter=all&used=true&order=Name', text=json.dumps(self.device_data)) domo = domoticz.Domoticz('mydomoticz') device = domo.select_device(idx=1) self.assertDictEqual(device[0],self.device_data['result'][0], 'Device1 not returned')
def test_select_device_name_returns_device2(self, m): m.get('http://mydomoticz:8080/json.htm?type-devices&filter=all&used=true&order=Name', text=json.dumps(self.device_data)) domo = domoticz.Domoticz('mydomoticz') device = domo.select_device(name='Device2') self.assertEqual(len(device), 1) self.assertDictEqual(device[0], self.device_data['result'][1])
def test_get_device_data_returns_device1(self, m): m.get('http://mydomoticz:8080/json.htm?type-devices&filter=all&used=true&order=Name', text=json.dumps(self.device_data)) domo = domoticz.Domoticz('mydomoticz') dev = domo.get_device_data(idx=1) self.assertEqual(len(dev), 1) self.assertDictEqual(dev[0], self.device_data['result'][0], 'Incorrect device returned')
def test_get_device_status_returns_both_devices(self, m): m.get('http://mydomoticz:8080/json.htm?filter=light&used=true&type=devices&order=Name', text=json.dumps(self.device_data)) domo = domoticz.Domoticz('mydomoticz') dev = domo.get_device_status_many('light') self.assertEqual(len(dev), 2) self.assertListEqual(dev, self.device_data['result'])