Exemple #1
0
 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.",
             CaseInsensitiveDict({"Content-Type": CONTENT_TYPE_TEXT_PLAIN}),
         ),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         ["key"],
         self.force_update,
         self.resource_template,
         self.json_attrs_path,
     )
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
     assert mock_logger.debug.called
Exemple #2
0
    def setUp(self):
        """Set up 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.device_class = None
        self.value_template = template("{{ value_json.key }}")
        self.value_template.hass = self.hass
        self.force_update = False
        self.resource_template = None

        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            self.value_template,
            [],
            self.force_update,
            self.resource_template,
        )
Exemple #3
0
    def test_update_with_application_xml_convert_json_attrs_with_jsonattr_template(
        self, ):
        """Test attributes get extracted from a JSON result that was converted from XML with application/xml mime type."""
        json_attrs_path = "$.main"
        value_template = template("{{ value_json.main.dog }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(
                "<main><dog>1</dog><cat>3</cat></main>",
                CaseInsensitiveDict({"Content-Type": "application/xml"}),
            ),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["dog", "cat"],
            self.force_update,
            self.resource_template,
            json_attrs_path,
        )

        self.sensor.update()
        assert "3" == self.sensor.device_state_attributes["cat"]
        assert "1" == self.sensor.device_state_attributes["dog"]
        assert "1" == self.sensor.state
Exemple #4
0
    def test_update_with_xml_convert_json_attrs_with_json_attrs_path(self):
        """Test attributes get extracted from a JSON result that was converted from XML with a template for the attributes."""
        json_attrs_path = "$.toplevel.second_level"
        value_template = template("{{ value_json.toplevel.master_value }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(
                "<toplevel><master_value>master</master_value><second_level><some_json_key>some_json_value</some_json_key><some_json_key2>some_json_value2</some_json_key2></second_level></toplevel>",
                CaseInsensitiveDict({"Content-Type": "text/xml+svg"}),
            ),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["some_json_key", "some_json_key2"],
            self.force_update,
            self.resource_template,
            json_attrs_path,
        )

        self.sensor.update()
        assert "some_json_value" == self.sensor.device_state_attributes[
            "some_json_key"]
        assert ("some_json_value2" ==
                self.sensor.device_state_attributes["some_json_key2"])
        assert "master" == self.sensor.state
Exemple #5
0
    def test_update_with_failed_get(self, mock_logger):
        """Test attributes get extracted from a XML result with bad xml."""
        value_template = template("{{ value_json.toplevel.master_value }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(None, None),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["key"],
            self.force_update,
            self.resource_template,
            self.json_attrs_path,
        )

        self.sensor.update()
        assert {} == self.sensor.device_state_attributes
        assert mock_logger.warning.called
        assert mock_logger.debug.called
        assert self.sensor.state is None
        assert self.sensor.available is False
Exemple #6
0
    def test_update_with_xml_convert_bad_xml(self, mock_logger):
        """Test attributes get extracted from a XML result with bad xml."""
        value_template = template("{{ value_json.toplevel.master_value }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(
                "this is not xml",
                CaseInsensitiveDict({"Content-Type": "text/xml"})),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["key"],
            self.force_update,
            self.resource_template,
            self.json_attrs_path,
        )

        self.sensor.update()
        assert {} == self.sensor.device_state_attributes
        assert mock_logger.warning.called
        assert mock_logger.debug.called
Exemple #7
0
    def setUp(self):
        """Set up 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 + '" }',
                CaseInsensitiveDict({"Content-Type": CONTENT_TYPE_JSON}),
            ),
        )
        self.name = "foo"
        self.unit_of_measurement = DATA_MEGABYTES
        self.device_class = None
        self.value_template = template("{{ value_json.key }}")
        self.json_attrs_path = None
        self.value_template.hass = self.hass
        self.force_update = False
        self.resource_template = None

        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            self.value_template,
            [],
            self.force_update,
            self.resource_template,
            self.json_attrs_path,
        )
        self.addCleanup(self.hass.stop)
    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.resource_template,
        )
        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
