Beispiel #1
0
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())
Beispiel #2
0
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())
Beispiel #3
0
    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
Beispiel #4
0
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()
Beispiel #5
0
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")
Beispiel #6
0
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']))
Beispiel #7
0
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)
Beispiel #8
0
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))
Beispiel #9
0
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()
Beispiel #10
0
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))
Beispiel #11
0
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
Beispiel #13
0
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)
Beispiel #14
0
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))
Beispiel #15
0
def map(vac: mirobo.Vacuum):
    """Returns the map token."""
    click.echo(vac.map())
Beispiel #16
0
def find(vac: mirobo.Vacuum):
    """Find the robot."""
    click.echo("Sending find the robot calls.")
    click.echo(vac.find())
Beispiel #17
0
def start(vac: mirobo.Vacuum):
    """Start cleaning."""
    click.echo("Starting cleaning: %s" % vac.start())
Beispiel #18
0
def sound(vac: mirobo.Vacuum):
    """Query sound settings."""
    click.echo(vac.sound_info())
Beispiel #19
0
def left(vac: mirobo.Vacuum, degrees: int):
    """Turn to left."""
    click.echo("Turning %s degrees left" % degrees)
    return vac.manual_control(degrees, 0)
Beispiel #20
0
def home(vac: mirobo.Vacuum):
    """Return home."""
    click.echo("Requesting return to home: %s" % vac.home())
Beispiel #21
0
def stop(vac: mirobo.Vacuum):
    """Stop cleaning."""
    click.echo("Stop cleaning: %s" % vac.stop())
Beispiel #22
0
def pause(vac: mirobo.Vacuum):
    """Pause cleaning."""
    click.echo("Pausing: %s" % vac.pause())
Beispiel #23
0
def spot(vac: mirobo.Vacuum):
    """Start spot cleaning."""
    click.echo("Starting spot cleaning: %s" % vac.spot())
Beispiel #24
0
def info(vac: mirobo.Vacuum):
    """Returns info"""
    res = vac.info()

    click.echo(res)
    _LOGGER.debug("Full response: %s" % pf(res.raw))
Beispiel #25
0
def right(vac: mirobo.Vacuum, degrees: int):
    """Turn to right."""
    click.echo("Turning right")
    return vac.manual_control(-degrees, 0)
Beispiel #26
0
def backward(vac: mirobo.Vacuum, amount:float):
    """Run backwards."""
    click.echo("Moving backwards")
    return vac.manual_control(0, -amount)
Beispiel #27
0
def serial_number(vac: mirobo.Vacuum):
    """Query serial number."""
    click.echo("Serial#: %s" % vac.serial_number())
Beispiel #28
0
def move(vac: mirobo.Vacuum, rotation: float, velocity: float, duration: int):
    """Pass raw manual values"""
    return vac.manual_control(rotation, velocity, duration)
Beispiel #29
0
def stop(vac: mirobo.Vacuum):
    """Deactivate the manual mode."""
    click.echo("Deactivating manual controls")
    return vac.manual_stop()
Beispiel #30
0
def info(vac: mirobo.Vacuum):
    """Return device information."""
    res = vac.info()

    click.echo("%s" % res)
    _LOGGER.debug("Full response: %s", pf(res.raw))