async def async_setup(hass, config): """Set up the Worx Landroid Cloud component.""" import pyworxcloud hass.data[LANDROID_API] = {} dev = 0 for cloud in config[DOMAIN]: cloud_email = cloud[CONF_EMAIL] cloud_password = cloud[CONF_PASSWORD] master = pyworxcloud.WorxCloud() auth = await master.initialize(cloud_email, cloud_password) if not auth: _LOGGER.warning("Error in authentication!") return False num_dev = await hass.async_add_executor_job(master.enumerate) for device in range(num_dev): client.append(dev) _LOGGER.debug("Connecting to device ID %s (%s)", device, cloud_email) client[dev] = pyworxcloud.WorxCloud() await client[dev].initialize(cloud_email, cloud_password) await hass.async_add_executor_job(client[dev].connect, device, False) api = WorxLandroidAPI(hass, dev, client[dev], config) await api.async_force_update() async_track_time_interval(hass, api.async_update, SCAN_INTERVAL) async_track_time_interval(hass, api.async_force_update, FORCED_UPDATE) hass.data[LANDROID_API][dev] = api dev += 1 async def handle_start(call): """Handle start service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.start() else: client[0].start() hass.services.async_register(DOMAIN, SERVICE_START, handle_start) async def handle_pause(call): """Handle pause service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.pause() else: client[0].pause() hass.services.async_register(DOMAIN, SERVICE_PAUSE, handle_pause) async def handle_home(call): """Handle pause service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.stop() else: client[0].stop() hass.services.async_register(DOMAIN, SERVICE_HOME, handle_home) async def handle_config(call): """Handle config service call.""" id = 0 sendData = False tmpdata = {} if "id" in call.data: _LOGGER.debug("Data from Home Assistant: %s", call.data["id"]) for cli in client: attrs = vars(cli) if (attrs["id"] == call.data["id"]): break else: id += 1 if "raindelay" in call.data: tmpdata["rd"] = call.data["raindelay"] _LOGGER.debug("Setting rain_delay for %s to %s", client[id].name, call.data["raindelay"]) sendData = True if "timeextension" in call.data: tmpdata["sc"] = {} tmpdata["sc"]["p"] = call.data["timeextension"] data = json.dumps(tmpdata) _LOGGER.debug("Setting time_extension for %s to %s", client[id].name, call.data["timeextension"]) sendData = True if sendData: data = json.dumps(tmpdata) _LOGGER.debug("Sending: %s", data) client[id].sendData(data) hass.services.async_register(DOMAIN, SERVICE_CONFIG, handle_config) return True
async def async_setup(hass, config): """Set up the Worx Landroid Cloud component.""" import pyworxcloud hass.data[LANDROID_API] = {} dev = 0 partymode = False ots = False for cloud in config[DOMAIN]: cloud_email = cloud[CONF_EMAIL] cloud_password = cloud[CONF_PASSWORD] cloud_type = cloud.get(CONF_TYPE, 'worx') master = pyworxcloud.WorxCloud() auth = await hass.async_add_executor_job(master.initialize, cloud_email, cloud_password, cloud_type) if not auth: _LOGGER.warning("Error in authentication!") return False try: num_dev = await hass.async_add_executor_job(master.enumerate) except Exception as err: _LOGGER.warning(err) return False for device in range(num_dev): client.append(dev) _LOGGER.debug("Connecting to device ID %s (%s)", device, cloud_email) client[dev] = pyworxcloud.WorxCloud() await hass.async_add_executor_job(client[dev].initialize, cloud_email, cloud_password, cloud_type) await hass.async_add_executor_job(client[dev].connect, device, False) api = WorxLandroidAPI(hass, dev, client[dev], config) await api.async_force_update() async_track_time_interval(hass, api.async_update, SCAN_INTERVAL) async_track_time_interval(hass, api.async_force_update, FORCED_UPDATE) hass.data[LANDROID_API][dev] = api _LOGGER.debug("Partymode available: %s", client[dev].partymode) if not partymode and client[dev].partymode: partymode = True if not ots and client[dev].ots_enabled: ots = True dev += 1 async def handle_poll(call): """Handle poll service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: error = cli.tryToPoll() if error is not None: _LOGGER.warning(error) elif error is None: _LOGGER.debug("Poll successful - updating info") await hass.async_add_executor_job(cli.getStatus) else: error = client[0].tryToPoll() if error is not None: _LOGGER.warning(error) elif error is None: _LOGGER.debug("Poll successful - updating info") await hass.async_add_executor_job(client[0].getStatus) hass.services.async_register(DOMAIN, SERVICE_POLL, handle_poll) async def handle_start(call): """Handle start service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.start() else: client[0].start() hass.services.async_register(DOMAIN, SERVICE_START, handle_start) async def handle_pause(call): """Handle pause service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.pause() else: client[0].pause() hass.services.async_register(DOMAIN, SERVICE_PAUSE, handle_pause) async def handle_home(call): """Handle pause service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.stop() else: client[0].stop() hass.services.async_register(DOMAIN, SERVICE_HOME, handle_home) async def handle_config(call): """Handle config service call.""" id = 0 sendData = False tmpdata = {} if "id" in call.data: _LOGGER.debug("Data from Home Assistant: %s", call.data["id"]) for cli in client: attrs = vars(cli) if (attrs["id"] == int(call.data["id"])): break else: id += 1 if "raindelay" in call.data: tmpdata["rd"] = int(call.data["raindelay"]) _LOGGER.debug("Setting rain_delay for %s to %s", client[id].name, call.data["raindelay"]) sendData = True if "timeextension" in call.data: tmpdata["sc"] = {} tmpdata["sc"]["p"] = int(call.data["timeextension"]) data = json.dumps(tmpdata) _LOGGER.debug("Setting time_extension for %s to %s", client[id].name, call.data["timeextension"]) sendData = True if "multizone_distances" in call.data: tmpdata["mz"] = [int(x) for x in call.data["multizone_distances"]] data = json.dumps(tmpdata) _LOGGER.debug("Setting multizone distances for %s to %s", client[id].name, call.data["multizone_distances"]) sendData = True if "multizone_probabilities" in call.data: tmpdata["mzv"] = [] for idx, val in enumerate(call.data["multizone_probabilities"]): for _ in range(val): tmpdata["mzv"].append(idx) data = json.dumps(tmpdata) _LOGGER.debug("Setting multizone probabilities for %s to %s", client[id].name, call.data["multizone_probabilities"]) sendData = True if sendData: data = json.dumps(tmpdata) _LOGGER.debug("Sending: %s", data) client[id].sendData(data) hass.services.async_register(DOMAIN, SERVICE_CONFIG, handle_config) async def handle_partymode(call): """Handle partymode service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.partyMode(call.data["enable"]) else: client[0].partyMode(call.data["enable"]) if partymode: hass.services.async_register(DOMAIN, SERVICE_PARTYMODE, handle_partymode) async def handle_setzone(call): """Handle setzone service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.setZone(call.data["zone"]) else: client[0].setZone(call.data["zone"]) hass.services.async_register(DOMAIN, SERVICE_SETZONE, handle_setzone) async def handle_lock(call): """Handle lock service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.lock(call.data["enable"]) else: client[0].lock(call.data["enable"]) hass.services.async_register(DOMAIN, SERVICE_LOCK, handle_lock) async def handle_restart(call): """Handle restart service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.restart() else: client[0].restart() hass.services.async_register(DOMAIN, SERVICE_RESTART, handle_restart) async def handle_edgecut(call): """Handle restart service call.""" if "id" in call.data: ID = int(call.data["id"]) for cli in client: attrs = vars(cli) if attrs["id"] == ID: cli.startEdgecut() else: client[0].startEdgecut() if ots: hass.services.async_register(DOMAIN, SERVICE_EDGECUT, handle_edgecut) return True
async def async_setup(hass, config): """Set up the Worx Landroid Cloud component.""" import pyworxcloud hass.data[LANDROID_API] = {} dev = 0 for cloud in config[DOMAIN]: cloud_email = cloud[CONF_EMAIL] cloud_password = cloud[CONF_PASSWORD] master = pyworxcloud.WorxCloud() auth = await master.initialize(cloud_email, cloud_password) if not auth: _LOGGER.warning("Error in authentication!") return False num_dev = master.enumerate() for device in range(num_dev): client.append(dev) _LOGGER.debug("Connecting to device ID %s (%s)", device, cloud_email) client[dev] = pyworxcloud.WorxCloud() await client[dev].initialize(cloud_email, cloud_password) await client[dev].connect(device, False) api = WorxLandroidAPI(hass, dev, client[dev], config) async_track_time_interval(hass, api.async_update, SCAN_INTERVAL) hass.data[LANDROID_API][dev] = api dev += 1 async def handle_start(call): """Handle start service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if (attrs["id"] == ID): cli.start() else: client[0].start() hass.services.async_register(DOMAIN, SERVICE_START, handle_start) async def handle_pause(call): """Handle pause service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if (attrs["id"] == ID): cli.pause() else: client[0].pause() hass.services.async_register(DOMAIN, SERVICE_PAUSE, handle_pause) async def handle_home(call): """Handle pause service call.""" if "id" in call.data: ID = call.data["id"] for cli in client: attrs = vars(cli) if (attrs["id"] == ID): cli.stop() else: client[0].stop() hass.services.async_register(DOMAIN, SERVICE_HOME, handle_home) # async def handle_config(call): # """Handle config service call.""" # if "id" in call.data: # _LOGGER.debug("Data from Home Assistant: %s", call.data["id"]) # # for cli in client: # attrs = vars(cli) # if (attrs["id"] == call.data["id"]): # _LOGGER.debug(attrs["name"]) # else: # _LOGGER.debug("No ID present - using 0") # # hass.services.async_register(DOMAIN, SERVICE_CONFIG, handle_config) return True