Skip to content

Commit

Permalink
Fix Dyson sensors if devices are configured without standby monitoring.
Browse files Browse the repository at this point in the history
Fixes home-assistant#8569 (home-assistant#8826)

Upgrade libpurecoolink libraries without unused enum34 dependency
  • Loading branch information
CharlesBlonde authored and dethpickle committed Aug 18, 2017
1 parent a610d52 commit 4de6ecf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_TIMEOUT, \
CONF_DEVICES

REQUIREMENTS = ['libpurecoollink==0.2.0']
REQUIREMENTS = ['libpurecoollink==0.4.1']

_LOGGER = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/fan/dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def async_added_to_hass(self):

def on_message(self, message):
"""Called when new messages received from the fan."""
from libpurecoollink.dyson import DysonState
if isinstance(message, DysonState):
from libpurecoollink.dyson_pure_state import DysonPureCoolState
if isinstance(message, DysonPureCoolState):
_LOGGER.debug("Message received for fan device %s : %s", self.name,
message)
self.schedule_update_ha_state()
Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/sensor/dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import asyncio

from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import TEMP_CELSIUS, STATE_OFF
from homeassistant.components.dyson import DYSON_DEVICES

from homeassistant.helpers.entity import Entity
Expand Down Expand Up @@ -128,6 +128,8 @@ def __init__(self, hass, device):
def state(self):
"""Return Dust value."""
if self._device.environmental_state:
if self._device.environmental_state.humidity == 0:
return STATE_OFF
return self._device.environmental_state.humidity
return None

Expand All @@ -151,6 +153,8 @@ def state(self):
"""Return Dust value."""
if self._device.environmental_state:
temperature_kelvin = self._device.environmental_state.temperature
if temperature_kelvin == 0:
return STATE_OFF
if self._unit == TEMP_CELSIUS:
return float("{0:.1f}".format(temperature_kelvin - 273.15))
return float("{0:.1f}".format(temperature_kelvin * 9 / 5 - 459.67))
Expand All @@ -172,7 +176,7 @@ def __init__(self, hass, device):

@property
def state(self):
"""Return Air QUality value."""
"""Return Air Quality value."""
if self._device.environmental_state:
return self._device.environmental_state.volatil_organic_compounds
return None
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ knxip==0.5
libnacl==1.5.2

# homeassistant.components.dyson
libpurecoollink==0.2.0
libpurecoollink==0.4.1

# homeassistant.components.device_tracker.mikrotik
librouteros==1.0.2
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ holidays==0.8.1
influxdb==3.0.0

# homeassistant.components.dyson
libpurecoollink==0.2.0
libpurecoollink==0.4.1

# homeassistant.components.media_player.soundtouch
libsoundtouch==0.7.2
Expand Down
4 changes: 2 additions & 2 deletions tests/components/fan/test_dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from homeassistant.components.fan import dyson
from tests.common import get_test_home_assistant
from libpurecoollink.const import FanSpeed, FanMode, NightMode, Oscillation
from libpurecoollink.dyson import DysonState
from libpurecoollink.dyson_pure_state import DysonPureCoolState


class MockDysonState(DysonState):
class MockDysonState(DysonPureCoolState):
"""Mock Dyson state."""

def __init__(self):
Expand Down
41 changes: 40 additions & 1 deletion tests/components/sensor/test_dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import unittest
from unittest import mock

from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, \
STATE_OFF
from homeassistant.components.sensor import dyson
from tests.common import get_test_home_assistant

Expand Down Expand Up @@ -31,6 +32,21 @@ def _get_with_state():
return device


def _get_with_standby_monitoring():
"""Return a valid device with state but with standby monitoring disable."""
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.filter_life = 100
device.environmental_state = mock.Mock()
device.environmental_state.dust = 5
device.environmental_state.humidity = 0
device.environmental_state.temperature = 0
device.environmental_state.volatil_organic_compounds = 2

return device


class DysonTest(unittest.TestCase):
"""Dyson Sensor component test class."""

Expand Down Expand Up @@ -128,6 +144,17 @@ def test_dyson_humidity_sensor_with_values(self):
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.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.entity_id, "sensor.dyson_1")

def test_dyson_temperature_sensor(self):
"""Test temperature sensor with no value."""
sensor = dyson.DysonTemperatureSensor(self.hass,
Expand Down Expand Up @@ -162,6 +189,18 @@ def test_dyson_temperature_sensor_with_values(self):
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(),
TEMP_CELSIUS)
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.entity_id, "sensor.dyson_1")

def test_dyson_air_quality_sensor(self):
"""Test air quality sensor with no value."""
sensor = dyson.DysonAirQualitySensor(self.hass,
Expand Down

0 comments on commit 4de6ecf

Please sign in to comment.