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 libpurecoollink.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: connected = dyson_device.connect(None, device["device_ip"], timeout, retry) 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) 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.connect(None, None, 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") discovery.load_platform(hass, "sensor", DOMAIN, {}, config) discovery.load_platform(hass, "fan", DOMAIN, {}, config) return True
def test_connect_device_fail(self, mocked_login, mocked_list_devices, mocked_connect, mocked_close_zeroconf): 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, 1) connected = devices[0].connect(None, retry=1, timeout=1) self.assertFalse(connected)
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, 1) self.assertEqual(len(devices), 2) 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')
def post(self): Email = request.json['email'] Password = request.json['password'] Language = request.json['language'] dyson_account = DysonAccount( Email, Password, Language) logged = dyson_account.login() if not logged: return {'success': False, 'message': 'Unable to login to Dyson account'} # List devices available on the Dyson account devices = dyson_account.devices() # Connect using discovery to the first device connected = devices[0].auto_connect() if not connected: return {'success': False, 'message': 'Successfully authenticated but unable to connect to your first device : ' + devices[0]} return jsonify(devices[0].state)
def test_connect_device(self, mocked_login, mocked_list_devices, mocked_connect, mocked_loop): dyson_account = DysonAccount("email", "password", "language") logged = dyson_account.login() self.assertEqual(mocked_login.call_count, 1) self.assertTrue(logged) devices = dyson_account.devices() self.assertEqual(mocked_list_devices.call_count, 1) network_device = NetworkDevice('device-1', 'host', 1111) devices[0].connection_callback(True) devices[0]._add_network_device(network_device) connected = devices[0].connect(None) self.assertTrue(connected) self.assertIsNone(devices[0].state) self.assertEqual(devices[0].network_device, network_device) self.assertEqual(mocked_connect.call_count, 1) self.assertEqual(mocked_loop.call_count, 1)
def test_connect_device_with_config(self, mocked_login, mocked_list_devices, mocked_connect, mocked_loop): dyson_account = DysonAccount("email", "password", "language") logged = dyson_account.login() self.assertEqual(mocked_login.call_count, 1) self.assertTrue(logged) devices = dyson_account.devices() self.assertEqual(mocked_list_devices.call_count, 1) devices[0].connection_callback(True) connected = devices[0].connect(Mock(), "192.168.0.2") self.assertTrue(connected) self.assertIsNone(devices[0].state) self.assertEqual(devices[0].network_device.name, "device-1") self.assertEqual(devices[0].network_device.address, "192.168.0.2") self.assertEqual(devices[0].network_device.port, 1883) self.assertEqual(mocked_connect.call_count, 1) self.assertEqual(mocked_loop.call_count, 1)
def dyson_stop(): import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) dyson_account = DysonAccount(config["account"], config["password"], config["language"]) logged = dyson_account.login() if not logged: response = Response(status=500) else: devices = dyson_account.devices() connected = devices[0].auto_connect() if not connected: response = Response(status=500) else: devices[0].set_configuration(fan_mode=FanMode.OFF) devices[0].disconnect response = Response(status=200) return response
from libpurecoollink.dyson import DysonAccount from libpurecoollink.const import FanSpeed, FanMode, NightMode, Oscillation, \ FanState, StandbyMonitoring, QualityTarget, ResetFilter, HeatMode, \ FocusMode, HeatTarget import urllib3 from config import config urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) dyson_account = DysonAccount(config["account"], config["password"], config["language"]) logged = dyson_account.login() if logged: print("login successfully") devices = dyson_account.devices() connected = devices[0].connect("192.168.1.101") if connected: print("connect successfully") devices[0].set_configuration(fan_mode=FanMode.OFF) devices[0].disconnect()
class Controller(polyinterface.Controller): def __init__(self, polyglot): super().__init__(polyglot) self.name = 'Dyson Controller' self.address = 'dysonctrl' self.primary = self.address self.dyson = None self.devlist = None 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() def stop(self): LOGGER.info('Dyson is stopping') for node in self.nodes: if self.nodes[node].address != self.address: self.nodes[node].stop() def updateInfo(self): pass def query(self): for node in self.nodes: self.nodes[node].reportDrivers() def discover(self, command=None): for dev in self.dyson.devices(): address = dev.serial.replace('-','').lower()[:14] name = dev.name if not address in self.nodes: if dev.product_type == DYSON_PURE_COOL or dev.product_type == DYSON_PURE_COOL_DESKTOP: LOGGER.info('Adding v2 product: {}, name: {}'.format(dev.product_type, dev.name)) self.addNode(DysonPureFan(self, self.address, address, name, dev)) elif dev.product_type == DYSON_PURE_COOL_LINK_TOUR: LOGGER.info('Adding v1 product: {}, name: {}'.format(dev.product_type, dev.name)) self.addNode(DysonPureFanV1(self, self.address, address, name, dev)) else: LOGGER.info('Found product type: {}, name: {} but it\'s not yet supported'.format(dev.product_type, dev.name)) id = 'DYSONCTRL' commands = {'DISCOVER': discover} drivers = [{'driver': 'ST', 'value': 1, 'uom': 2}]