def test_prefix_slash(self): settings = { "client": self.mock, "prefix": "/testname/" } config.PERSISTENCE = [persistence.Redis(**settings)] persistence.set("lights/fan", "value") self.mock.set.assert_called_once_with("/testname/lights/fan", "value") self.mock.publish.assert_called_once_with("/testname/lights/fan", "value")
def test_retain_default_true(self): settings = { "_publish": self.mock, } config.PERSISTENCE = [persistence.MQTT(**settings)] persistence.set("lights/fan", "value") call_args = self.mock.single.call_args self.assertEqual(call_args[0][0], "lights/fan") self.assertEqual(call_args[1]['retain'], True)
def test_set_no_publish(self): settings = { "client": self.mock, "publish": False, } config.PERSISTENCE = [persistence.Redis(**settings)] persistence.set("lights/fan", "value") self.mock.set.assert_called_once_with("lights/fan", "value") self.mock.publish.assert_not_called()
def test_prefix_slash(self): settings = { "_publish": self.mock, "prefix": "/testname/" } config.PERSISTENCE = [persistence.MQTT(**settings)] persistence.set("lights/fan", "value") call_args = self.mock.single.call_args self.assertEqual(call_args[0][0], "/testname/lights/fan") self.assertEqual(call_args[1]['payload'], "value")
def test_set_custom_prefix(self): settings = { "_publish": self.mock, "discovery_prefix": "myhass", } config.PERSISTENCE = [persistence.MQTTHomeAssistant(**settings)] persistence.set("lights/fan", "value") call_args = self.mock.single.call_args self.assertEqual(call_args[0][0], "myhass/light/lights_fan/state") self.assertEqual(call_args[1]['payload'], "value")
def test_set_only_publish(self): settings = { "client": self.mock, "db": None, } config.PERSISTENCE = [persistence.Redis(**settings)] persistence.set("lights/fan", "value") self.assertEqual(None, persistence.get("lights/fan")) self.mock.set.assert_not_called() self.mock.get.assert_not_called() self.mock.publish.assert_called_once_with("lights/fan", "value")
def _set(self, state): """ Saves the given state to persistence """ persistence.set(self._state_path(), state) pubsub.publish({'device': self, 'state': state})
def test_failure(self): # should swallow errors when setting import paho.mqtt self.mock.single.side_effect = paho.mqtt.MQTTException("Failure") persistence.set("lights/fan", "value")
def test_set(self): persistence.set("lights/fan", "value") call_args = self.mock.single.call_args self.assertEqual(call_args[0][0], "lights/fan") self.assertEqual(call_args[1]['payload'], "value")
def test_saved_to_right_place(self): key = "key_name" data = "Test" persistence.set(key, data) dir_contents = os.listdir(self._dirname) self.assertEqual(set([key]), set(dir_contents))
def test_failure(self): # should swallow errors when setting self.mock.get.side_effect = IOError("Failure") self.mock.set.side_effect = IOError("Failure") persistence.get("lights/fan") persistence.set("lights/fan", "value")
def test_get_wrong_name(self): key = "key_name" data = "Test" persistence.set(key, data) self.assertEqual(None, persistence.get(key+"Wrong"))
def test_set(self): persistence.set("lights/fan", "value") self.mock.set.assert_called_once_with("lights/fan", "value") self.mock.publish.assert_called_once_with("lights/fan", "value")
def test_save(self): key = "key_name" data = "Test" persistence.set(key, data) self.assertEqual(data, persistence.get(key))