From 5bfc1f3d4d6e43bc02a127ac773d6cc45556bf03 Mon Sep 17 00:00:00 2001 From: Ziv <16467659+ziv1234@users.noreply.github.com> Date: Thu, 16 Apr 2020 00:03:14 +0300 Subject: [PATCH] Removed uncaught exceptions from Dyson (#34112) * fixed what seems to be a typo * added load_mock_device in common.py so it loads all the required things into the mocks so they don't throw exceptions for mocks not being able to convert to int * reverted change in homeassistant/components/dyson/sensor.py added both values to the mock device (volatil and volatile) --- tests/components/dyson/common.py | 25 ++++++++++++++++ tests/components/dyson/test_air_quality.py | 6 ++-- tests/components/dyson/test_climate.py | 33 +++++++--------------- tests/components/dyson/test_fan.py | 24 +++------------- tests/components/dyson/test_init.py | 8 ++++-- tests/components/dyson/test_sensor.py | 20 ++++--------- tests/ignore_uncaught_exceptions.py | 30 -------------------- 7 files changed, 52 insertions(+), 94 deletions(-) create mode 100644 tests/components/dyson/common.py diff --git a/tests/components/dyson/common.py b/tests/components/dyson/common.py new file mode 100644 index 00000000000..b78e7d58283 --- /dev/null +++ b/tests/components/dyson/common.py @@ -0,0 +1,25 @@ +"""Common utils for Dyson tests.""" + +from unittest import mock + +from libpurecool.dyson_pure_cool import FanSpeed + + +def load_mock_device(device): + """Load the mock with default values so it doesn't throw errors.""" + device.serial = "XX-XXXXX-XX" + device.name = "Temp Name" + device.connect = mock.Mock(return_value=True) + device.auto_connect = mock.Mock(return_value=True) + device.environmental_state.particulate_matter_25 = "0000" + device.environmental_state.particulate_matter_10 = "0000" + device.environmental_state.nitrogen_dioxide = "0000" + device.environmental_state.volatil_organic_compounds = "0000" + device.environmental_state.volatile_organic_compounds = "0000" + device.environmental_state.temperature = 250 + device.state.hepa_filter_state = 0 + device.state.carbon_filter_state = 0 + device.state.speed = FanSpeed.FAN_SPEED_1.value + device.state.oscillation_angle_low = "000" + device.state.oscillation_angle_high = "000" + device.state.filter_life = "000" diff --git a/tests/components/dyson/test_air_quality.py b/tests/components/dyson/test_air_quality.py index ed2fbed34f3..fcd801616c9 100644 --- a/tests/components/dyson/test_air_quality.py +++ b/tests/components/dyson/test_air_quality.py @@ -17,14 +17,14 @@ import homeassistant.components.dyson.air_quality as dyson from homeassistant.helpers import discovery from homeassistant.setup import async_setup_component +from .common import load_mock_device + def _get_dyson_purecool_device(): """Return a valid device as provided by the Dyson web services.""" device = mock.Mock(spec=DysonPureCool) - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.name = "Living room" - device.connect = mock.Mock(return_value=True) - device.auto_connect = mock.Mock(return_value=True) device.environmental_state.particulate_matter_25 = "0014" device.environmental_state.particulate_matter_10 = "0025" device.environmental_state.nitrogen_dioxide = "0042" diff --git a/tests/components/dyson/test_climate.py b/tests/components/dyson/test_climate.py index dbc477203a1..345eae6f553 100644 --- a/tests/components/dyson/test_climate.py +++ b/tests/components/dyson/test_climate.py @@ -3,7 +3,7 @@ import unittest from unittest import mock import asynctest -from libpurecool.const import FocusMode, HeatMode, HeatState, HeatTarget, TiltState +from libpurecool.const import FocusMode, HeatMode, HeatState, HeatTarget from libpurecool.dyson_pure_hotcool_link import DysonPureHotCoolLink from libpurecool.dyson_pure_state import DysonPureHotCoolState @@ -12,6 +12,8 @@ from homeassistant.components.dyson import climate as dyson from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.setup import async_setup_component +from .common import load_mock_device + from tests.common import get_test_home_assistant @@ -41,7 +43,7 @@ def _get_config(): def _get_device_with_no_state(): """Return a device with no state.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" + load_mock_device(device) device.state = None device.environmental_state = None return device @@ -50,16 +52,14 @@ def _get_device_with_no_state(): def _get_device_off(): """Return a device with state off.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" - device.state = mock.Mock() - device.environmental_state = mock.Mock() + load_mock_device(device) return device def _get_device_focus(): """Return a device with fan state of focus mode.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" + load_mock_device(device) device.state.focus_mode = FocusMode.FOCUS_ON.value return device @@ -67,7 +67,7 @@ def _get_device_focus(): def _get_device_diffuse(): """Return a device with fan state of diffuse mode.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" + load_mock_device(device) device.state.focus_mode = FocusMode.FOCUS_OFF.value return device @@ -75,41 +75,28 @@ def _get_device_diffuse(): def _get_device_cool(): """Return a device with state of cooling.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" - device.serial = "XX-XXXXX-XX" - device.state.tilt = TiltState.TILT_FALSE.value + load_mock_device(device) device.state.focus_mode = FocusMode.FOCUS_OFF.value device.state.heat_target = HeatTarget.celsius(12) device.state.heat_mode = HeatMode.HEAT_OFF.value device.state.heat_state = HeatState.HEAT_STATE_OFF.value - device.environmental_state.temperature = 288 - device.environmental_state.humidity = 53 return device def _get_device_heat_off(): """Return a device with state of heat reached target.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" - device.state = mock.Mock() - device.state.tilt = TiltState.TILT_FALSE.value - device.state.focus_mode = FocusMode.FOCUS_ON.value - device.state.heat_target = HeatTarget.celsius(20) + load_mock_device(device) device.state.heat_mode = HeatMode.HEAT_ON.value device.state.heat_state = HeatState.HEAT_STATE_OFF.value - device.environmental_state.temperature = 293 - device.environmental_state.humidity = 53 return device def _get_device_heat_on(): """Return a device with state of heating.""" device = mock.Mock(spec=DysonPureHotCoolLink) - device.name = "Device_name" + load_mock_device(device) device.serial = "YY-YYYYY-YY" - device.state = mock.Mock() - device.state.tilt = TiltState.TILT_FALSE.value - device.state.focus_mode = FocusMode.FOCUS_ON.value device.state.heat_target = HeatTarget.celsius(23) device.state.heat_mode = HeatMode.HEAT_ON.value device.state.heat_state = HeatState.HEAT_STATE_ON.value diff --git a/tests/components/dyson/test_fan.py b/tests/components/dyson/test_fan.py index 367a86eabb4..7801c897723 100644 --- a/tests/components/dyson/test_fan.py +++ b/tests/components/dyson/test_fan.py @@ -26,6 +26,8 @@ from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_O from homeassistant.helpers import discovery from homeassistant.setup import async_setup_component +from .common import load_mock_device + from tests.common import get_test_home_assistant @@ -40,37 +42,19 @@ class MockDysonState(DysonPureCoolState): def _get_dyson_purecool_device(): """Return a valid device as provided by the Dyson web services.""" device = mock.Mock(spec=DysonPureCool) - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.name = "Living room" - device.connect = mock.Mock(return_value=True) - device.auto_connect = mock.Mock(return_value=True) - device.state = mock.Mock() - device.state.oscillation = "OION" - device.state.fan_power = "ON" - device.state.speed = FanSpeed.FAN_SPEED_AUTO.value - device.state.night_mode = "OFF" - device.state.auto_mode = "ON" - device.state.oscillation_angle_low = "0090" - device.state.oscillation_angle_high = "0180" - device.state.front_direction = "ON" - device.state.sleep_timer = 60 - device.state.hepa_filter_state = "0090" - device.state.carbon_filter_state = "0080" return device def _get_dyson_purecoollink_device(): """Return a valid device as provided by the Dyson web services.""" device = mock.Mock(spec=DysonPureCoolLink) - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.name = "Living room" - device.connect = mock.Mock(return_value=True) - device.auto_connect = mock.Mock(return_value=True) - device.state = mock.Mock() device.state.oscillation = "ON" device.state.fan_mode = "FAN" device.state.speed = FanSpeed.FAN_SPEED_AUTO.value - device.state.night_mode = "OFF" return device diff --git a/tests/components/dyson/test_init.py b/tests/components/dyson/test_init.py index 067f6a360ed..37cc69b6765 100644 --- a/tests/components/dyson/test_init.py +++ b/tests/components/dyson/test_init.py @@ -4,13 +4,15 @@ from unittest import mock from homeassistant.components import dyson +from .common import load_mock_device + from tests.common import get_test_home_assistant def _get_dyson_account_device_available(): """Return a valid device provide by Dyson web services.""" device = mock.Mock() - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.connect = mock.Mock(return_value=True) device.auto_connect = mock.Mock(return_value=True) return device @@ -19,7 +21,7 @@ def _get_dyson_account_device_available(): def _get_dyson_account_device_not_available(): """Return an invalid device provide by Dyson web services.""" device = mock.Mock() - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.connect = mock.Mock(return_value=False) device.auto_connect = mock.Mock(return_value=False) return device @@ -28,7 +30,7 @@ def _get_dyson_account_device_not_available(): def _get_dyson_account_device_error(): """Return an invalid device raising OSError while connecting.""" device = mock.Mock() - device.serial = "XX-XXXXX-XX" + load_mock_device(device) device.connect = mock.Mock(side_effect=OSError("Network error")) return device diff --git a/tests/components/dyson/test_sensor.py b/tests/components/dyson/test_sensor.py index 6dd29741153..4d3d1c96101 100644 --- a/tests/components/dyson/test_sensor.py +++ b/tests/components/dyson/test_sensor.py @@ -18,20 +18,15 @@ from homeassistant.const import ( from homeassistant.helpers import discovery from homeassistant.setup import async_setup_component +from .common import load_mock_device + from tests.common import get_test_home_assistant def _get_dyson_purecool_device(): """Return a valid device provide by Dyson web services.""" device = mock.Mock(spec=DysonPureCool) - device.serial = "XX-XXXXX-XX" - device.name = "Living room" - device.connect = mock.Mock(return_value=True) - device.auto_connect = mock.Mock(return_value=True) - device.environmental_state.humidity = 42 - device.environmental_state.temperature = 280 - device.state.hepa_filter_state = 90 - device.state.carbon_filter_state = 80 + load_mock_device(device) return device @@ -61,10 +56,9 @@ def _get_device_without_state(): def _get_with_state(): """Return a valid device with state values.""" device = mock.Mock() + load_mock_device(device) 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 = 45 device.environmental_state.temperature = 295 @@ -76,14 +70,10 @@ def _get_with_state(): def _get_with_standby_monitoring(): """Return a valid device with state but with standby monitoring disable.""" device = mock.Mock() + load_mock_device(device) 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 diff --git a/tests/ignore_uncaught_exceptions.py b/tests/ignore_uncaught_exceptions.py index 3b569ebaedd..8e6004d362e 100644 --- a/tests/ignore_uncaught_exceptions.py +++ b/tests/ignore_uncaught_exceptions.py @@ -1,35 +1,5 @@ """List of modules that have uncaught exceptions today. Will be shrunk over time.""" IGNORE_UNCAUGHT_EXCEPTIONS = [ - ("tests.components.dyson.test_air_quality", "test_purecool_aiq_attributes"), - ("tests.components.dyson.test_air_quality", "test_purecool_aiq_update_state"), - ( - "tests.components.dyson.test_air_quality", - "test_purecool_component_setup_only_once", - ), - ("tests.components.dyson.test_air_quality", "test_purecool_aiq_without_discovery"), - ( - "tests.components.dyson.test_air_quality", - "test_purecool_aiq_empty_environment_state", - ), - ( - "tests.components.dyson.test_climate", - "test_setup_component_with_parent_discovery", - ), - ("tests.components.dyson.test_fan", "test_purecoollink_attributes"), - ("tests.components.dyson.test_fan", "test_purecool_turn_on"), - ("tests.components.dyson.test_fan", "test_purecool_set_speed"), - ("tests.components.dyson.test_fan", "test_purecool_turn_off"), - ("tests.components.dyson.test_fan", "test_purecool_set_dyson_speed"), - ("tests.components.dyson.test_fan", "test_purecool_oscillate"), - ("tests.components.dyson.test_fan", "test_purecool_set_night_mode"), - ("tests.components.dyson.test_fan", "test_purecool_set_auto_mode"), - ("tests.components.dyson.test_fan", "test_purecool_set_angle"), - ("tests.components.dyson.test_fan", "test_purecool_set_flow_direction_front"), - ("tests.components.dyson.test_fan", "test_purecool_set_timer"), - ("tests.components.dyson.test_fan", "test_purecool_update_state"), - ("tests.components.dyson.test_fan", "test_purecool_update_state_filter_inv"), - ("tests.components.dyson.test_fan", "test_purecool_component_setup_only_once"), - ("tests.components.dyson.test_sensor", "test_purecool_component_setup_only_once"), ("tests.components.ios.test_init", "test_creating_entry_sets_up_sensor"), ("tests.components.ios.test_init", "test_not_configuring_ios_not_creates_entry"), ("tests.components.local_file.test_camera", "test_file_not_readable"),