Example #1
0
    def start(self):
        # LOGGER.setLevel(logging.INFO)
        LOGGER.info('Started Dyson controller')
        if 'username' not in self.polyConfig[
                'customParams'] or 'password' not in self.polyConfig[
                    'customParams']:
            LOGGER.error(
                'Please specify username and password in the NodeServer configuration parameters'
            )
            return False
        username = self.polyConfig['customParams']['username']
        password = self.polyConfig['customParams']['password']
        if 'country' in self.polyConfig['customParams']:
            country = self.polyConfig['customParams']['country']
        else:
            country = 'US'

        try:
            self.dyson = DysonAccount(username, password, country)
        except Exception as ex:
            LOGGER.error('ERROR connecting to the Dyson API: {}'.format(ex))
            return
        if 'devlist' in self.polyConfig['customParams']:
            try:
                self.devlist = json.loads(
                    self.polyConfig['customParams']['devlist'])
            except Exception as ex:
                LOGGER.error('Failed to parse the devlist: {}'.format(ex))
                return False
        logged_in = self.dyson.login()
        if not logged_in:
            LOGGER.error('Failed to login to Dyson account')
        else:
            self.discover()
Example #2
0
    def login_account(self):
        if self.__account:
            return self.__account

        # Log to Dyson account
        # Language is a two characters code (eg: FR)
        dyson_account = DysonAccount(self.__username, self.__password, self.__country)
        logged = dyson_account.login()
        self.__account = dyson_account

        if not logged:
            print('Unable to login to Dyson account')
            self.__account = None

        return self.__account
Example #3
0
 def test_list_devices(self, mocked_login, mocked_list_devices):
     dyson_account = DysonAccount("email", "password", "language")
     dyson_account.login()
     self.assertEqual(mocked_login.call_count, 1)
     self.assertTrue(dyson_account.logged)
     devices = dyson_account.devices()
     self.assertEqual(mocked_list_devices.call_count, 2)
     self.assertEqual(len(devices), 6)
     self.assertTrue(isinstance(devices[0], DysonPureCoolLink))
     self.assertTrue(isinstance(devices[1], DysonPureHotCoolLink))
     self.assertTrue(isinstance(devices[2], Dyson360Eye))
     self.assertTrue(devices[0].active)
     self.assertTrue(devices[0].auto_update)
     self.assertFalse(devices[0].new_version_available)
     self.assertEqual(devices[0].serial, 'device-id-1')
     self.assertEqual(devices[0].name, 'device-1')
     self.assertEqual(devices[0].version, '21.03.08')
     self.assertEqual(devices[0].product_type, '475')
     self.assertEqual(devices[0].credentials, 'password1')
Example #4
0
 def test_not_logged(self):
     dyson_account = DysonAccount("email", "password", "language")
     self.assertRaises(DysonNotLoggedException, dyson_account.devices)
Example #5
0
 def test_connect_account_failed(self, mocked_login):
     dyson_account = DysonAccount("email", "password", "language")
     logged = dyson_account.login()
     self.assertEqual(mocked_login.call_count, 1)
     self.assertFalse(logged)
Example #6
0
 def test_connect_account_cn(self, mocked_login):
     dyson_account = DysonAccount("email", "password", "CN")
     logged = dyson_account.login()
     self.assertEqual(mocked_login.call_count, 1)
     self.assertTrue(logged)
Example #7
0
def setup(hass, config):
    """Set up the Dyson parent component."""
    _LOGGER.info("Creating new Dyson component")

    if DYSON_DEVICES not in hass.data:
        hass.data[DYSON_DEVICES] = []

    from libpurecool.dyson import DysonAccount
    dyson_account = DysonAccount(config[DOMAIN].get(CONF_USERNAME),
                                 config[DOMAIN].get(CONF_PASSWORD),
                                 config[DOMAIN].get(CONF_LANGUAGE))

    logged = dyson_account.login()

    timeout = config[DOMAIN].get(CONF_TIMEOUT)
    retry = config[DOMAIN].get(CONF_RETRY)

    if not logged:
        _LOGGER.error("Not connected to Dyson account. Unable to add devices")
        return False

    _LOGGER.info("Connected to Dyson account")
    dyson_devices = dyson_account.devices()
    if CONF_DEVICES in config[DOMAIN] and config[DOMAIN].get(CONF_DEVICES):
        configured_devices = config[DOMAIN].get(CONF_DEVICES)
        for device in configured_devices:
            dyson_device = next(
                (d for d in dyson_devices if d.serial == device["device_id"]),
                None)
            if dyson_device:
                try:
                    connected = dyson_device.connect(device["device_ip"])
                    if connected:
                        _LOGGER.info("Connected to device %s", dyson_device)
                        hass.data[DYSON_DEVICES].append(dyson_device)
                    else:
                        _LOGGER.warning("Unable to connect to device %s",
                                        dyson_device)
                except OSError as ose:
                    _LOGGER.error("Unable to connect to device %s: %s",
                                  str(dyson_device.network_device), str(ose))
            else:
                _LOGGER.warning("Unable to find device %s in Dyson account",
                                device["device_id"])
    else:
        # Not yet reliable
        for device in dyson_devices:
            _LOGGER.info(
                "Trying to connect to device %s with timeout=%i "
                "and retry=%i", device, timeout, retry)
            connected = device.auto_connect(timeout, retry)
            if connected:
                _LOGGER.info("Connected to device %s", device)
                hass.data[DYSON_DEVICES].append(device)
            else:
                _LOGGER.warning("Unable to connect to device %s", device)

    # Start fan/sensors components
    if hass.data[DYSON_DEVICES]:
        _LOGGER.debug("Starting sensor/fan components")
        for platform in DYSON_PLATFORMS:
            discovery.load_platform(hass, platform, DOMAIN, {}, config)

    return True
Example #8
0

def print_new_attributes():
    print(device)
    print(device.state, type(device.state))
    print(device.environmental_state, type(device.environmental_state))
    print("---")


def wait():
    print("Waiting for 10 seconds")
    time.sleep(10)
    print("Wait done!")


dyson_account = DysonAccount(USER, PASS, LANG)
while dyson_account.login() == False:
    if dyson_account.wait_2fa_start and \
       dyson_account.authenticate():
        print('authent ok!')
    if dyson_account.wait_2fa_verify and \
       dyson_account.verify(input("Please input token received by mail:")):
        print('verify ok!')
        break
    if dyson_account.wait_2fa_start == False:
        dyson_account.prune()
    time.sleep(1)
dyson_account.nukeDeviceCache()
devices = dyson_account.devices()
print(devices)