Exemple #9
0
    def test_update_with_xml_convert_json_attrs_with_jsonattr_template(self):
        """Test attributes get extracted from a JSON result that was converted from XML."""
        json_attrs_path = "$.response"
        value_template = template("{{ value_json.response.bss.wlan }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(
                '<?xml version="1.0" encoding="utf-8"?><response><scan>0</scan><ver>12556</ver><count>48</count><ssid>alexander</ssid><bss><valid>0</valid><name>0</name><privacy>0</privacy><wlan>bogus</wlan><strength>0</strength></bss><led0>0</led0><led1>0</led1><led2>0</led2><led3>0</led3><led4>0</led4><led5>0</led5><led6>0</led6><led7>0</led7><btn0>up</btn0><btn1>up</btn1><btn2>up</btn2><btn3>up</btn3><pot0>0</pot0><usr0>0</usr0><temp0>0x0XF0x0XF</temp0><time0> 0</time0></response>',
                CaseInsensitiveDict({"Content-Type": "text/xml"}),
            ),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["led0", "led1", "temp0", "time0", "ver"],
            self.force_update,
            self.resource_template,
            json_attrs_path,
        )

        self.sensor.update()
        assert "0" == self.sensor.device_state_attributes["led0"]
        assert "0" == self.sensor.device_state_attributes["led1"]
        assert "0x0XF0x0XF" == self.sensor.device_state_attributes["temp0"]
        assert "0" == self.sensor.device_state_attributes["time0"]
        assert "12556" == self.sensor.device_state_attributes["ver"]
        assert "bogus" == self.sensor.state
Exemple #10
0
    def test_update_with_json_attrs_with_json_attrs_path(self):
        """Test attributes get extracted from a JSON result with a template for the attributes."""
        json_attrs_path = "$.toplevel.second_level"
        value_template = template("{{ value_json.toplevel.master_value }}")
        value_template.hass = self.hass

        self.rest.update = Mock(
            "rest.RestData.update",
            side_effect=self.update_side_effect(
                '{ "toplevel": {"master_value": "master", "second_level": {"some_json_key": "some_json_value", "some_json_key2": "some_json_value2" } } }',
                CaseInsensitiveDict({"Content-Type": CONTENT_TYPE_JSON}),
            ),
        )
        self.sensor = rest.RestSensor(
            self.hass,
            self.rest,
            self.name,
            self.unit_of_measurement,
            self.device_class,
            value_template,
            ["some_json_key", "some_json_key2"],
            self.force_update,
            self.resource_template,
            json_attrs_path,
        )

        self.sensor.update()
        assert "some_json_value" == self.sensor.device_state_attributes[
            "some_json_key"]
        assert ("some_json_value2" ==
                self.sensor.device_state_attributes["some_json_key2"])
        assert "master" == self.sensor.state
Exemple #11
0
 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,
                                   self.device_class, None, ['key'],
                                   self.force_update)
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
Exemple #12
0
 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
Exemple #13
0
 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,
                                   self.device_class, None, ['key'],
                                   self.force_update)
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
Exemple #14
0
 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,
                                   self.device_class, None, ['key'],
                                   self.force_update)
     self.sensor.update()
     assert 'some_json_value' == \
         self.sensor.device_state_attributes['key']
 def test_update_with_json_attrs_list_dict(self):
     """Test attributes get extracted from a JSON list[0] result."""
     self.rest.update = Mock(
         "rest.RestData.update",
         side_effect=self.update_side_effect('[{ "key": "another_value" }]'),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         ["key"],
         self.force_update,
         self.resource_template,
     )
     self.sensor.update()
     assert "another_value" == self.sensor.device_state_attributes["key"]
Exemple #16
0
 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
Exemple #17
0
 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",
             CaseInsensitiveDict({"Content-Type": CONTENT_TYPE_JSON})),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         [],
         self.force_update,
         self.resource_template,
         self.json_attrs_path,
     )
     self.sensor.update()
     assert "plain_state" == self.sensor.state
     assert self.sensor.available
Exemple #18
0
 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" }',
             CaseInsensitiveDict({"Content-Type": CONTENT_TYPE_JSON}),
         ),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         ["key"],
         self.force_update,
         self.resource_template,
         self.json_attrs_path,
     )
     self.sensor.update()
     assert "some_json_value" == self.sensor.device_state_attributes["key"]
Exemple #19
0
 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, CaseInsensitiveDict({"Content-Type":
                                        CONTENT_TYPE_JSON})),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         ["key"],
         self.force_update,
         self.resource_template,
         self.json_attrs_path,
     )
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
Exemple #20
0
 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"]',
             CaseInsensitiveDict({"Content-Type": "application/json"}),
         ),
     )
     self.sensor = rest.RestSensor(
         self.hass,
         self.rest,
         self.name,
         self.unit_of_measurement,
         self.device_class,
         None,
         ["key"],
         self.force_update,
         self.resource_template,
         self.json_attrs_path,
     )
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called