Refactored units and icons for the Dyson sensors (#14550)

* 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
pull/14150/merge
Giuseppe 2018-09-21 15:55:07 +02:00 committed by Paulus Schoutsen
parent d5813cf167
commit 213171769d
2 changed files with 97 additions and 95 deletions

View File

@ -14,12 +14,20 @@ from homeassistant.helpers.entity import Entity
DEPENDENCIES = ['dyson'] DEPENDENCIES = ['dyson']
SENSOR_UNITS = { SENSOR_UNITS = {
'air_quality': 'level', 'air_quality': None,
'dust': 'level', 'dust': None,
'filter_life': 'hours', 'filter_life': 'hours',
'humidity': '%', '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__) _LOGGER = logging.getLogger(__name__)
@ -32,23 +40,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
from libpurecoollink.dyson_pure_cool_link import DysonPureCoolLink from libpurecoollink.dyson_pure_cool_link import DysonPureCoolLink
for device in [d for d in hass.data[DYSON_DEVICES] if for device in [d for d in hass.data[DYSON_DEVICES] if
isinstance(d, DysonPureCoolLink)]: isinstance(d, DysonPureCoolLink)]:
devices.append(DysonFilterLifeSensor(hass, device)) devices.append(DysonFilterLifeSensor(device))
devices.append(DysonDustSensor(hass, device)) devices.append(DysonDustSensor(device))
devices.append(DysonHumiditySensor(hass, device)) devices.append(DysonHumiditySensor(device))
devices.append(DysonTemperatureSensor(hass, device, unit)) devices.append(DysonTemperatureSensor(device, unit))
devices.append(DysonAirQualitySensor(hass, device)) devices.append(DysonAirQualitySensor(device))
add_entities(devices) add_entities(devices)
class DysonSensor(Entity): class DysonSensor(Entity):
"""Representation of Dyson sensor.""" """Representation of a generic Dyson sensor."""
def __init__(self, hass, device): def __init__(self, device, sensor_type):
"""Create a new Dyson filter life sensor.""" """Create a new generic Dyson sensor."""
self.hass = hass
self._device = device self._device = device
self._old_value = None self._old_value = None
self._name = None self._name = None
self._sensor_type = sensor_type
@asyncio.coroutine @asyncio.coroutine
def async_added_to_hass(self): def async_added_to_hass(self):
@ -72,17 +80,27 @@ class DysonSensor(Entity):
@property @property
def name(self): def name(self):
"""Return the name of the dyson sensor name.""" """Return the name of the Dyson sensor name."""
return self._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): class DysonFilterLifeSensor(DysonSensor):
"""Representation of Dyson filter life sensor (in hours).""" """Representation of Dyson Filter Life sensor (in hours)."""
def __init__(self, hass, device): def __init__(self, device):
"""Create a new Dyson filter life sensor.""" """Create a new Dyson Filter Life sensor."""
DysonSensor.__init__(self, hass, device) super().__init__(device, 'filter_life')
self._name = "{} filter life".format(self._device.name) self._name = "{} Filter Life".format(self._device.name)
@property @property
def state(self): def state(self):
@ -91,19 +109,14 @@ class DysonFilterLifeSensor(DysonSensor):
return int(self._device.state.filter_life) return int(self._device.state.filter_life)
return None return None
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['filter_life']
class DysonDustSensor(DysonSensor): class DysonDustSensor(DysonSensor):
"""Representation of Dyson Dust sensor (lower is better).""" """Representation of Dyson Dust sensor (lower is better)."""
def __init__(self, hass, device): def __init__(self, device):
"""Create a new Dyson Dust sensor.""" """Create a new Dyson Dust sensor."""
DysonSensor.__init__(self, hass, device) super().__init__(device, 'dust')
self._name = "{} dust".format(self._device.name) self._name = "{} Dust".format(self._device.name)
@property @property
def state(self): def state(self):
@ -112,47 +125,37 @@ class DysonDustSensor(DysonSensor):
return self._device.environmental_state.dust return self._device.environmental_state.dust
return None return None
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['dust']
class DysonHumiditySensor(DysonSensor): class DysonHumiditySensor(DysonSensor):
"""Representation of Dyson Humidity sensor.""" """Representation of Dyson Humidity sensor."""
def __init__(self, hass, device): def __init__(self, device):
"""Create a new Dyson Humidity sensor.""" """Create a new Dyson Humidity sensor."""
DysonSensor.__init__(self, hass, device) super().__init__(device, 'humidity')
self._name = "{} humidity".format(self._device.name) self._name = "{} Humidity".format(self._device.name)
@property @property
def state(self): def state(self):
"""Return Dust value.""" """Return Humidity value."""
if self._device.environmental_state: if self._device.environmental_state:
if self._device.environmental_state.humidity == 0: if self._device.environmental_state.humidity == 0:
return STATE_OFF return STATE_OFF
return self._device.environmental_state.humidity return self._device.environmental_state.humidity
return None return None
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['humidity']
class DysonTemperatureSensor(DysonSensor): class DysonTemperatureSensor(DysonSensor):
"""Representation of Dyson Temperature sensor.""" """Representation of Dyson Temperature sensor."""
def __init__(self, hass, device, unit): def __init__(self, device, unit):
"""Create a new Dyson Temperature sensor.""" """Create a new Dyson Temperature sensor."""
DysonSensor.__init__(self, hass, device) super().__init__(device, 'temperature')
self._name = "{} temperature".format(self._device.name) self._name = "{} Temperature".format(self._device.name)
self._unit = unit self._unit = unit
@property @property
def state(self): def state(self):
"""Return Dust value.""" """Return Temperature value."""
if self._device.environmental_state: if self._device.environmental_state:
temperature_kelvin = self._device.environmental_state.temperature temperature_kelvin = self._device.environmental_state.temperature
if temperature_kelvin == 0: if temperature_kelvin == 0:
@ -171,10 +174,10 @@ class DysonTemperatureSensor(DysonSensor):
class DysonAirQualitySensor(DysonSensor): class DysonAirQualitySensor(DysonSensor):
"""Representation of Dyson Air Quality sensor (lower is better).""" """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.""" """Create a new Dyson Air Quality sensor."""
DysonSensor.__init__(self, hass, device) super().__init__(device, 'air_quality')
self._name = "{} air quality".format(self._device.name) self._name = "{} AQI".format(self._device.name)
@property @property
def state(self): def state(self):
@ -182,8 +185,3 @@ class DysonAirQualitySensor(DysonSensor):
if self._device.environmental_state: if self._device.environmental_state:
return self._device.environmental_state.volatil_organic_compounds return self._device.environmental_state.volatil_organic_compounds
return None return None
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SENSOR_UNITS['air_quality']

View File

@ -70,11 +70,11 @@ class DysonTest(unittest.TestCase):
"""Test setup component with devices.""" """Test setup component with devices."""
def _add_device(devices): def _add_device(devices):
assert len(devices) == 5 assert len(devices) == 5
assert devices[0].name == "Device_name filter life" assert devices[0].name == "Device_name Filter Life"
assert devices[1].name == "Device_name dust" assert devices[1].name == "Device_name Dust"
assert devices[2].name == "Device_name humidity" assert devices[2].name == "Device_name Humidity"
assert devices[3].name == "Device_name temperature" assert devices[3].name == "Device_name Temperature"
assert devices[4].name == "Device_name air quality" assert devices[4].name == "Device_name AQI"
device_fan = _get_device_without_state() device_fan = _get_device_without_state()
device_non_fan = _get_with_state() device_non_fan = _get_with_state()
@ -83,143 +83,147 @@ class DysonTest(unittest.TestCase):
def test_dyson_filter_life_sensor(self): def test_dyson_filter_life_sensor(self):
"""Test filter life sensor with no value.""" """Test filter life sensor with no value."""
sensor = dyson.DysonFilterLifeSensor(self.hass, sensor = dyson.DysonFilterLifeSensor(_get_device_without_state())
_get_device_without_state()) sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state) self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, "hours") 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message') sensor.on_message('message')
def test_dyson_filter_life_sensor_with_values(self): def test_dyson_filter_life_sensor_with_values(self):
"""Test filter sensor with values.""" """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" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 100) self.assertEqual(sensor.state, 100)
self.assertEqual(sensor.unit_of_measurement, "hours") 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message') sensor.on_message('message')
def test_dyson_dust_sensor(self): def test_dyson_dust_sensor(self):
"""Test dust sensor with no value.""" """Test dust sensor with no value."""
sensor = dyson.DysonDustSensor(self.hass, sensor = dyson.DysonDustSensor(_get_device_without_state())
_get_device_without_state()) sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state) self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, 'level') self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name dust") self.assertEqual(sensor.name, "Device_name Dust")
self.assertEqual(sensor.entity_id, "sensor.dyson_1") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_dust_sensor_with_values(self): def test_dyson_dust_sensor_with_values(self):
"""Test dust sensor with values.""" """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" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 5) self.assertEqual(sensor.state, 5)
self.assertEqual(sensor.unit_of_measurement, 'level') self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name dust") self.assertEqual(sensor.name, "Device_name Dust")
self.assertEqual(sensor.entity_id, "sensor.dyson_1") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_humidity_sensor(self): def test_dyson_humidity_sensor(self):
"""Test humidity sensor with no value.""" """Test humidity sensor with no value."""
sensor = dyson.DysonHumiditySensor(self.hass, sensor = dyson.DysonHumiditySensor(_get_device_without_state())
_get_device_without_state()) sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state) self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, '%') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_humidity_sensor_with_values(self): def test_dyson_humidity_sensor_with_values(self):
"""Test humidity sensor with values.""" """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" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 45) self.assertEqual(sensor.state, 45)
self.assertEqual(sensor.unit_of_measurement, '%') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_humidity_standby_monitoring(self): def test_dyson_humidity_standby_monitoring(self):
"""Test humidity sensor while device is in standby monitoring.""" """Test humidity sensor while device is in standby monitoring."""
sensor = dyson.DysonHumiditySensor(self.hass, sensor = dyson.DysonHumiditySensor(_get_with_standby_monitoring())
_get_with_standby_monitoring()) sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, STATE_OFF) self.assertEqual(sensor.state, STATE_OFF)
self.assertEqual(sensor.unit_of_measurement, '%') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_temperature_sensor(self): def test_dyson_temperature_sensor(self):
"""Test temperature sensor with no value.""" """Test temperature sensor with no value."""
sensor = dyson.DysonTemperatureSensor(self.hass, sensor = dyson.DysonTemperatureSensor(_get_device_without_state(),
_get_device_without_state(),
TEMP_CELSIUS) TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state) self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, '°C') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_temperature_sensor_with_values(self): def test_dyson_temperature_sensor_with_values(self):
"""Test temperature sensor with values.""" """Test temperature sensor with values."""
sensor = dyson.DysonTemperatureSensor(self.hass, sensor = dyson.DysonTemperatureSensor(_get_with_state(),
_get_with_state(),
TEMP_CELSIUS) TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 21.9) self.assertEqual(sensor.state, 21.9)
self.assertEqual(sensor.unit_of_measurement, '°C') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor = dyson.DysonTemperatureSensor(self.hass, sensor = dyson.DysonTemperatureSensor(_get_with_state(),
_get_with_state(),
TEMP_FAHRENHEIT) TEMP_FAHRENHEIT)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 71.3) self.assertEqual(sensor.state, 71.3)
self.assertEqual(sensor.unit_of_measurement, '°F') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_temperature_standby_monitoring(self): def test_dyson_temperature_standby_monitoring(self):
"""Test temperature sensor while device is in standby monitoring.""" """Test temperature sensor while device is in standby monitoring."""
sensor = dyson.DysonTemperatureSensor(self.hass, sensor = dyson.DysonTemperatureSensor(_get_with_standby_monitoring(),
_get_with_standby_monitoring(),
TEMP_CELSIUS) TEMP_CELSIUS)
sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, STATE_OFF) self.assertEqual(sensor.state, STATE_OFF)
self.assertEqual(sensor.unit_of_measurement, '°C') 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") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_air_quality_sensor(self): def test_dyson_air_quality_sensor(self):
"""Test air quality sensor with no value.""" """Test air quality sensor with no value."""
sensor = dyson.DysonAirQualitySensor(self.hass, sensor = dyson.DysonAirQualitySensor(_get_device_without_state())
_get_device_without_state()) sensor.hass = self.hass
sensor.entity_id = "sensor.dyson_1" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertIsNone(sensor.state) self.assertIsNone(sensor.state)
self.assertEqual(sensor.unit_of_measurement, 'level') self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name air quality") self.assertEqual(sensor.name, "Device_name AQI")
self.assertEqual(sensor.entity_id, "sensor.dyson_1") self.assertEqual(sensor.entity_id, "sensor.dyson_1")
def test_dyson_air_quality_sensor_with_values(self): def test_dyson_air_quality_sensor_with_values(self):
"""Test air quality sensor with values.""" """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" sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll) self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 2) self.assertEqual(sensor.state, 2)
self.assertEqual(sensor.unit_of_measurement, 'level') self.assertEqual(sensor.unit_of_measurement, None)
self.assertEqual(sensor.name, "Device_name air quality") self.assertEqual(sensor.name, "Device_name AQI")
self.assertEqual(sensor.entity_id, "sensor.dyson_1") self.assertEqual(sensor.entity_id, "sensor.dyson_1")