def test_as_number_coercion(self): """Test state_as_number with number.""" for _state in ('0', '0.0', 0, 0.0): self.assertEqual( 0.0, state.state_as_number( ha.State('domain.test', _state, {}))) for _state in ('1', '1.0', 1, 1.0): self.assertEqual( 1.0, state.state_as_number( ha.State('domain.test', _state, {})))
def test_as_number_states(self): """Test state_as_number with states.""" zero_states = (STATE_OFF, STATE_CLOSED, STATE_UNLOCKED, STATE_BELOW_HORIZON) one_states = (STATE_ON, STATE_OPEN, STATE_LOCKED, STATE_ABOVE_HORIZON) for _state in zero_states: self.assertEqual(0, state.state_as_number( ha.State('domain.test', _state, {}))) for _state in one_states: self.assertEqual(1, state.state_as_number( ha.State('domain.test', _state, {})))
def influx_event_listener(event): """Listen for new messages on the bus and sends them to Influx.""" state = event.data.get('new_state') if state is None or state.state in ( STATE_UNKNOWN, '', STATE_UNAVAILABLE) or \ state.entity_id in blacklist: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state measurement = state.attributes.get('unit_of_measurement') if measurement in (None, ''): measurement = state.entity_id json_body = [ { 'measurement': measurement, 'tags': { 'domain': state.domain, 'entity_id': state.object_id, }, 'time': event.time_fired, 'fields': { 'value': _state, } } ] try: influx.write_points(json_body) except exceptions.InfluxDBClientError: _LOGGER.exception('Error saving event "%s" to InfluxDB', json_body)
def splunk_event_listener(event): """Listen for new messages on the bus and sends them to Splunk.""" state = event.data.get('new_state') if state is None: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state json_body = [{ 'domain': state.domain, 'entity_id': state.object_id, 'attributes': dict(state.attributes), 'time': str(event.time_fired), 'value': _state, }] try: payload = {"host": event_collector, "event": json_body} requests.post(event_collector, data=json.dumps(payload), headers=headers) except requests.exceptions.RequestException as error: _LOGGER.exception('Error saving event to Splunk: %s', error)
def influx_event_listener(event): """Listen for new messages on the bus and sends them to Influx.""" state = event.data.get('new_state') if state is None or state.state in ( STATE_UNKNOWN, '', STATE_UNAVAILABLE) or \ state.entity_id in blacklist: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state measurement = state.attributes.get('unit_of_measurement') if measurement in (None, ''): measurement = state.entity_id json_body = [{ 'measurement': measurement, 'tags': { 'domain': state.domain, 'entity_id': state.object_id, }, 'time': event.time_fired, 'fields': { 'value': _state, } }] try: influx.write_points(json_body) except exceptions.InfluxDBClientError: _LOGGER.exception('Error saving event "%s" to InfluxDB', json_body)
def splunk_event_listener(event): """Listen for new messages on the bus and sends them to Splunk.""" state = event.data.get('new_state') if state is None: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state json_body = [ { 'domain': state.domain, 'entity_id': state.object_id, 'attributes': dict(state.attributes), 'time': str(event.time_fired), 'value': _state, } ] try: payload = {"host": event_collector, "event": json_body} requests.post(event_collector, data=json.dumps(payload), headers=headers) except requests.exceptions.RequestException as error: _LOGGER.exception('Error saving event to Splunk: %s', error)
def dweet_event_listener(event): """Listen for new messages on the bus and sends them to Dweet.io.""" state = event.data.get('new_state') if state is None or state.state in (STATE_UNKNOWN, '') \ or state.entity_id not in whitelist: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state json_body[state.attributes.get('friendly_name')] = _state send_data(name, json_body)
def statsd_event_listener(event): """Listen for new messages on the bus and sends them to StatsD.""" state = event.data.get('new_state') if state is None: return try: _state = state_helper.state_as_number(state) except ValueError: return if not isinstance(_state, NUM_TYPES): return _LOGGER.debug('Sending %s.%s', state.entity_id, _state) meter.send(state.entity_id, _state)
def _report_attributes(self, entity_id, new_state): """Report the attributes.""" now = time.time() things = dict(new_state.attributes) try: things['state'] = state.state_as_number(new_state) except ValueError: pass lines = ['%s.%s.%s %f %i' % (self._prefix, entity_id, key.replace(' ', '_'), value, now) for key, value in things.items() if isinstance(value, (float, int))] if not lines: return _LOGGER.debug('Sending to graphite: %s', lines) try: self._send_to_graphite('\n'.join(lines)) except socket.gaierror: _LOGGER.error('Unable to connect to host %s', self._host) except socket.error: _LOGGER.exception('Failed to send data to graphite')
def _report_attributes(self, entity_id, new_state): """Report the attributes.""" now = time.time() things = dict(new_state.attributes) try: things['state'] = state.state_as_number(new_state) except ValueError: pass lines = [ '%s.%s.%s %f %i' % (self._prefix, entity_id, key.replace(' ', '_'), value, now) for key, value in things.items() if isinstance(value, (float, int)) ] if not lines: return _LOGGER.debug('Sending to graphite: %s', lines) try: self._send_to_graphite('\n'.join(lines)) except socket.gaierror: _LOGGER.error('Unable to connect to host %s', self._host) except socket.error: _LOGGER.exception('Failed to send data to graphite')
def logentries_event_listener(event): """Listen for new messages on the bus and sends them to Logentries.""" state = event.data.get('new_state') if state is None: return try: _state = state_helper.state_as_number(state) except ValueError: _state = state.state json_body = [ { 'domain': state.domain, 'entity_id': state.object_id, 'attributes': dict(state.attributes), 'time': str(event.time_fired), 'value': _state, } ] try: payload = {"host": le_wh, "event": json_body} requests.post(le_wh, data=json.dumps(payload), timeout=10) except requests.exceptions.RequestException as error: _LOGGER.exception('Error sending to Logentries: %s', error)