Skip to content

Commit

Permalink
Refactored units and icons for the Dyson sensors (#14550)
Browse files Browse the repository at this point in the history
* Refactored units and icons for the Dyson sensors

* Adapted unit tests to the new device names and unit of measurements

* Use None as empty unit of measurement

* Unrelated overall improvements following code review

* Adapted tests to new constructors as per previous commit

* Make sure the sensors have their `hass` attribute set in the test environment
  • Loading branch information
glpatcern authored and balloob committed Sep 21, 2018
1 parent d5813cf commit 2131717
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 95 deletions.
100 changes: 49 additions & 51 deletions homeassistant/components/sensor/dyson.py
Expand Up @@ -14,12 +14,20 @@
DEPENDENCIES = ['dyson']

SENSOR_UNITS = {
'air_quality': 'level',
'dust': 'level',
'air_quality': None,
'dust': None,
'filter_life': 'hours',
'humidity': '%',
}

SENSOR_ICONS = {
'air_quality': 'mdi:fan',
'dust': 'mdi:cloud',
'filter_life': 'mdi:filter-outline',
'humidity': 'mdi:water-percent',
'temperature': 'mdi:thermometer',
}

_LOGGER = logging.getLogger(__name__)


Expand All @@ -32,23 +40,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
from libpurecoollink.dyson_pure_cool_link import DysonPureCoolLink
for device in [d for d in hass.data[DYSON_DEVICES] if
isinstance(d, DysonPureCoolLink)]:
devices.append(DysonFilterLifeSensor(hass, device))
devices.append(DysonDustSensor(hass, device))
devices.append(DysonHumiditySensor(hass, device))
devices.append(DysonTemperatureSensor(hass, device, unit))
devices.append(DysonAirQualitySensor(hass, device))
devices.append(DysonFilterLifeSensor(device))
devices.append(DysonDustSensor(device))
devices.append(DysonHumiditySensor(device))
devices.append(DysonTemperatureSensor(device, unit))
devices.append(DysonAirQualitySensor(device))
add_entities(devices)


class DysonSensor(Entity):
"""Representation of Dyson sensor."""
"""Representation of a generic Dyson sensor."""

def __init__(self, hass, device):
"""Create a new Dyson filter life sensor."""
self.hass = hass
def __init__(self, device, sensor_type):
"""Create a new generic Dyson sensor."""
self._device = device
self._old_value = None
self._name = None
self._sensor_type = sensor_type

@asyncio.coroutine
def async_added_to_hass(self):
Expand All @@ -72,17 +80,27 @@ def should_poll(self):

@property
def name(self):
"""Return the name of the dyson sensor name."""
"""Return the name of the Dyson sensor name."""
return self._name

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS[self._sensor_type]

@property
def icon(self):
"""Return the icon for this sensor."""
return SENSOR_ICONS[self._sensor_type]


class DysonFilterLifeSensor(DysonSensor):
"""Representation of Dyson filter life sensor (in hours)."""
"""Representation of Dyson Filter Life sensor (in hours)."""

def __init__(self, hass, device):
"""Create a new Dyson filter life sensor."""
DysonSensor.__init__(self, hass, device)
self._name = "{} filter life".format(self._device.name)
def __init__(self, device):
"""Create a new Dyson Filter Life sensor."""
super().__init__(device, 'filter_life')
self._name = "{} Filter Life".format(self._device.name)

@property
def state(self):
Expand All @@ -91,19 +109,14 @@ def state(self):
return int(self._device.state.filter_life)
return None

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['filter_life']


class DysonDustSensor(DysonSensor):
"""Representation of Dyson Dust sensor (lower is better)."""

def __init__(self, hass, device):
def __init__(self, device):
"""Create a new Dyson Dust sensor."""
DysonSensor.__init__(self, hass, device)
self._name = "{} dust".format(self._device.name)
super().__init__(device, 'dust')
self._name = "{} Dust".format(self._device.name)

@property
def state(self):
Expand All @@ -112,47 +125,37 @@ def state(self):
return self._device.environmental_state.dust
return None

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['dust']


class DysonHumiditySensor(DysonSensor):
"""Representation of Dyson Humidity sensor."""

def __init__(self, hass, device):
def __init__(self, device):
"""Create a new Dyson Humidity sensor."""
DysonSensor.__init__(self, hass, device)
self._name = "{} humidity".format(self._device.name)
super().__init__(device, 'humidity')
self._name = "{} Humidity".format(self._device.name)

@property
def state(self):
"""Return Dust value."""
"""Return Humidity value."""
if self._device.environmental_state:
if self._device.environmental_state.humidity == 0:
return STATE_OFF
return self._device.environmental_state.humidity
return None

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['humidity']


class DysonTemperatureSensor(DysonSensor):
"""Representation of Dyson Temperature sensor."""

def __init__(self, hass, device, unit):
def __init__(self, device, unit):
"""Create a new Dyson Temperature sensor."""
DysonSensor.__init__(self, hass, device)
self._name = "{} temperature".format(self._device.name)
super().__init__(device, 'temperature')
self._name = "{} Temperature".format(self._device.name)
self._unit = unit

@property
def state(self):
"""Return Dust value."""
"""Return Temperature value."""
if self._device.environmental_state:
temperature_kelvin = self._device.environmental_state.temperature
if temperature_kelvin == 0:
Expand All @@ -171,19 +174,14 @@ def unit_of_measurement(self):
class DysonAirQualitySensor(DysonSensor):
"""Representation of Dyson Air Quality sensor (lower is better)."""

def __init__(self, hass, device):
def __init__(self, device):
"""Create a new Dyson Air Quality sensor."""
DysonSensor.__init__(self, hass, device)
self._name = "{} air quality".format(self._device.name)
super().__init__(device, 'air_quality')
self._name = "{} AQI".format(self._device.name)

@property
def state(self):
"""Return Air Quality value."""
if self._device.environmental_state:
return self._device.environmental_state.volatil_organic_compounds
return None

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['air_quality']
92 changes: 48 additions & 44 deletions tests/components/sensor/test_dyson.py
Expand Up @@ -70,11 +70,11 @@ def test_setup_component(self):
"""Test setup component with devices."""
def _add_device(devices):
assert len(devices) == 5
assert devices[0].name == "Device_name filter life"
assert devices[1].name == "Device_name dust"
assert devices[2].name == "Device_name humidity"
assert devices[3].name == "Device_name temperature"
assert devices[4].name == "Device_name air quality"
assert devices[0].name == "Device_name Filter Life"
assert devices[1].name == "Device_name Dust"
assert devices[2].name == "Device_name Humidity"
assert devices[3].name == "Device_name Temperature"
assert devices[4].name == "Device_name AQI"

device_fan = _get_device_without_state()
device_non_fan = _get_with_state()
Expand All @@ -83,143 +83,147 @@ def _add_device(devices):

def test_dyson_filter_life_sensor(self):
"""Test filter life sensor with no value."""
sensor = dyson.DysonFilterLifeSensor(self.hass,
_get_device_without_state())
sensor = dyson.DysonFilterLifeSensor(_get_device_without_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, "hours")
self.assertEqual(sensor.name, "Device_name filter life")
self.assertEqual(sensor.name, "Device_name Filter Life")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message')

def test_dyson_filter_life_sensor_with_values(self):
"""Test filter sensor with values."""
sensor = dyson.DysonFilterLifeSensor(self.hass, _get_with_state())
sensor = dyson.DysonFilterLifeSensor(_get_with_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 100)
self.assertEqual(sensor.unit_of_measurement, "hours")
self.assertEqual(sensor.name, "Device_name filter life")
self.assertEqual(sensor.name, "Device_name Filter Life")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message')

def test_dyson_dust_sensor(self):
"""Test dust sensor with no value."""
sensor = dyson.DysonDustSensor(self.hass,
_get_device_without_state())
sensor = dyson.DysonDustSensor(_get_device_without_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, 'level')
self.assertEqual(sensor.name, "Device_name dust")
self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name Dust")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_dust_sensor_with_values(self):
"""Test dust sensor with values."""
sensor = dyson.DysonDustSensor(self.hass, _get_with_state())
sensor = dyson.DysonDustSensor(_get_with_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 5)
self.assertEqual(sensor.unit_of_measurement, 'level')
self.assertEqual(sensor.name, "Device_name dust")
self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name Dust")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_humidity_sensor(self):
"""Test humidity sensor with no value."""
sensor = dyson.DysonHumiditySensor(self.hass,
_get_device_without_state())
sensor = dyson.DysonHumiditySensor(_get_device_without_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, '%')
self.assertEqual(sensor.name, "Device_name humidity")
self.assertEqual(sensor.name, "Device_name Humidity")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_humidity_sensor_with_values(self):
"""Test humidity sensor with values."""
sensor = dyson.DysonHumiditySensor(self.hass, _get_with_state())
sensor = dyson.DysonHumiditySensor(_get_with_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 45)
self.assertEqual(sensor.unit_of_measurement, '%')
self.assertEqual(sensor.name, "Device_name humidity")
self.assertEqual(sensor.name, "Device_name Humidity")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_humidity_standby_monitoring(self):
"""Test humidity sensor while device is in standby monitoring."""
sensor = dyson.DysonHumiditySensor(self.hass,
_get_with_standby_monitoring())
sensor = dyson.DysonHumiditySensor(_get_with_standby_monitoring())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, STATE_OFF)
self.assertEqual(sensor.unit_of_measurement, '%')
self.assertEqual(sensor.name, "Device_name humidity")
self.assertEqual(sensor.name, "Device_name Humidity")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_temperature_sensor(self):
"""Test temperature sensor with no value."""
sensor = dyson.DysonTemperatureSensor(self.hass,
_get_device_without_state(),
sensor = dyson.DysonTemperatureSensor(_get_device_without_state(),
TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, '°C')
self.assertEqual(sensor.name, "Device_name temperature")
self.assertEqual(sensor.name, "Device_name Temperature")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_temperature_sensor_with_values(self):
"""Test temperature sensor with values."""
sensor = dyson.DysonTemperatureSensor(self.hass,
_get_with_state(),
sensor = dyson.DysonTemperatureSensor(_get_with_state(),
TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 21.9)
self.assertEqual(sensor.unit_of_measurement, '°C')
self.assertEqual(sensor.name, "Device_name temperature")
self.assertEqual(sensor.name, "Device_name Temperature")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

sensor = dyson.DysonTemperatureSensor(self.hass,
_get_with_state(),
sensor = dyson.DysonTemperatureSensor(_get_with_state(),
TEMP_FAHRENHEIT)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 71.3)
self.assertEqual(sensor.unit_of_measurement, '°F')
self.assertEqual(sensor.name, "Device_name temperature")
self.assertEqual(sensor.name, "Device_name Temperature")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_temperature_standby_monitoring(self):
"""Test temperature sensor while device is in standby monitoring."""
sensor = dyson.DysonTemperatureSensor(self.hass,
_get_with_standby_monitoring(),
sensor = dyson.DysonTemperatureSensor(_get_with_standby_monitoring(),
TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, STATE_OFF)
self.assertEqual(sensor.unit_of_measurement, '°C')
self.assertEqual(sensor.name, "Device_name temperature")
self.assertEqual(sensor.name, "Device_name Temperature")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_air_quality_sensor(self):
"""Test air quality sensor with no value."""
sensor = dyson.DysonAirQualitySensor(self.hass,
_get_device_without_state())
sensor = dyson.DysonAirQualitySensor(_get_device_without_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, 'level')
self.assertEqual(sensor.name, "Device_name air quality")
self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name AQI")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

def test_dyson_air_quality_sensor_with_values(self):
"""Test air quality sensor with values."""
sensor = dyson.DysonAirQualitySensor(self.hass, _get_with_state())
sensor = dyson.DysonAirQualitySensor(_get_with_state())
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 2)
self.assertEqual(sensor.unit_of_measurement, 'level')
self.assertEqual(sensor.name, "Device_name air quality")
self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name AQI")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")

0 comments on commit 2131717

Please sign in to comment.