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'])
def test_get_sunriseset_returns_sunriseset(self, m): m.get( 'http://mydomoticz:8080/json.htm?type=command¶m=getSunRiseSet', text=json.dumps(self.sunrise_data)) domo = domoticz.Domoticz('mydomoticz') sriseset = domo.get_sunriseset() self.assertDictEqual(sriseset, self.sunrise_data, 'Incorrect SunRiseSet data')
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_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_get_variable_time_returns_time(self, m): domo = domoticz.Domoticz('mydomoticz') m.get( 'http://mydomoticz:8080/json.htm?type=command¶m=getuservariable&idx=5', text='{{"status":"OK", "result":{data}}}'.format( data=json.dumps([self.var_time]))) var = domo.get_user_variable(idx=5) self.assertTrue(isinstance(var, time.struct_time)) self.assertEqual(var, time.strptime('16:00', '%H:%M'))
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_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_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_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_cache_device_list(self, m): m.get( 'http://mydomoticz:8080/json.htm?type-devices&filter=all&used=true&order=Name', text= '{"result":[{"Type":"Light"}],"ServerTime":"2016-07-13 01:01:01"}') domo = domoticz.Domoticz('mydomoticz') domo._cache_device_list() self.assertEqual(len(domo.device_list), 1, 'device list is not as expected with 1 device')
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_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_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_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_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')
INTRO_MSG = 'How can I help?' HELP_MSG = 'I didn' 't quite understand that.' + '\n' + INTRO_MSG config = ConfigParser.ConfigParser() config.read('bot.config') BOT_ID = config.get('slack', 'bot_id') # constants AT_BOT = "<@" + BOT_ID + ">" # instantiate Slack clients slack_client = SlackClient(config.get('slack', 'token')) slack_notify = SlackNotify(config.get('slack', 'token')) domo = domoticz.Domoticz(config.get('domoticz', 'host')) domo._cache_device_list() def handle_command(command, channel): """ Receives commands directed at the bot and determines if they are valid commands. If so, then acts on the commands. If not, returns back what it needs for clarification. :param channel: :param command: """ response = 'Not sure what you mean. Sample commands to try `status all`, `status temp`, `temp`' command_grp, cmd = parse_command(command) if command_grp in command_groups: if cmd == '' and (len(commands[command_grp]) == 0