From 6b21df90537e45a5339f2234933bfa54f2124f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 9 Nov 2020 22:01:13 +0000 Subject: [PATCH] Expose Dyson PureCool filter life remaining percentage (#42765) --- homeassistant/components/dyson/sensor.py | 47 ++++++++++++++ tests/components/dyson/test_sensor.py | 80 +++++++++++++++++++++++- 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/dyson/sensor.py b/homeassistant/components/dyson/sensor.py index f26d83de45d..1629d0fa06b 100644 --- a/homeassistant/components/dyson/sensor.py +++ b/homeassistant/components/dyson/sensor.py @@ -13,6 +13,8 @@ SENSOR_UNITS = { "air_quality": None, "dust": None, "filter_life": TIME_HOURS, + "carbon_filter_state": PERCENTAGE, + "hepa_filter_state": PERCENTAGE, "humidity": PERCENTAGE, } @@ -20,6 +22,8 @@ SENSOR_ICONS = { "air_quality": "mdi:fan", "dust": "mdi:cloud", "filter_life": "mdi:filter-outline", + "carbon_filter_state": "mdi:filter-outline", + "hepa_filter_state": "mdi:filter-outline", "humidity": "mdi:water-percent", "temperature": "mdi:thermometer", } @@ -48,6 +52,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None): new_entities.append(DysonTemperatureSensor(device, unit)) if f"{device.serial}-humidity" not in device_ids: new_entities.append(DysonHumiditySensor(device)) + + # For PureCool+Humidify devices, a single filter exists, called "Combi Filter". + # It's reported with the HEPA state, while the Carbon state is set to INValid. + if device.state and device.state.carbon_filter_state == "INV": + if f"{device.serial}-hepa_filter_state" not in device_ids: + new_entities.append(DysonHepaFilterLifeSensor(device, "Combi")) + else: + if f"{device.serial}-hepa_filter_state" not in device_ids: + new_entities.append(DysonHepaFilterLifeSensor(device)) + if f"{device.serial}-carbon_filter_state" not in device_ids: + new_entities.append(DysonCarbonFilterLifeSensor(device)) elif isinstance(device, DysonPureCoolLink): new_entities.append(DysonFilterLifeSensor(device)) new_entities.append(DysonDustSensor(device)) @@ -126,6 +141,38 @@ class DysonFilterLifeSensor(DysonSensor): return None +class DysonCarbonFilterLifeSensor(DysonSensor): + """Representation of Dyson Carbon Filter Life sensor (in percent).""" + + def __init__(self, device): + """Create a new Dyson Carbon Filter Life sensor.""" + super().__init__(device, "carbon_filter_state") + self._name = f"{self._device.name} Carbon Filter Remaining Life" + + @property + def state(self): + """Return filter life remaining in percent.""" + if self._device.state: + return int(self._device.state.carbon_filter_state) + return None + + +class DysonHepaFilterLifeSensor(DysonSensor): + """Representation of Dyson HEPA (or Combi) Filter Life sensor (in percent).""" + + def __init__(self, device, filter_type="HEPA"): + """Create a new Dyson Filter Life sensor.""" + super().__init__(device, "hepa_filter_state") + self._name = f"{self._device.name} {filter_type} Filter Remaining Life" + + @property + def state(self): + """Return filter life remaining in percent.""" + if self._device.state: + return int(self._device.state.hepa_filter_state) + return None + + class DysonDustSensor(DysonSensor): """Representation of Dyson Dust sensor (lower is better).""" diff --git a/tests/components/dyson/test_sensor.py b/tests/components/dyson/test_sensor.py index 5a6febc98cf..4acc52743bb 100644 --- a/tests/components/dyson/test_sensor.py +++ b/tests/components/dyson/test_sensor.py @@ -8,6 +8,7 @@ from libpurecool.dyson_pure_cool_link import DysonPureCoolLink from homeassistant.components import dyson as dyson_parent from homeassistant.components.dyson import sensor as dyson from homeassistant.const import ( + ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, STATE_OFF, TEMP_CELSIUS, @@ -67,6 +68,36 @@ def _get_with_state(): return device +def _get_purecool_device(): + """Return a valid device with filters life state values.""" + device = mock.Mock(spec=DysonPureCool) + load_mock_device(device) + device.name = "PureCool" + device.state.carbon_filter_state = "0096" + device.state.hepa_filter_state = "0056" + device.environmental_state.dust = 5 + device.environmental_state.humidity = 45 + device.environmental_state.temperature = 295 + device.environmental_state.volatil_organic_compounds = 2 + + return device + + +def _get_purecool_humidify_device(): + """Return a valid device with filters life state values.""" + device = mock.Mock(spec=DysonPureCool) + load_mock_device(device) + device.name = "PureCool_Humidify" + device.state.carbon_filter_state = "INV" + device.state.hepa_filter_state = "0075" + device.environmental_state.dust = 5 + device.environmental_state.humidity = 45 + device.environmental_state.temperature = 295 + device.environmental_state.volatil_organic_compounds = 2 + + return device + + def _get_with_standby_monitoring(): """Return a valid device with state but with standby monitoring disable.""" device = mock.Mock() @@ -110,7 +141,10 @@ class DysonTest(unittest.TestCase): device_fan = _get_device_without_state() device_non_fan = _get_with_state() - self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan] + self.hass.data[dyson.DYSON_DEVICES] = [ + device_fan, + device_non_fan, + ] dyson.setup_platform(self.hass, None, _add_device, mock.MagicMock()) def test_dyson_filter_life_sensor(self): @@ -272,4 +306,46 @@ async def test_purecool_component_setup_only_once(devices, login, hass): discovery.load_platform(hass, "sensor", dyson_parent.DOMAIN, {}, config) await hass.async_block_till_done() - assert len(hass.data[dyson.DYSON_SENSOR_DEVICES]) == 2 + assert len(hass.data[dyson.DYSON_SENSOR_DEVICES]) == 4 + + +@patch("libpurecool.dyson.DysonAccount.login", return_value=True) +@patch( + "libpurecool.dyson.DysonAccount.devices", + return_value=[_get_purecool_device()], +) +async def test_dyson_purecool_filter_state_sensor(devices, login, hass): + """Test filter sensor with values.""" + config = _get_config() + await async_setup_component(hass, dyson_parent.DOMAIN, config) + await hass.async_block_till_done() + + state = hass.states.get("sensor.purecool_hepa_filter_remaining_life") + assert state is not None + assert state.state == "56" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + assert state.name == "PureCool HEPA Filter Remaining Life" + + state = hass.states.get("sensor.purecool_carbon_filter_remaining_life") + assert state is not None + assert state.state == "96" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + assert state.name == "PureCool Carbon Filter Remaining Life" + + +@patch("libpurecool.dyson.DysonAccount.login", return_value=True) +@patch( + "libpurecool.dyson.DysonAccount.devices", + return_value=[_get_purecool_humidify_device()], +) +async def test_dyson_purecool_humidify_filter_state_sensor(devices, login, hass): + """Test filter sensor with values.""" + config = _get_config() + await async_setup_component(hass, dyson_parent.DOMAIN, config) + await hass.async_block_till_done() + + state = hass.states.get("sensor.purecool_humidify_combi_filter_remaining_life") + assert state is not None + assert state.state == "75" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + assert state.name == "PureCool_Humidify Combi Filter Remaining Life"