Пример #1
0
 def test_render_with_possible_json_value_with_valid_json(self):
     """."""
     self.assertEqual(
         'world',
         template.render_with_possible_json_value(self.hass,
                                                  '{{ value_json.hello }}',
                                                  '{"hello": "world"}'))
Пример #2
0
 def message_received(topic, payload, qos):
     """A new MQTT message has been received."""
     if value_template is not None:
         payload = template.render_with_possible_json_value(
             hass, value_template, payload)
     self._state = payload
     self.update_ha_state()
Пример #3
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Dweet sensor."""
    import dweepy

    device = config.get('device')
    value_template = config.get(CONF_VALUE_TEMPLATE)

    if None in (device, value_template):
        _LOGGER.error('Not all required config keys present: %s',
                      ', '.join(CONF_DEVICE, CONF_VALUE_TEMPLATE))
        return False

    try:
        content = json.dumps(dweepy.get_latest_dweet_for(device)[0]['content'])
    except dweepy.DweepyError:
        _LOGGER.error("Device/thing '%s' could not be found", device)
        return False

    if template.render_with_possible_json_value(hass,
                                                value_template,
                                                content) is '':
        _LOGGER.error("'%s' was not found", value_template)
        return False

    dweet = DweetData(device)

    add_devices([DweetSensor(hass,
                             dweet,
                             config.get('name', DEFAULT_NAME),
                             value_template,
                             config.get('unit_of_measurement'))])
Пример #4
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the Dweet sensor."""
    import dweepy

    device = config.get('device')
    value_template = config.get(CONF_VALUE_TEMPLATE)

    if None in (device, value_template):
        _LOGGER.error('Not all required config keys present: %s',
                      ', '.join(CONF_DEVICE, CONF_VALUE_TEMPLATE))
        return False

    try:
        content = json.dumps(dweepy.get_latest_dweet_for(device)[0]['content'])
    except dweepy.DweepyError:
        _LOGGER.error("Device/thing '%s' could not be found", device)
        return False

    if template.render_with_possible_json_value(hass, value_template,
                                                content) is '':
        _LOGGER.error("'%s' was not found", value_template)
        return False

    dweet = DweetData(device)

    add_devices([
        DweetSensor(hass, dweet, config.get('name', DEFAULT_NAME),
                    value_template, config.get('unit_of_measurement'))
    ])
Пример #5
0
 def test_render_with_possible_json_value_with_template_error_value(self):
     """."""
     self.assertEqual(
         '-',
         template.render_with_possible_json_value(self.hass,
                                                  '{{ value_json', 'hello',
                                                  '-'))
Пример #6
0
 def test_render_with_possible_json_value_with_invalid_json(self):
     """."""
     self.assertEqual(
         '',
         template.render_with_possible_json_value(self.hass,
                                                  '{{ value_json }}',
                                                  '{ I AM NOT JSON }'))
Пример #7
0
 def update(self):
     """Update device state."""
     if self._command_state:
         payload = str(self._query_state())
         if self._value_template:
             payload = template.render_with_possible_json_value(
                 self._hass, self._value_template, payload)
         self._state = int(payload)
Пример #8
0
 def update(self):
     """Update device state."""
     if self._command_state:
         payload = str(self._query_state())
         if self._value_template:
             payload = template.render_with_possible_json_value(
                 self._hass, self._value_template, payload)
         self._state = int(payload)
Пример #9
0
    def is_on(self):
        """Return true if the binary sensor is on."""
        if self.rest.data is None:
            return False

        if self._value_template is not None:
            self.rest.data = template.render_with_possible_json_value(
                self._hass, self._value_template, self.rest.data, False)
        return bool(int(self.rest.data))
Пример #10
0
 def state(self):
     """Return the state."""
     if self.dweet.data is None:
         return STATE_UNKNOWN
     else:
         values = json.dumps(self.dweet.data[0]['content'])
         value = template.render_with_possible_json_value(
             self.hass, self._value_template, values)
         return value
Пример #11
0
 def state(self):
     """Return the state."""
     if self.dweet.data is None:
         return STATE_UNKNOWN
     else:
         values = json.dumps(self.dweet.data[0]['content'])
         value = template.render_with_possible_json_value(
             self.hass, self._value_template, values)
         return value
Пример #12
0
    def update(self):
        """Get the latest data and updates the state."""
        self.data.update()
        value = self.data.value

        if self._value_template is not None:
            self._state = template.render_with_possible_json_value(
                self._hass, self._value_template, value, 'N/A')
        else:
            self._state = value
Пример #13
0
 def message_received(topic, payload, qos):
     """A new MQTT message has been received."""
     if value_template is not None:
         payload = template.render_with_possible_json_value(
             hass, value_template, payload)
     if payload.isnumeric() and 0 <= int(payload) <= 100:
         self._state = int(payload)
         self.update_ha_state()
     else:
         _LOGGER.warning(
             "Payload is expected to be an integer between 0 and 100")
Пример #14
0
 def message_received(topic, payload, qos):
     """A new MQTT message has been received."""
     if value_template is not None:
         payload = template.render_with_possible_json_value(
             hass, value_template, payload)
     if payload.isnumeric() and 0 <= int(payload) <= 100:
         self._state = int(payload)
         self.update_ha_state()
     else:
         _LOGGER.warning(
             "Payload is expected to be an integer between 0 and 100")
Пример #15
0
 def message_received(topic, payload, qos):
     """A new MQTT message has been received."""
     if value_template is not None:
         payload = template.render_with_possible_json_value(
             hass, value_template, payload)
     if payload == self._state_open:
         self._state = True
         self.update_ha_state()
     elif payload == self._state_closed:
         self._state = False
         self.update_ha_state()
Пример #16
0
 def message_received(topic, payload, qos):
     """A new MQTT message has been received."""
     if value_template is not None:
         payload = template.render_with_possible_json_value(
             hass, value_template, payload)
     if payload == self._payload_on:
         self._state = True
         self.update_ha_state()
     elif payload == self._payload_off:
         self._state = False
         self.update_ha_state()
Пример #17
0
    def update(self):
        """Get the latest data from REST API and update the state."""
        self.rest.update()
        value = self.rest.data

        if value is None:
            value = STATE_UNKNOWN
        elif self._value_template is not None:
            value = template.render_with_possible_json_value(
                self._hass, self._value_template, value, STATE_UNKNOWN)

        self._state = value
Пример #18
0
    def update(self):
        """Get the latest data and updates the state."""
        self.data.update()
        value = self.data.value

        if self._value_template is not None:
            value = template.render_with_possible_json_value(
                self._hass, self._value_template, value, False)
        if value == self._payload_on:
            self._state = True
        elif value == self._payload_off:
            self._state = False
Пример #19
0
 def test_render_with_possible_json_value_with_valid_json(self):
     """."""
     self.assertEqual(
         'world',
         template.render_with_possible_json_value(
             self.hass, '{{ value_json.hello }}', '{"hello": "world"}'))
Пример #20
0
 def test_render_with_possible_json_value_with_invalid_json(self):
     """."""
     self.assertEqual(
         '',
         template.render_with_possible_json_value(
             self.hass, '{{ value_json }}', '{ I AM NOT JSON }'))
Пример #21
0
 def test_render_with_possible_json_value_with_template_error_value(self):
     """."""
     self.assertEqual(
         '-',
         template.render_with_possible_json_value(
             self.hass, '{{ value_json', 'hello', '-'))