def fanspeed(vac: mirobo.Vacuum, speed): """Query and adjust the fan speed.""" if speed: click.echo("Setting fan speed to %s" % speed) vac.set_fan_speed(speed) else: click.echo("Current fan speed: %s" % vac.fan_speed())
def timezone(vac: mirobo.Vacuum, tz=None): """Query or set the timezone.""" if tz is not None: click.echo("Setting timezone to: %s" % tz) click.echo(vac.set_timezone(tz)) else: click.echo("Timezone: %s" % vac.timezone())
def vacuum(self): if not self._vacuum: from mirobo import Vacuum _LOGGER.info("initializing with host %s token %s" % (self.host, self.token)) self._vacuum = Vacuum(self.host, self.token) return self._vacuum
def manual(vac: mirobo.Vacuum): """Control the robot manually.""" command = '' if command == 'start': click.echo("Starting manual control") return vac.manual_start() if command == 'stop': click.echo("Stopping manual control") return vac.manual_stop()
def update(vac: mirobo.Vacuum, timer_id, enable, disable): """Enable/disable a timer.""" from mirobo.vacuum import TimerState if enable and not disable: vac.update_timer(timer_id, TimerState.On) elif disable and not enable: vac.update_timer(timer_id, TimerState.Off) else: click.echo("You need to specify either --enable or --disable")
def dnd(vac: mirobo.Vacuum, cmd: str, start_hr: int, start_min: int, end_hr: int, end_min: int): """Query and adjust do-not-disturb mode.""" if cmd == "off": click.echo("Disabling DND..") print(vac.disable_dnd()) elif cmd == "on": click.echo("Enabling DND %s:%s to %s:%s" % (start_hr, start_min, end_hr, end_min)) click.echo(vac.set_dnd(start_hr, start_min, end_hr, end_min)) else: x = vac.dnd_status()[0] click.echo("DND %02i:%02i to %02i:%02i (enabled: %s)" % (x['start_hour'], x['start_minute'], x['end_hour'], x['end_minute'], x['enabled']))
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the Xiaomi vacuum cleaner robot platform.""" from mirobo import Vacuum if PLATFORM not in hass.data: hass.data[PLATFORM] = {} host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) # Create handler _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) vacuum = Vacuum(host, token) mirobo = MiroboVacuum(name, vacuum) hass.data[PLATFORM][host] = mirobo async_add_devices([mirobo], update_before_add=True) @asyncio.coroutine def async_service_handler(service): """Map services to methods on MiroboVacuum.""" method = SERVICE_TO_METHOD.get(service.service) params = { key: value for key, value in service.data.items() if key != ATTR_ENTITY_ID } entity_ids = service.data.get(ATTR_ENTITY_ID) if entity_ids: target_vacuums = [ vac for vac in hass.data[PLATFORM].values() if vac.entity_id in entity_ids ] else: target_vacuums = hass.data[PLATFORM].values() update_tasks = [] for vacuum in target_vacuums: yield from getattr(vacuum, method['method'])(**params) for vacuum in target_vacuums: update_coro = vacuum.async_update_ha_state(True) update_tasks.append(update_coro) if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop) descriptions = yield from hass.async_add_job( load_yaml_config_file, os.path.join(os.path.dirname(__file__), 'services.yaml')) for vacuum_service in SERVICE_TO_METHOD: schema = SERVICE_TO_METHOD[vacuum_service].get('schema', VACUUM_SERVICE_SCHEMA) hass.services.async_register( DOMAIN, vacuum_service, async_service_handler, description=descriptions.get(vacuum_service), schema=schema)
def raw_command(vac: mirobo.Vacuum, cmd, parameters): """Run a raw command.""" params = [] # type: Any if parameters: params = ast.literal_eval(parameters) click.echo("Sending cmd %s with params %s" % (cmd, params)) click.echo(vac.raw_command(cmd, params))
def cleaning_history(vac: mirobo.Vacuum): """Query the cleaning history.""" res = vac.clean_history() click.echo("Total clean count: %s" % res.count) click.echo("Cleaned for: %s (area: %s m²)" % (res.total_duration, res.total_area)) click.echo() for idx, id_ in enumerate(res.ids): for e in vac.clean_details(id_): color = "green" if e.complete else "yellow" click.echo(click.style( "Clean #%s: %s-%s (complete: %s, error: %s)" % ( idx, e.start, e.end, e.complete, e.error), bold=True, fg=color)) click.echo(" Area cleaned: %s m²" % e.area) click.echo(" Duration: (%s)" % e.duration) click.echo()
def timer(ctx, vac: mirobo.Vacuum): """List and modify existing timers.""" if ctx.invoked_subcommand is not None: return timers = vac.timer() click.echo("Timezone: %s\n" % vac.timezone()) for idx, timer in enumerate(timers): color = "green" if timer.enabled else "yellow" click.echo( click.style("Timer #%s, id %s (ts: %s)" % (idx, timer.id, timer.ts), bold=True, fg=color)) click.echo(" %s" % timer.cron) min, hr, x, y, days = timer.cron.split(' ') cron = "%s %s %s %s %s" % (min, hr, x, y, days) click.echo(" %s" % pretty_cron.prettify_cron(cron))
def consumables(vac: mirobo.Vacuum): """Return consumables status.""" res = vac.consumable_status() click.echo("Main brush: %s (left %s)" % (res.main_brush, res.main_brush_left)) click.echo("Side brush: %s (left %s)" % (res.side_brush, res.side_brush_left)) click.echo("Filter: %s (left %s)" % (res.filter, res.filter_left)) click.echo("Sensor dirty: %s" % res.sensor_dirty)
def vacuum(self): """Property accessor for vacuum object.""" if not self._vacuum: from mirobo import Vacuum _LOGGER.info("initializing with host %s token %s", self.host, self.token) self._vacuum = Vacuum(self.host, self.token) return self._vacuum
def status(vac: mirobo.Vacuum): """Returns the state information.""" res = vac.status() if not res: return # bail out if res.error_code: click.echo(click.style("Error: %s !" % res.error, bold=True, fg='red')) click.echo(click.style("State: %s" % res.state, bold=True)) click.echo("Battery: %s %%" % res.battery) click.echo("Fanspeed: %s %%" % res.fanspeed) click.echo("Cleaning since: %s" % res.clean_time) click.echo("Cleaned area: %s m²" % res.clean_area)
def timer(vac: mirobo.Vacuum, timer): """Schedule vacuuming, times in GMT.""" if timer: raise NotImplementedError() # vac.set_timer(x) pass else: timers = vac.timer() for idx, timer in enumerate(timers): color = "green" if timer.enabled else "yellow" # Note ts == ID for changes click.echo(click.style("Timer #%s, id %s (ts: %s)" % ( idx, timer.id, timer.ts), bold=True, fg=color)) print(" %s" % timer.cron) min, hr, x, y, days = timer.cron.split(' ') # hr is in gmt+8 (chinese time), TODO convert to local hr = (int(hr) - 8) % 24 cron = "%s %s %s %s %s" % (min, hr, x, y, days) click.echo(" %s" % pretty_cron.prettify_cron(cron))
def map(vac: mirobo.Vacuum): """Returns the map token.""" click.echo(vac.map())
def find(vac: mirobo.Vacuum): """Find the robot.""" click.echo("Sending find the robot calls.") click.echo(vac.find())
def start(vac: mirobo.Vacuum): """Start cleaning.""" click.echo("Starting cleaning: %s" % vac.start())
def sound(vac: mirobo.Vacuum): """Query sound settings.""" click.echo(vac.sound_info())
def left(vac: mirobo.Vacuum, degrees: int): """Turn to left.""" click.echo("Turning %s degrees left" % degrees) return vac.manual_control(degrees, 0)
def home(vac: mirobo.Vacuum): """Return home.""" click.echo("Requesting return to home: %s" % vac.home())
def stop(vac: mirobo.Vacuum): """Stop cleaning.""" click.echo("Stop cleaning: %s" % vac.stop())
def pause(vac: mirobo.Vacuum): """Pause cleaning.""" click.echo("Pausing: %s" % vac.pause())
def spot(vac: mirobo.Vacuum): """Start spot cleaning.""" click.echo("Starting spot cleaning: %s" % vac.spot())
def info(vac: mirobo.Vacuum): """Returns info""" res = vac.info() click.echo(res) _LOGGER.debug("Full response: %s" % pf(res.raw))
def right(vac: mirobo.Vacuum, degrees: int): """Turn to right.""" click.echo("Turning right") return vac.manual_control(-degrees, 0)
def backward(vac: mirobo.Vacuum, amount:float): """Run backwards.""" click.echo("Moving backwards") return vac.manual_control(0, -amount)
def serial_number(vac: mirobo.Vacuum): """Query serial number.""" click.echo("Serial#: %s" % vac.serial_number())
def move(vac: mirobo.Vacuum, rotation: float, velocity: float, duration: int): """Pass raw manual values""" return vac.manual_control(rotation, velocity, duration)
def stop(vac: mirobo.Vacuum): """Deactivate the manual mode.""" click.echo("Deactivating manual controls") return vac.manual_stop()
def info(vac: mirobo.Vacuum): """Return device information.""" res = vac.info() click.echo("%s" % res) _LOGGER.debug("Full response: %s", pf(res.raw))