def test_update_with_no_template(self): """Test update when there is no value template.""" self.rest.update = Mock( 'rest.RestData.update', side_effect=self.update_side_effect('plain_state')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, None) self.sensor.update() self.assertEqual('plain_state', self.sensor.state)
def test_update_with_json_attrs_no_data(self, mock_logger): """Test attributes when no JSON result fetched.""" self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect(None)) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, None, ['key'], self.force_update) self.sensor.update() self.assertEqual({}, self.sensor.device_state_attributes) self.assertTrue(mock_logger.warning.called)
def test_update_with_json_attrs_not_dict(self, mock_logger): """Test attributes get extracted from a JSON result.""" self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect( '["list", "of", "things"]')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, None, ['key'], self.force_update) self.sensor.update() self.assertEqual({}, self.sensor.device_state_attributes) self.assertTrue(mock_logger.warning.called)
def test_update_with_json_attrs(self): """Test attributes get extracted from a JSON result.""" self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect( '{ "key": "some_json_value" }')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, None, ['key'], self.force_update) self.sensor.update() self.assertEqual('some_json_value', self.sensor.device_state_attributes['key'])
def test_update_with_no_template(self): """Test update when there is no value template.""" self.rest.update = Mock( 'rest.RestData.update', side_effect=self.update_side_effect('plain_state')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, self.device_class, None, [], self.force_update) self.sensor.update() assert 'plain_state' == self.sensor.state assert self.sensor.available
def test_update_with_json_attrs_bad_JSON(self, mock_logger): """Test attributes get extracted from a JSON result.""" self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect( 'This is text rather than JSON data.')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, self.device_class, None, ['key'], self.force_update) self.sensor.update() assert {} == self.sensor.device_state_attributes assert mock_logger.warning.called assert mock_logger.debug.called
def test_update_with_json_attrs_and_template(self): """Test attributes get extracted from a JSON result.""" self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect( '{ "key": "json_state_updated_value" }')) self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, self.device_class, self.value_template, ['key'], self.force_update) self.sensor.update() assert 'json_state_updated_value' == self.sensor.state assert 'json_state_updated_value' == \ self.sensor.device_state_attributes['key'], \ self.force_update
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.initial_state = 'initial_state' self.rest = Mock('rest.RestData') self.rest.update = Mock('rest.RestData.update', side_effect=self.update_side_effect( '{ "key": "' + self.initial_state + '" }')) self.name = 'foo' self.unit_of_measurement = 'MB' self.value_template = template('{{ value_json.key }}') self.value_template.hass = self.hass self.sensor = rest.RestSensor(self.hass, self.rest, self.name, self.unit_of_measurement, self.value_template)