def test_connect_device_with_config_failed(self, mocked_connect,
                                            mocked_loop_start,
                                            mocked_loop_stop):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/"
         "70ZGysII1Ke1i0ZHakFH84DZuxsSQ4KTT2v"
         "bCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     device.connection_callback(False)
     connected = device.connect("192.168.0.2")
     self.assertFalse(connected)
     self.assertIsNone(device.state)
     self.assertEqual(device.network_device.name, "device-1")
     self.assertEqual(device.network_device.address, "192.168.0.2")
     self.assertEqual(device.network_device.port, 1883)
     self.assertEqual(mocked_connect.call_count, 1)
     self.assertEqual(mocked_loop_start.call_count, 1)
     self.assertEqual(mocked_loop_stop.call_count, 1)
 def test_device_connected(self):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     device.connected = True
     self.assertTrue(device.connected)
     device.connected = False
     self.assertFalse(device.connected)
 def test_connect_device_fail(self, mocked_close_zeroconf):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/"
         "70ZGysII1Ke1i0ZHakFH84DZuxsSQ4KTT2v"
         "bCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     connected = device.auto_connect(retry=1, timeout=1)
     self.assertFalse(connected)
     self.assertEqual(mocked_close_zeroconf.call_count, 1)
 def test_on_connect_failed(self):
     userdata = Mock()
     userdata.product_type = 'ptype'
     userdata.serial = 'serial'
     DysonPureCoolLink.on_connect(None, userdata, None, 1)
     userdata.connection_callback.assert_called_with(False)
     self.assertEqual(userdata.connection_callback.call_count, 1)
 def test_environment_state(self):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     sensor_state = DysonEnvironmentalSensorState(
         open("tests/data/sensor.json", "r").read())
     device.environmental_state = sensor_state
     self.assertEqual(device.environmental_state.dust, 4)
 def test_on_connect(self):
     client = Mock()
     client.subscribe = Mock()
     userdata = Mock()
     userdata.status_topic = "ptype/serial/status/current"
     DysonPureCoolLink.on_connect(client, userdata, None, 0)
     userdata.connection_callback.assert_called_with(True)
     self.assertEqual(userdata.connection_callback.call_count, 1)
     client.subscribe.assert_called_with("ptype/serial/status/current")
    def test_on_message_sensor(self):
        def on_message(msg):
            assert isinstance(msg, DysonEnvironmentalSensorState)

        userdata = Mock()
        userdata.callback_message = [on_message]
        msg = Mock()
        payload = b'{"msg": "ENVIRONMENTAL-CURRENT-SENSOR-DATA","time":' \
                  b'"2017-06-17T23:05:49.001Z","data": '\
                  b'{"tact": "2967","hact": "0054","pact": "0004",' \
                  b'"vact": "0005","sltm": "0028"}}'
        msg.payload = payload
        DysonPureCoolLink.on_message(None, userdata, msg)
 def test_on_message_without_callback(self):
     userdata = Mock()
     userdata.callback_message = []
     msg = Mock()
     payload = b'{"msg":"CURRENT-STATE","time":' \
               b'"2017-02-19T15:00:18.000Z","mode-reason":"LAPP",' \
               b'"state-reason":"MODE","dial":"OFF","rssi":"-58",' \
               b'"product-state":{"fmod":"AUTO","fnst":"FAN",' \
               b'"fnsp":"AUTO","qtar":"0004","oson":"OFF","rhtm":"ON",' \
               b'"filf":"2159","ercd":"02C0","nmod":"ON","wacd":"NONE"},' \
               b'"scheduler":{"srsc":"cbd0","dstv":"0001","tzid":"0001"}}'
     msg.payload = payload
     DysonPureCoolLink.on_message(None, userdata, msg)
    def test_on_message_with_unknown_message(self):
        def on_message(msg):
            # Should not be called
            assert msg == 0

        userdata = Mock()
        userdata.callback_message = [on_message]
        msg = Mock()
        payload = b'{"msg": "ENVIRONMENTAL-CURRENT-SENSOR-DATAS","time":' \
                  b'"2017-06-17T23:05:49.001Z","data": ' \
                  b'{"tact": "2967","hact": "0054","pact": "0004",' \
                  b'"vact": "0005","sltm": "0028"}}'
        msg.payload = payload
        DysonPureCoolLink.on_message(None, userdata, msg)
 def test_device_dyson_listener(self, mocked_ntoa):
     listener = DysonPureCoolLink.DysonDeviceListener(
         'serial-1', on_add_device)
     zeroconf = Mock()
     listener.remove_service(zeroconf, "ptype", "serial-1")
     info = Mock()
     info.address = "192.168.0.1"
     zeroconf.get_service_info = Mock()
     zeroconf.get_service_info.return_value = info
     listener.add_service(zeroconf, '_dyson_mqtt._tcp.local.',
                          'ptype_serial-1._dyson_mqtt._tcp.local.')
    def test_on_message_hot(self):
        def on_message(msg):
            assert isinstance(msg, DysonPureHotCoolState)

        device = DysonPureCoolLink({
            "Active":
            True,
            "Serial":
            "device-id-1",
            "Name":
            "device-1",
            "ScaleUnit":
            "SU01",
            "Version":
            "21.03.08",
            "LocalCredentials":
            "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/"
            "70ZGysII1Ke1i0ZHakFH84DZuxsSQ4KTT2v"
            "bCm7uYeTORULKLKQ==",
            "AutoUpdate":
            True,
            "NewVersionAvailable":
            False,
            "ProductType":
            "455"
        })
        device.add_message_listener(on_message)
        msg = Mock()
        payload = open("tests/data/state_hot.json", "r").read()
        msg.payload = Mock()
        msg.payload.decode.return_value = payload
        DysonPureCoolLink.on_message(None, device, msg)
 def test_status_topic(self):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/"
         "70ZGysII1Ke1i0ZHakFH84DZuxsSQ4KTT2v"
         "bCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     self.assertEqual(device.status_topic, "475/device-id-1/status/current")
 def test_connect_device(self, mocked_connect, mocked_loop):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/"
         "70ZGysII1Ke1i0ZHakFH84DZuxsSQ4KTT2v"
         "bCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     network_device = NetworkDevice('device-1', 'host', 1111)
     device.state_data_available()
     device.sensor_data_available()
     device.connection_callback(True)
     device._add_network_device(network_device)
     connected = device.auto_connect()
     self.assertTrue(connected)
     self.assertIsNone(device.state)
     self.assertEqual(device.network_device, network_device)
     self.assertEqual(mocked_connect.call_count, 1)
     self.assertEqual(mocked_loop.call_count, 1)
     device.disconnect()
 def test_dont_set_configuration_if_not_connected(self, mocked_connect,
                                                  mocked_publish):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     network_device = NetworkDevice('device-1', 'host', 1111)
     device._add_network_device(network_device)
     device._current_state = DysonPureCoolState(
         open("tests/data/state.json", "r").read())
     device.connection_callback(False)
     connected = device.auto_connect()
     self.assertFalse(connected)
     self.assertEqual(mocked_connect.call_count, 1)
     device.set_configuration(fan_mode=FanMode.FAN,
                              oscillation=Oscillation.OSCILLATION_ON,
                              fan_speed=FanSpeed.FAN_SPEED_3,
                              night_mode=NightMode.NIGHT_MODE_OFF)
     self.assertEqual(mocked_publish.call_count, 0)
     self.assertEqual(
         device.__repr__(),
         "DysonPureCoolLink(serial=device-id-1,active=True,"
         "name=device-1,version=21.03.08,auto_update=True,"
         "new_version_available=False,product_type=475,"
         "network_device=NetworkDevice(name=device-1,"
         "address=host,port=1111))")
 def test_set_configuration_timer_off(self, mocked_connect, mocked_publish):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     network_device = NetworkDevice('device-1', 'host', 1111)
     device._add_network_device(network_device)
     device._current_state = DysonPureCoolState(
         open("tests/data/state.json", "r").read())
     device.connection_callback(True)
     device.state_data_available()
     device.sensor_data_available()
     connected = device.auto_connect()
     self.assertTrue(connected)
     self.assertEqual(mocked_connect.call_count, 1)
     device.set_configuration(sleep_timer=0)
     self.assertEqual(mocked_publish.call_count, 3)
     self.assertEqual(
         device.__repr__(),
         "DysonPureCoolLink(serial=device-id-1,active=True,"
         "name=device-1,version=21.03.08,auto_update=True,"
         "new_version_available=False,product_type=475,"
         "network_device=NetworkDevice(name=device-1,"
         "address=host,port=1111))")
     device.disconnect()
 def test_set_configuration_rst_filter(self, mocked_connect,
                                       mocked_publish):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     network_device = NetworkDevice('device-1', 'host', 1111)
     device._add_network_device(network_device)
     device._current_state = DysonPureCoolState(
         open("tests/data/state.json", "r").read())
     device.connection_callback(True)
     device.state_data_available()
     device.sensor_data_available()
     connected = device.auto_connect()
     self.assertTrue(connected)
     self.assertEqual(mocked_connect.call_count, 1)
     device.set_configuration(fan_mode=FanMode.FAN,
                              oscillation=Oscillation.OSCILLATION_ON,
                              fan_speed=FanSpeed.FAN_SPEED_3,
                              night_mode=NightMode.NIGHT_MODE_OFF,
                              quality_target=QualityTarget.QUALITY_NORMAL,
                              standby_monitoring=SM.STANDBY_MONITORING_ON,
                              reset_filter=ResetFilter.RESET_FILTER)
     self.assertEqual(mocked_publish.call_count, 3)
     self.assertEqual(
         device.__repr__(),
         "DysonPureCoolLink(serial=device-id-1,active=True,"
         "name=device-1,version=21.03.08,auto_update=True,"
         "new_version_available=False,product_type=475,"
         "network_device=NetworkDevice(name=device-1,"
         "address=host,port=1111))")
     device.disconnect()
 def test_dont_request_state_if_not_connected(self, mocked_connect,
                                              mocked_publish):
     device = DysonPureCoolLink({
         "Active":
         True,
         "Serial":
         "device-id-1",
         "Name":
         "device-1",
         "ScaleUnit":
         "SU01",
         "Version":
         "21.03.08",
         "LocalCredentials":
         "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
         "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
         "AutoUpdate":
         True,
         "NewVersionAvailable":
         False,
         "ProductType":
         "475"
     })
     network_device = NetworkDevice('device-1', 'host', 1111)
     device.connection_callback(False)
     device._add_network_device(network_device)
     connected = device.connect(None, "192.168.0.2")
     self.assertFalse(connected)
     self.assertEqual(mocked_connect.call_count, 1)
     device.request_current_state()
     self.assertEqual(mocked_publish.call_count, 0)
     device.request_environmental_state()
     self.assertEqual(mocked_publish.call_count, 0)
    def test_add_message_listener(self):
        def on_message():
            pass

        def on_message_2():
            pass

        device = DysonPureCoolLink({
            "Active":
            True,
            "Serial":
            "device-id-1",
            "Name":
            "device-1",
            "ScaleUnit":
            "SU01",
            "Version":
            "21.03.08",
            "LocalCredentials":
            "1/aJ5t52WvAfn+z+fjDuef86kQDQPefbQ6/70ZGysII1K"
            "e1i0ZHakFH84DZuxsSQ4KTT2vbCm7uYeTORULKLKQ==",
            "AutoUpdate":
            True,
            "NewVersionAvailable":
            False,
            "ProductType":
            "475"
        })
        device.add_message_listener(on_message)
        assert len(device.callback_message) == 1
        device.remove_message_listener(on_message)
        assert len(device.callback_message) == 0
        device.add_message_listener(on_message_2)
        device.add_message_listener(on_message)
        assert len(device.callback_message) == 2
        device.clear_message_listener()
        assert len(device.callback_message) == 0