def _handle_climate(self, state): temp = state.attributes.get(ATTR_TEMPERATURE) if temp: if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric( 'temperature_c', self.prometheus_client.Gauge, 'Temperature in degrees Celsius') metric.labels(**self._labels(state)).set(temp) current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE) if current_temp: if self._climate_units == TEMP_FAHRENHEIT: current_temp = fahrenheit_to_celsius(current_temp) metric = self._metric( 'current_temperature_c', self.prometheus_client.Gauge, 'Current Temperature in degrees Celsius') metric.labels(**self._labels(state)).set(current_temp) metric = self._metric( 'climate_state', self.prometheus_client.Gauge, 'State of the thermostat (0/1)') try: value = state_helper.state_as_number(state) metric.labels(**self._labels(state)).set(value) except ValueError: pass
def _handle_climate(self, state): temp = state.attributes.get(ATTR_TEMPERATURE) if temp: if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric( "temperature_c", self.prometheus_cli.Gauge, "Temperature in degrees Celsius", ) metric.labels(**self._labels(state)).set(temp) current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE) if current_temp: if self._climate_units == TEMP_FAHRENHEIT: current_temp = fahrenheit_to_celsius(current_temp) metric = self._metric( "current_temperature_c", self.prometheus_cli.Gauge, "Current Temperature in degrees Celsius", ) metric.labels(**self._labels(state)).set(current_temp) current_action = state.attributes.get(ATTR_HVAC_ACTION) if current_action: metric = self._metric( "climate_action", self.prometheus_cli.Gauge, "HVAC action", ["action"], ) for action in CURRENT_HVAC_ACTIONS: metric.labels(**dict(self._labels(state), action=action)).set( float(action == current_action) )
def _handle_climate(self, state): temp = state.attributes.get(ATTR_TEMPERATURE) if temp: if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric('temperature_c', self.prometheus_client.Gauge, 'Temperature in degrees Celsius') metric.labels(**self._labels(state)).set(temp) current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE) if current_temp: if self._climate_units == TEMP_FAHRENHEIT: current_temp = fahrenheit_to_celsius(current_temp) metric = self._metric('current_temperature_c', self.prometheus_client.Gauge, 'Current Temperature in degrees Celsius') metric.labels(**self._labels(state)).set(current_temp) metric = self._metric('climate_state', self.prometheus_client.Gauge, 'State of the thermostat (0/1)') try: value = self.state_as_number(state) metric.labels(**self._labels(state)).set(value) except ValueError: pass
def _handle_sensor(self, state): unit = self._unit_string( state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)) for metric_handler in self._sensor_metric_handlers: metric = metric_handler(state, unit) if metric is not None: break if metric is not None: documentation = "State of the sensor" if unit: documentation = f"Sensor data measured in {unit}" _metric = self._metric(metric, self.prometheus_cli.Gauge, documentation) try: value = self.state_as_number(state) if state.attributes.get( ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT: value = fahrenheit_to_celsius(value) _metric.labels(**self._labels(state)).set(value) except ValueError: pass self._battery(state)
def _handle_sensor(self, state): unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) metric = state.entity_id.split(".")[1] if '_' not in str(metric): metric = state.entity_id.replace('.', '_') try: int(metric.split("_")[-1]) metric = "_".join(metric.split("_")[:-1]) except ValueError: pass _metric = self._metric(metric, self.prometheus_client.Gauge, state.entity_id) try: value = self.state_as_number(state) if unit == TEMP_FAHRENHEIT: value = fahrenheit_to_celsius(value) _metric.labels(**self._labels(state)).set(value) except ValueError: pass self._battery(state)
def _handle_sensor(self, state): unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) metric = state.entity_id.split(".")[1] if '_' not in str(metric): metric = state.entity_id.replace('.', '_') try: int(metric.split("_")[-1]) metric = "_".join(metric.split("_")[:-1]) except ValueError: pass _metric = self._metric(metric, self.prometheus_client.Gauge, state.entity_id) try: value = state_helper.state_as_number(state) if unit == TEMP_FAHRENHEIT: value = fahrenheit_to_celsius(value) _metric.labels(**self._labels(state)).set(value) except ValueError: pass self._battery(state)
def convert(temperature, unit, to_unit): """Convert temperature to correct unit.""" if unit == to_unit or unit is None or to_unit is None: return temperature elif unit == TEMP_CELSIUS: return temp_util.celsius_to_fahrenheit(temperature) return temp_util.fahrenheit_to_celsius(temperature)
def async_restore_last_state(self, last_state): """Restore previous state.""" if last_state.state == STATE_UNKNOWN: return if last_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) != TEMP_CELSIUS: ftemp = float(last_state.state) self._state = round(fahrenheit_to_celsius(ftemp), 1) return self._state = last_state.state
def _handle_climate_temp(self, state, attr, metric_name, metric_description): if temp := state.attributes.get(attr): if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric( metric_name, self.prometheus_cli.Gauge, metric_description, ) metric.labels(**self._labels(state)).set(temp)
def _handle_sensor(self, state): _sensor_types = { TEMP_CELSIUS: ( 'temperature_c', self.prometheus_client.Gauge, 'Temperature in degrees Celsius', ), TEMP_FAHRENHEIT: ( 'temperature_c', self.prometheus_client.Gauge, 'Temperature in degrees Celsius', ), '%': ( 'relative_humidity', self.prometheus_client.Gauge, 'Relative humidity (0..100)', ), 'lux': ( 'light_lux', self.prometheus_client.Gauge, 'Light level in lux', ), 'kWh': ( 'electricity_used_kwh', self.prometheus_client.Gauge, 'Electricity used by this device in KWh', ), 'V': ( 'voltage', self.prometheus_client.Gauge, 'Currently reported voltage in Volts', ), 'W': ( 'electricity_usage_w', self.prometheus_client.Gauge, 'Currently reported electricity draw in Watts', ), 'min': ( 'sensor_min', self.prometheus_client.Gauge, 'Time in minutes reported by a sensor' ), 'Events': ( 'sensor_event_count', self.prometheus_client.Gauge, 'Number of events for a sensor' ), } unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) metric = _sensor_types.get(unit) if metric is not None: metric = self._metric(*metric) try: value = state_helper.state_as_number(state) if unit == TEMP_FAHRENHEIT: value = fahrenheit_to_celsius(value) metric.labels(**self._labels(state)).set(value) except ValueError: pass self._battery(state)
def _handle_climate(self, state): temp = state.attributes.get(ATTR_TEMPERATURE) if temp: if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric( "temperature_c", self.prometheus_cli.Gauge, "Temperature in degrees Celsius", ) metric.labels(**self._labels(state)).set(temp) current_temp = state.attributes.get(ATTR_CURRENT_TEMPERATURE) if current_temp: if self._climate_units == TEMP_FAHRENHEIT: current_temp = fahrenheit_to_celsius(current_temp) metric = self._metric( "current_temperature_c", self.prometheus_cli.Gauge, "Current Temperature in degrees Celsius", ) metric.labels(**self._labels(state)).set(current_temp)
def _handle_climate(self, state): temp = state.attributes.get(ATTR_TEMPERATURE) if temp: unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) if unit == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric('temperature_c', self.prometheus_client.Gauge, 'Temperature in degrees Celsius') metric.labels(**self._labels(state)).set(temp) metric = self._metric('climate_state', self.prometheus_client.Gauge, 'State of the thermostat (0/1)') try: value = state_helper.state_as_number(state) metric.labels(**self._labels(state)).set(value) except ValueError: pass
f"input_number_state_{unit}", self.prometheus_cli.Gauge, f"State of the input number measured in {unit}", ) else: metric = self._metric( "input_number_state", self.prometheus_cli.Gauge, "State of the input number", ) with suppress(ValueError): value = self.state_as_number(state) if state.attributes.get( ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT: value = fahrenheit_to_celsius(value) metric.labels(**self._labels(state)).set(value) def _handle_device_tracker(self, state): metric = self._metric( "device_tracker_state", self.prometheus_cli.Gauge, "State of the device tracker (0/1)", ) value = self.state_as_number(state) metric.labels(**self._labels(state)).set(value) def _handle_person(self, state): metric = self._metric("person_state", self.prometheus_cli.Gauge, "State of the person (0/1)") value = self.state_as_number(state)
def state(self) -> Optional[float]: """Return the current temperature.""" if self._device.temperature is None: return None return round(fahrenheit_to_celsius(self._device.temperature), 1)