Set device_class on temperature sensors A-E (#49524)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2021-07-12 22:45:29 +02:00 committed by GitHub
parent 0099b54489
commit 9b8a776001
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 593 additions and 258 deletions

View file

@ -4,7 +4,12 @@ import logging
from homeassistant.components import mqtt
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import DEGREE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.const import (
DEGREE,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.core import callback
from homeassistant.util import slugify
@ -30,7 +35,9 @@ def discover_sensors(topic, payload):
unit = TEMP_FAHRENHEIT
else:
unit = TEMP_CELSIUS
return ArwnSensor(topic, name, "temp", unit)
return ArwnSensor(
topic, name, "temp", unit, device_class=DEVICE_CLASS_TEMPERATURE
)
if domain == "moisture":
name = f"{parts[2]} Moisture"
return ArwnSensor(topic, name, "moisture", unit, "mdi:water-percent")
@ -117,7 +124,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class ArwnSensor(SensorEntity):
"""Representation of an ARWN sensor."""
def __init__(self, topic, name, state_key, units, icon=None):
def __init__(self, topic, name, state_key, units, icon=None, device_class=None):
"""Initialize the sensor."""
self.hass = None
self.entity_id = _slug(name)
@ -128,6 +135,7 @@ class ArwnSensor(SensorEntity):
self.event = {}
self._unit_of_measurement = units
self._icon = icon
self._device_class = device_class
def set_event(self, event):
"""Update the sensor with the most recent event."""
@ -168,6 +176,11 @@ class ArwnSensor(SensorEntity):
"""Return the polling state."""
return False
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return self._device_class
@property
def icon(self):
"""Return the icon of device based on its type."""