Add sensor support for dyson 2018 models (#22578)
fix check for already created entities remove hepa and carbon filter add AQI attribute initial commit fix check for already created entities remove hepa and carbon filter add AQI attribute add air quality component tests fix method call tests fix line lengths fix pylint issues fix docstrings revert fan related changes remove whitespace change fix fan update state test add for loop for platform initialization add requested changes to aiq platform change string concatenation to new style string formatting update air quality tests update air quality tests refactor sensor component changes fix pylint issues fix debug string in the air quality component replace failing tests for older devices fix line length fan tests remove dependencies const and move imports move back imports to methods remove whitespace from blank line
This commit is contained in:
1 parent
b4e2a0ef84
commit
1d70005b01
9 files changed
+430
-112
No files matched your search
@@ -16,6 +16,7 @@ CONF_RETRY = 'retry'
|
||||
DEFAULT_TIMEOUT = 5
|
||||
DEFAULT_RETRY = 10
|
||||
DYSON_DEVICES = 'dyson_devices'
|
||||
DYSON_PLATFORMS = ['sensor', 'fan', 'vacuum', 'climate', 'air_quality']
|
||||
|
||||
DOMAIN = 'dyson'
|
||||
|
||||
@@ -91,9 +92,7 @@ def setup(hass, config):
|
||||
# Start fan/sensors components
|
||||
if hass.data[DYSON_DEVICES]:
|
||||
_LOGGER.debug("Starting sensor/fan components")
|
||||
discovery.load_platform(hass, "sensor", DOMAIN, {}, config)
|
||||
discovery.load_platform(hass, "fan", DOMAIN, {}, config)
|
||||
discovery.load_platform(hass, "vacuum", DOMAIN, {}, config)
|
||||
discovery.load_platform(hass, "climate", DOMAIN, {}, config)
|
||||
for platform in DYSON_PLATFORMS:
|
||||
discovery.load_platform(hass, platform, DOMAIN, {}, config)
|
||||
|
||||
return True
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Support for Dyson Pure Cool Air Quality Sensors."""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.air_quality import AirQualityEntity, DOMAIN
|
||||
from . import DYSON_DEVICES
|
||||
|
||||
ATTRIBUTION = 'Dyson purifier air quality sensor'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DYSON_AIQ_DEVICES = 'dyson_aiq_devices'
|
||||
|
||||
ATTR_VOC = 'volatile_organic_compounds'
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Dyson Sensors."""
|
||||
from libpurecool.dyson_pure_cool import DysonPureCool
|
||||
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
hass.data.setdefault(DYSON_AIQ_DEVICES, [])
|
||||
|
||||
# Get Dyson Devices from parent component
|
||||
device_ids = [device.unique_id for device in hass.data[DYSON_AIQ_DEVICES]]
|
||||
for device in hass.data[DYSON_DEVICES]:
|
||||
if isinstance(device, DysonPureCool) and \
|
||||
device.serial not in device_ids:
|
||||
hass.data[DYSON_AIQ_DEVICES].append(DysonAirSensor(device))
|
||||
add_entities(hass.data[DYSON_AIQ_DEVICES])
|
||||
|
||||
|
||||
class DysonAirSensor(AirQualityEntity):
|
||||
"""Representation of a generic Dyson air quality sensor."""
|
||||
|
||||
def __init__(self, device):
|
||||
"""Create a new generic air quality Dyson sensor."""
|
||||
self._device = device
|
||||
self._old_value = None
|
||||
self._name = device.name
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self.hass.async_add_executor_job(
|
||||
self._device.add_message_listener, self.on_message)
|
||||
|
||||
def on_message(self, message):
|
||||
"""Handle new messages which are received from the fan."""
|
||||
from libpurecool.dyson_pure_state_v2 import \
|
||||
DysonEnvironmentalSensorV2State
|
||||
|
||||
_LOGGER.debug('%s: Message received for %s device: %s',
|
||||
DOMAIN, self.name, message)
|
||||
if (self._old_value is None or
|
||||
self._old_value != self._device.environmental_state) and \
|
||||
isinstance(message, DysonEnvironmentalSensorV2State):
|
||||
self._old_value = self._device.environmental_state
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the Dyson sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def attribution(self):
|
||||
"""Return the attribution."""
|
||||
return ATTRIBUTION
|
||||
|
||||
@property
|
||||
def air_quality_index(self):
|
||||
"""Return the Air Quality Index (AQI)."""
|
||||
return max(self.particulate_matter_2_5,
|
||||
self.particulate_matter_10,
|
||||
self.nitrogen_dioxide,
|
||||
self.volatile_organic_compounds)
|
||||
|
||||
@property
|
||||
def particulate_matter_2_5(self):
|
||||
"""Return the particulate matter 2.5 level."""
|
||||
if self._device.environmental_state:
|
||||
return int(self._device.environmental_state.particulate_matter_25)
|
||||
return None
|
||||
|
||||
@property
|
||||
def particulate_matter_10(self):
|
||||
"""Return the particulate matter 10 level."""
|
||||
if self._device.environmental_state:
|
||||
return int(self._device.environmental_state.particulate_matter_10)
|
||||
return None
|
||||
|
||||
@property
|
||||
def nitrogen_dioxide(self):
|
||||
"""Return the NO2 (nitrogen dioxide) level."""
|
||||
if self._device.environmental_state:
|
||||
return int(self._device.environmental_state.nitrogen_dioxide)
|
||||
return None
|
||||
|
||||
@property
|
||||
def volatile_organic_compounds(self):
|
||||
"""Return the VOC (Volatile Organic Compounds) level."""
|
||||
if self._device.environmental_state:
|
||||
return int(self._device.
|
||||
environmental_state.volatile_organic_compounds)
|
||||
return None
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the sensor's unique id."""
|
||||
return self._device.serial
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
data = {}
|
||||
|
||||
voc = self.volatile_organic_compounds
|
||||
if voc is not None:
|
||||
data[ATTR_VOC] = voc
|
||||
return data
|
||||
@@ -474,7 +474,8 @@ class DysonPureCoolDevice(FanEntity):
|
||||
FanSpeed.FAN_SPEED_6.value: SPEED_MEDIUM,
|
||||
FanSpeed.FAN_SPEED_7.value: SPEED_MEDIUM,
|
||||
FanSpeed.FAN_SPEED_8.value: SPEED_HIGH,
|
||||
FanSpeed.FAN_SPEED_9.value: SPEED_HIGH}
|
||||
FanSpeed.FAN_SPEED_9.value: SPEED_HIGH,
|
||||
FanSpeed.FAN_SPEED_10.value: SPEED_HIGH}
|
||||
|
||||
return speed_map[self._device.state.speed]
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import logging
|
||||
|
||||
from homeassistant.const import STATE_OFF, TEMP_CELSIUS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import DYSON_DEVICES
|
||||
|
||||
SENSOR_UNITS = {
|
||||
@@ -21,26 +20,38 @@ SENSOR_ICONS = {
|
||||
'temperature': 'mdi:thermometer',
|
||||
}
|
||||
|
||||
DYSON_SENSOR_DEVICES = 'dyson_sensor_devices'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Dyson Sensors."""
|
||||
_LOGGER.debug("Creating new Dyson fans")
|
||||
devices = []
|
||||
unit = hass.config.units.temperature_unit
|
||||
# Get Dyson Devices from parent component
|
||||
from libpurecool.dyson_pure_cool import DysonPureCool
|
||||
from libpurecool.dyson_pure_cool_link import DysonPureCoolLink
|
||||
from libpurecool.dyson_pure_cool import DysonPureCool
|
||||
|
||||
for device in [d for d in hass.data[DYSON_DEVICES]
|
||||
if isinstance(d, DysonPureCoolLink) and
|
||||
not isinstance(d, DysonPureCool)]:
|
||||
devices.append(DysonFilterLifeSensor(device))
|
||||
devices.append(DysonDustSensor(device))
|
||||
devices.append(DysonHumiditySensor(device))
|
||||
devices.append(DysonTemperatureSensor(device, unit))
|
||||
devices.append(DysonAirQualitySensor(device))
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
hass.data.setdefault(DYSON_SENSOR_DEVICES, [])
|
||||
unit = hass.config.units.temperature_unit
|
||||
devices = hass.data[DYSON_SENSOR_DEVICES]
|
||||
|
||||
# Get Dyson Devices from parent component
|
||||
device_ids = [device.unique_id for device in
|
||||
hass.data[DYSON_SENSOR_DEVICES]]
|
||||
for device in hass.data[DYSON_DEVICES]:
|
||||
if isinstance(device, DysonPureCool):
|
||||
if '{}-{}'.format(device.serial, 'temperature') not in device_ids:
|
||||
devices.append(DysonTemperatureSensor(device, unit))
|
||||
if '{}-{}'.format(device.serial, 'humidity') not in device_ids:
|
||||
devices.append(DysonHumiditySensor(device))
|
||||
elif isinstance(device, DysonPureCoolLink):
|
||||
devices.append(DysonFilterLifeSensor(device))
|
||||
devices.append(DysonDustSensor(device))
|
||||
devices.append(DysonHumiditySensor(device))
|
||||
devices.append(DysonTemperatureSensor(device, unit))
|
||||
devices.append(DysonAirQualitySensor(device))
|
||||
add_entities(devices)
|
||||
|
||||
|
||||
@@ -56,7 +67,7 @@ class DysonSensor(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self.hass.async_add_job(
|
||||
self.hass.async_add_executor_job(
|
||||
self._device.add_message_listener, self.on_message)
|
||||
|
||||
def on_message(self, message):
|
||||
@@ -88,6 +99,11 @@ class DysonSensor(Entity):
|
||||
"""Return the icon for this sensor."""
|
||||
return SENSOR_ICONS[self._sensor_type]
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the sensor's unique id."""
|
||||
return '{}-{}'.format(self._device.serial, self._sensor_type)
|
||||
|
||||
|
||||
class DysonFilterLifeSensor(DysonSensor):
|
||||
"""Representation of Dyson Filter Life sensor (in hours)."""
|
||||
|
||||
Reference in new issue
Block a user