Add device class to temperature sensors for octoprint (#56997)

This commit is contained in:
Chris 2021-10-15 09:45:04 -07:00 committed by GitHub
parent 42803e6ac0
commit f8d0f76721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,8 @@ import logging
import requests import requests
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS from homeassistant.const import DEVICE_CLASS_TEMPERATURE, PERCENTAGE, TEMP_CELSIUS
from . import DOMAIN as COMPONENT_DOMAIN, SENSOR_TYPES from . import DOMAIN as COMPONENT_DOMAIN, SENSOR_TYPES
@ -44,27 +44,28 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
for tool in tools: for tool in tools:
for temp_type in types: for temp_type in types:
new_sensor = OctoPrintSensor( new_sensor = OctoPrintSensor(
octoprint_api, api=octoprint_api,
temp_type, condition=temp_type,
temp_type, sensor_type=temp_type,
name, sensor_name=name,
SENSOR_TYPES[octo_type][3], unit=SENSOR_TYPES[octo_type][3],
SENSOR_TYPES[octo_type][0], endpoint=SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1], group=SENSOR_TYPES[octo_type][1],
tool, tool=tool,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
) )
devices.append(new_sensor) devices.append(new_sensor)
else: else:
new_sensor = OctoPrintSensor( new_sensor = OctoPrintSensor(
octoprint_api, api=octoprint_api,
octo_type, condition=octo_type,
SENSOR_TYPES[octo_type][2], sensor_type=SENSOR_TYPES[octo_type][2],
name, sensor_name=name,
SENSOR_TYPES[octo_type][3], unit=SENSOR_TYPES[octo_type][3],
SENSOR_TYPES[octo_type][0], endpoint=SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1], group=SENSOR_TYPES[octo_type][1],
None, icon=SENSOR_TYPES[octo_type][4],
SENSOR_TYPES[octo_type][4],
) )
devices.append(new_sensor) devices.append(new_sensor)
add_entities(devices, True) add_entities(devices, True)
@ -84,6 +85,8 @@ class OctoPrintSensor(SensorEntity):
group, group,
tool=None, tool=None,
icon=None, icon=None,
device_class=None,
state_class=None,
): ):
"""Initialize a new OctoPrint sensor.""" """Initialize a new OctoPrint sensor."""
self.sensor_name = sensor_name self.sensor_name = sensor_name
@ -99,6 +102,8 @@ class OctoPrintSensor(SensorEntity):
self.api_group = group self.api_group = group
self.api_tool = tool self.api_tool = tool
self._icon = icon self._icon = icon
self._attr_device_class = device_class
self._attr_state_class = state_class
_LOGGER.debug("Created OctoPrint sensor %r", self) _LOGGER.debug("Created OctoPrint sensor %r", self)
@property @property