Add wirelesstag ambient temperature sensor for Outdoor Probe (#60243)

* Added support for ambient temperature sensor for Outdoor Probe.

* Switched to use of SensorEntityDescription for device_class.

* Removed unused config param, restored use of _sensor_type ivar.

* Use entity descriptions as dict for shorter iteration.

* Clean up

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Sergiy Maysak 2021-11-25 00:02:18 +02:00 committed by GitHub
parent 15f62a7237
commit de1527d0e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,22 @@
"""Sensor support for Wireless Sensor Tags platform."""
from __future__ import annotations
import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_MONITORED_CONDITIONS
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import (
CONF_MONITORED_CONDITIONS,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -14,16 +26,45 @@ from . import DOMAIN as WIRELESSTAG_DOMAIN, SIGNAL_TAG_UPDATE, WirelessTagBaseSe
_LOGGER = logging.getLogger(__name__)
SENSOR_TEMPERATURE = "temperature"
SENSOR_AMBIENT_TEMPERATURE = "ambient_temperature"
SENSOR_HUMIDITY = "humidity"
SENSOR_MOISTURE = "moisture"
SENSOR_LIGHT = "light"
SENSOR_TYPES = [SENSOR_TEMPERATURE, SENSOR_HUMIDITY, SENSOR_MOISTURE, SENSOR_LIGHT]
SENSOR_TYPES: dict[str, SensorEntityDescription] = {
SENSOR_TEMPERATURE: SensorEntityDescription(
key=SENSOR_TEMPERATURE,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
SENSOR_AMBIENT_TEMPERATURE: SensorEntityDescription(
key=SENSOR_AMBIENT_TEMPERATURE,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
SENSOR_HUMIDITY: SensorEntityDescription(
key=SENSOR_HUMIDITY,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),
SENSOR_MOISTURE: SensorEntityDescription(
key=SENSOR_MOISTURE,
device_class=SENSOR_MOISTURE,
state_class=STATE_CLASS_MEASUREMENT,
),
SENSOR_LIGHT: SensorEntityDescription(
key=SENSOR_LIGHT,
device_class=DEVICE_CLASS_ILLUMINANCE,
state_class=STATE_CLASS_MEASUREMENT,
),
}
SENSOR_KEYS: list[str] = list(SENSOR_TYPES)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
cv.ensure_list, [vol.In(SENSOR_KEYS)]
)
}
)
@ -35,11 +76,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensors = []
tags = platform.tags
for tag in tags.values():
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
if sensor_type in tag.allowed_sensor_types:
sensors.append(
WirelessTagSensor(platform, tag, sensor_type, hass.config)
)
for key in config[CONF_MONITORED_CONDITIONS]:
if key not in tag.allowed_sensor_types:
continue
description = SENSOR_TYPES[key]
sensors.append(WirelessTagSensor(platform, tag, description))
add_entities(sensors, True)
@ -47,11 +88,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
"""Representation of a Sensor."""
def __init__(self, api, tag, sensor_type, config):
entity_description: SensorEntityDescription
def __init__(self, api, tag, description):
"""Initialize a WirelessTag sensor."""
super().__init__(api, tag)
self._sensor_type = sensor_type
self._sensor_type = description.key
self.entity_description = description
self._name = self._tag.name
# I want to see entity_id as:
@ -87,11 +131,6 @@ class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
"""Return the state of the sensor."""
return self._state
@property
def device_class(self):
"""Return the class of the sensor."""
return self._sensor_type
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""