コード例 #1
0
ファイル: mqtt.py プロジェクト: Jocke1970/home-assistant
 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()
コード例 #2
0
ファイル: dweet.py プロジェクト: FanaHOVA/home-assistant
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'))])
コード例 #3
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 = (payload.lower() == "true")
コード例 #4
0
ファイル: dweet.py プロジェクト: FanaHOVA/home-assistant
 def state(self):
     """ Returns 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
コード例 #5
0
ファイル: rest.py プロジェクト: Jocke1970/home-assistant
    def is_on(self):
        """Return 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))
コード例 #6
0
    def update(self):
        """ Gets 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
コード例 #7
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")
コード例 #8
0
ファイル: mqtt.py プロジェクト: FanaHOVA/home-assistant
 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")
コード例 #9
0
ファイル: mqtt.py プロジェクト: FanaHOVA/home-assistant
 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()
コード例 #10
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()
コード例 #11
0
    def update(self):
        """ Gets 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
コード例 #12
0
ファイル: rest.py プロジェクト: olavhaugen/home-assistant
    def update(self):
        """ Gets the latest data from REST API and updates the state. """
        self.rest.update()
        value = self.rest.data

        if 'error' in value:
            self._state = value['error']
        else:
            if self._value_template is not None:
                value = template.render_with_possible_json_value(
                    self._hass, self._value_template, value, 'N/A')
            self._state = value
コード例 #13
0
ファイル: rest.py プロジェクト: zmrow/home-assistant
    def update(self):
        """ Gets the latest data from REST API and updates 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
コード例 #14
0
ファイル: command_sensor.py プロジェクト: t30/home-assistant
    def update(self):
        """ Gets 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
コード例 #15
0
    def update(self):
        """ Gets the latest data from REST API and updates the state. """
        self.rest.update()
        value = self.rest.data

        if 'error' in value:
            self._state = value['error']
        else:
            if self._value_template is not None:
                value = template.render_with_possible_json_value(
                    self._hass, self._value_template, value, 'N/A')
            self._state = value
コード例 #16
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 }'))
コード例 #17
0
 def test_render_with_possible_json_value_with_template_error(self):
     self.assertEqual(
         'hello',
         template.render_with_possible_json_value(self.hass,
                                                  '{{ value_json', 'hello'))
コード例 #18
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"}'))
コード例 #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_error_value(self):
     self.assertEqual(
         '-',
         template.render_with_possible_json_value(
             self.hass, '{{ value_json', 'hello', '-'))