mirror of
https://github.com/home-assistant/core.git
synced 2025-05-05 06:29:16 +00:00
Fix Prometheus casting issues (#22282)
## Description: Fix Prometheus casting issues **Related issue (if applicable):** fixes #8659. ## Checklist: - [x] The code change is tested and works locally. - [x] Local tests pass with `tox`. **Your PR cannot be merged unless tests pass** - [x] There is no commented out code in this PR.
This commit is contained in:
parent
a58f1eedda
commit
e9cd9f88be
@ -103,6 +103,16 @@ class PrometheusMetrics:
|
|||||||
full_metric_name, documentation, labels)
|
full_metric_name, documentation, labels)
|
||||||
return self._metrics[metric]
|
return self._metrics[metric]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def state_as_number(state):
|
||||||
|
"""Return a state casted to a float."""
|
||||||
|
try:
|
||||||
|
value = state_helper.state_as_number(state)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning("Could not convert %s to float", state)
|
||||||
|
value = 0
|
||||||
|
return value
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _labels(state):
|
def _labels(state):
|
||||||
return {
|
return {
|
||||||
@ -130,7 +140,7 @@ class PrometheusMetrics:
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the binary sensor (0/1)',
|
'State of the binary sensor (0/1)',
|
||||||
)
|
)
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_input_boolean(self, state):
|
def _handle_input_boolean(self, state):
|
||||||
@ -139,7 +149,7 @@ class PrometheusMetrics:
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the input boolean (0/1)',
|
'State of the input boolean (0/1)',
|
||||||
)
|
)
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_device_tracker(self, state):
|
def _handle_device_tracker(self, state):
|
||||||
@ -148,7 +158,7 @@ class PrometheusMetrics:
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the device tracker (0/1)',
|
'State of the device tracker (0/1)',
|
||||||
)
|
)
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_person(self, state):
|
def _handle_person(self, state):
|
||||||
@ -157,7 +167,7 @@ class PrometheusMetrics:
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the person (0/1)',
|
'State of the person (0/1)',
|
||||||
)
|
)
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_light(self, state):
|
def _handle_light(self, state):
|
||||||
@ -171,7 +181,7 @@ class PrometheusMetrics:
|
|||||||
if 'brightness' in state.attributes:
|
if 'brightness' in state.attributes:
|
||||||
value = state.attributes['brightness'] / 255.0
|
value = state.attributes['brightness'] / 255.0
|
||||||
else:
|
else:
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
value = value * 100
|
value = value * 100
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -183,7 +193,7 @@ class PrometheusMetrics:
|
|||||||
self.prometheus_client.Gauge,
|
self.prometheus_client.Gauge,
|
||||||
'State of the lock (0/1)',
|
'State of the lock (0/1)',
|
||||||
)
|
)
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_climate(self, state):
|
def _handle_climate(self, state):
|
||||||
@ -209,7 +219,7 @@ class PrometheusMetrics:
|
|||||||
'climate_state', self.prometheus_client.Gauge,
|
'climate_state', self.prometheus_client.Gauge,
|
||||||
'State of the thermostat (0/1)')
|
'State of the thermostat (0/1)')
|
||||||
try:
|
try:
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
@ -232,7 +242,7 @@ class PrometheusMetrics:
|
|||||||
state.entity_id)
|
state.entity_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
if unit == TEMP_FAHRENHEIT:
|
if unit == TEMP_FAHRENHEIT:
|
||||||
value = fahrenheit_to_celsius(value)
|
value = fahrenheit_to_celsius(value)
|
||||||
_metric.labels(**self._labels(state)).set(value)
|
_metric.labels(**self._labels(state)).set(value)
|
||||||
@ -249,7 +259,7 @@ class PrometheusMetrics:
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
value = state_helper.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user