Python json.htm() Examples

The following are code examples for showing how to use json.htm(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: domoticz_skill   Author: matleses   File: Domoticz.py    (license) View Source Project 5 votes vote down vote up
def switch(self, state, idx):
        """Switch the device in Domoticz."""
        try:
            f = urllib.urlopen(self.url + "/json.htm?type=command&param=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())) 
Example 2
Project: domoticz_skill   Author: matleses   File: Domoticz.py    (license) View Source Project 5 votes vote down vote up
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())) 
Example 3
Project: domoticz   Author: ericstaal   File: plugin.py    (license) View Source Project 5 votes vote down vote up
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&param=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) 
Example 4
Project: domoticz   Author: ericstaal   File: doorbell.py    (license) View Source Project 5 votes vote down vote up
def reportBell():
  domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + str(domoticzidx) + "&switchcmd=On")
  time.sleep(mintimebetweenrings)
  domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + str(domoticzidx) + "&switchcmd=Off") 
Example 5
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_get_all_variables_returns_five_vars(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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) 
Example 6
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_get_variable_by_idx_returns_one(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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) 
Example 7
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_get_variable_by_name_returns_dummyval(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars)))
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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') 
Example 8
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_get_variable_float_returns_float(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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) 
Example 9
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_get_variable_date_returns_date(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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)) 
Example 10
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
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&param=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars)))
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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&param=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') 
Example 11
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
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&param=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars)))
        m.get('http://mydomoticz:8080/json.htm?type=command&param=saveuservariable&vname=var_str&vtype=2&vvalue=newvalue', text='{"status":"OK"}')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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&param=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') 
Example 12
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
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&param=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars)))
        m.get('http://mydomoticz:8080/json.htm?type=command&param=saveuservariable&vname=var_str&vtype=2&vvalue=newvalue', text='{"status":"OK"}')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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&param=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') 
Example 13
Project: domoticz-slack-bot   Author: atharvai   File: test_user_variables.py    (license) View Source Project 5 votes vote down vote up
def test_delete_user_variable_returns_ok(self, m):
        domo = domoticz.Domoticz('mydomoticz')
        m.get('http://mydomoticz:8080/json.htm?type=command&param=getuservariables', text='{{"status":"OK", "result":{data}}}'.format(data=json.dumps(self.all_vars[1:])))
        m.get('http://mydomoticz:8080/json.htm?type=command&param=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) 
Example 14
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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') 
Example 15
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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') 
Example 16
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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') 
Example 17
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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]) 
Example 18
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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') 
Example 19
Project: domoticz-slack-bot   Author: atharvai   File: test_device.py    (license) View Source Project 5 votes vote down vote up
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'])