Fixed Canary temperature sensor and remapped air quality value (#11355)
* Fixed Canary temperature sensor and remapped air quality value * Addressed review comment * - Fixed canary tests and added more tests - Removed py-canary requirements from tests * Noop to trigger a build again * - Removed py-canary requirements from tests * Addressed PR comment * - Updated tests - Removed py-canary from gen_requirements_all.py * - Fixed hound violation * Added back py-canary to gen_requirements_all.py as it's still need in tests * Added back py-canary to test requirements as it's still need in tests * Address PR comment
This commit is contained in:
parent
c7efe5b7dd
commit
b3bf6c4be2
3 changed files with 145 additions and 56 deletions
|
@ -4,13 +4,27 @@ Support for Canary sensors.
|
|||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.canary/
|
||||
"""
|
||||
|
||||
from homeassistant.components.canary import DATA_CANARY
|
||||
from homeassistant.const import TEMP_FAHRENHEIT, TEMP_CELSIUS
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
DEPENDENCIES = ['canary']
|
||||
|
||||
SENSOR_VALUE_PRECISION = 1
|
||||
SENSOR_VALUE_PRECISION = 2
|
||||
ATTR_AIR_QUALITY = "air_quality"
|
||||
|
||||
# Sensor types are defined like so:
|
||||
# sensor type name, unit_of_measurement, icon
|
||||
SENSOR_TYPES = [
|
||||
["temperature", TEMP_CELSIUS, "mdi:thermometer"],
|
||||
["humidity", "%", "mdi:water-percent"],
|
||||
["air_quality", None, "mdi:weather-windy"],
|
||||
]
|
||||
|
||||
STATE_AIR_QUALITY_NORMAL = "normal"
|
||||
STATE_AIR_QUALITY_ABNORMAL = "abnormal"
|
||||
STATE_AIR_QUALITY_VERY_ABNORMAL = "very_abnormal"
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
@ -18,11 +32,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
data = hass.data[DATA_CANARY]
|
||||
devices = []
|
||||
|
||||
from canary.api import SensorType
|
||||
for location in data.locations:
|
||||
for device in location.devices:
|
||||
if device.is_online:
|
||||
for sensor_type in SensorType:
|
||||
for sensor_type in SENSOR_TYPES:
|
||||
devices.append(CanarySensor(data, sensor_type, location,
|
||||
device))
|
||||
|
||||
|
@ -37,10 +50,9 @@ class CanarySensor(Entity):
|
|||
self._data = data
|
||||
self._sensor_type = sensor_type
|
||||
self._device_id = device.device_id
|
||||
self._is_celsius = location.is_celsius
|
||||
self._sensor_value = None
|
||||
|
||||
sensor_type_name = sensor_type.value.replace("_", " ").title()
|
||||
sensor_type_name = sensor_type[0].replace("_", " ").title()
|
||||
self._name = '{} {} {}'.format(location.name,
|
||||
device.name,
|
||||
sensor_type_name)
|
||||
|
@ -59,27 +71,51 @@ class CanarySensor(Entity):
|
|||
def unique_id(self):
|
||||
"""Return the unique ID of this sensor."""
|
||||
return "sensor_canary_{}_{}".format(self._device_id,
|
||||
self._sensor_type.value)
|
||||
self._sensor_type[0])
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement this sensor expresses itself in."""
|
||||
from canary.api import SensorType
|
||||
if self._sensor_type == SensorType.TEMPERATURE:
|
||||
return TEMP_CELSIUS if self._is_celsius else TEMP_FAHRENHEIT
|
||||
elif self._sensor_type == SensorType.HUMIDITY:
|
||||
return "%"
|
||||
elif self._sensor_type == SensorType.AIR_QUALITY:
|
||||
return ""
|
||||
"""Return the unit of measurement."""
|
||||
return self._sensor_type[1]
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon for the sensor."""
|
||||
return self._sensor_type[2]
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
if self._sensor_type[0] == "air_quality" \
|
||||
and self._sensor_value is not None:
|
||||
air_quality = None
|
||||
if self._sensor_value <= .4:
|
||||
air_quality = STATE_AIR_QUALITY_VERY_ABNORMAL
|
||||
elif self._sensor_value <= .59:
|
||||
air_quality = STATE_AIR_QUALITY_ABNORMAL
|
||||
elif self._sensor_value <= 1.0:
|
||||
air_quality = STATE_AIR_QUALITY_NORMAL
|
||||
|
||||
return {
|
||||
ATTR_AIR_QUALITY: air_quality
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
def update(self):
|
||||
"""Get the latest state of the sensor."""
|
||||
self._data.update()
|
||||
|
||||
readings = self._data.get_readings(self._device_id)
|
||||
value = next((
|
||||
reading.value for reading in readings
|
||||
if reading.sensor_type == self._sensor_type), None)
|
||||
from canary.api import SensorType
|
||||
canary_sensor_type = None
|
||||
if self._sensor_type[0] == "air_quality":
|
||||
canary_sensor_type = SensorType.AIR_QUALITY
|
||||
elif self._sensor_type[0] == "temperature":
|
||||
canary_sensor_type = SensorType.TEMPERATURE
|
||||
elif self._sensor_type[0] == "humidity":
|
||||
canary_sensor_type = SensorType.HUMIDITY
|
||||
|
||||
value = self._data.get_reading(self._device_id, canary_sensor_type)
|
||||
|
||||
if value is not None:
|
||||
self._sensor_value = round(float(value), SENSOR_VALUE_PRECISION)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue