| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,6 +84,8 @@ async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip): | |||
| 84 | 84 | else: | |
| 85 | 85 | click.echo("Unable to detect type, use --strip or --bulb or --plug!") | |
| 86 | 86 | return | |
| 87 | + | ||
| 88 | + await dev.update() | ||
| 87 | 89 | ctx.obj = dev | |
| 88 | 90 | ||
| 89 | 91 | if ctx.invoked_subcommand is None: | |
@@ -106,6 +108,8 @@ async def scan(dev): | |||
| 106 | 108 | for dev in devs: | |
| 107 | 109 | click.echo(f"\t {dev}") | |
| 108 | 110 | ||
| 111 | + return devs | ||
| 112 | + | ||
| 109 | 113 | ||
| 110 | 114 | @wifi.command() | |
| 111 | 115 | @click.argument("ssid") | |
@@ -120,18 +124,7 @@ async def join(dev: SmartDevice, ssid, password, keytype): | |||
| 120 | 124 | f"Response: {res} - if the device is not able to join the network, it will revert back to its previous state." | |
| 121 | 125 | ) | |
| 122 | 126 | ||
| 123 | - | ||
| 124 | - @cli.command() | ||
| 125 | - @click.option("--scrub/--no-scrub", default=True) | ||
| 126 | - @click.pass_context | ||
| 127 | - async def dump_discover(ctx, scrub): | ||
| 128 | - """Dump discovery information. | ||
| 129 | - | ||
| 130 | - Useful for dumping into a file to be added to the test suite. | ||
| 131 | - """ | ||
| 132 | - click.echo( | ||
| 133 | - "This is deprecated, use the script inside devtools to generate a devinfo file." | ||
| 134 | - ) | ||
| 127 | + return res | ||
| 135 | 128 | ||
| 136 | 129 | ||
| 137 | 130 | @cli.command() | |
@@ -158,35 +151,30 @@ async def discover(ctx, timeout, discover_only, dump_raw): | |||
| 158 | 151 | ||
| 159 | 152 | async def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3): | |
| 160 | 153 | """Discover a device identified by its alias.""" | |
| 161 | - click.echo( | ||
| 162 | - f"Trying to discover {alias} using {attempts} attempts of {timeout} seconds" | ||
| 163 | - ) | ||
| 164 | 154 | for attempt in range(1, attempts): | |
| 165 | - click.echo(f"Attempt {attempt} of {attempts}") | ||
| 166 | 155 | found_devs = await Discover.discover(target=target, timeout=timeout) | |
| 167 | - found_devs = found_devs.items() | ||
| 168 | - for ip, dev in found_devs: | ||
| 156 | + for ip, dev in found_devs.items(): | ||
| 169 | 157 | if dev.alias.lower() == alias.lower(): | |
| 170 | 158 | host = dev.host | |
| 171 | 159 | return host | |
| 160 | + | ||
| 172 | 161 | return None | |
| 173 | 162 | ||
| 174 | 163 | ||
| 175 | 164 | @cli.command() | |
| 176 | 165 | @pass_dev | |
| 177 | 166 | async def sysinfo(dev): | |
| 178 | 167 | """Print out full system information.""" | |
| 179 | - await dev.update() | ||
| 180 | 168 | click.echo(click.style("== System info ==", bold=True)) | |
| 181 | 169 | click.echo(pf(dev.sys_info)) | |
| 170 | + return dev.sys_info | ||
| 182 | 171 | ||
| 183 | 172 | ||
| 184 | 173 | @cli.command() | |
| 185 | 174 | @pass_dev | |
| 186 | 175 | @click.pass_context | |
| 187 | 176 | async def state(ctx, dev: SmartDevice): | |
| 188 | 177 | """Print out device state and versions.""" | |
| 189 | - await dev.update() | ||
| 190 | 178 | click.echo(click.style(f"== {dev.alias} - {dev.model} ==", bold=True)) | |
| 191 | 179 | click.echo(f"\tHost: {dev.host}") | |
| 192 | 180 | click.echo( | |
@@ -234,7 +222,6 @@ async def state(ctx, dev: SmartDevice): | |||
| 234 | 222 | @click.option("--index", type=int) | |
| 235 | 223 | async def alias(dev, new_alias, index): | |
| 236 | 224 | """Get or set the device (or plug) alias.""" | |
| 237 | - await dev.update() | ||
| 238 | 225 | if index is not None: | |
| 239 | 226 | if not dev.is_strip: | |
| 240 | 227 | click.echo("Index can only used for power strips!") | |
@@ -244,8 +231,8 @@ async def alias(dev, new_alias, index): | |||
| 244 | 231 | ||
| 245 | 232 | if new_alias is not None: | |
| 246 | 233 | click.echo(f"Setting alias to {new_alias}") | |
| 247 | - click.echo(await dev.set_alias(new_alias)) | ||
| 248 | - await dev.update() | ||
| 234 | + res = await dev.set_alias(new_alias) | ||
| 235 | + return res | ||
| 249 | 236 | ||
| 250 | 237 | click.echo(f"Alias: {dev.alias}") | |
| 251 | 238 | if dev.is_strip: | |
@@ -264,9 +251,11 @@ async def raw_command(dev: SmartDevice, module, command, parameters): | |||
| 264 | 251 | ||
| 265 | 252 | if parameters is not None: | |
| 266 | 253 | parameters = ast.literal_eval(parameters) | |
| 254 | + | ||
| 267 | 255 | res = await dev._query_helper(module, command, parameters) | |
| 268 | - await dev.update() # TODO: is this needed? | ||
| 256 | + | ||
| 269 | 257 | click.echo(res) | |
| 258 | + return res | ||
| 270 | 259 | ||
| 271 | 260 | ||
| 272 | 261 | @cli.command() | |
@@ -280,7 +269,6 @@ async def emeter(dev: SmartDevice, year, month, erase): | |||
| 280 | 269 | Daily and monthly data provided in CSV format. | |
| 281 | 270 | """ | |
| 282 | 271 | click.echo(click.style("== Emeter ==", bold=True)) | |
| 283 | - await dev.update() | ||
| 284 | 272 | if not dev.has_emeter: | |
| 285 | 273 | click.echo("Device has no emeter") | |
| 286 | 274 | return | |
@@ -324,15 +312,15 @@ async def emeter(dev: SmartDevice, year, month, erase): | |||
| 324 | 312 | @pass_dev | |
| 325 | 313 | async def brightness(dev: SmartBulb, brightness: int, transition: int): | |
| 326 | 314 | """Get or set brightness.""" | |
| 327 | - await dev.update() | ||
| 328 | 315 | if not dev.is_dimmable: | |
| 329 | 316 | click.echo("This device does not support brightness.") | |
| 330 | 317 | return | |
| 318 | + | ||
| 331 | 319 | if brightness is None: | |
| 332 | 320 | click.echo(f"Brightness: {dev.brightness}") | |
| 333 | 321 | else: | |
| 334 | 322 | click.echo(f"Setting brightness to {brightness}") | |
| 335 | - click.echo(await dev.set_brightness(brightness, transition=transition)) | ||
| 323 | + return await dev.set_brightness(brightness, transition=transition) | ||
| 336 | 324 | ||
| 337 | 325 | ||
| 338 | 326 | @cli.command() | |
@@ -343,10 +331,10 @@ async def brightness(dev: SmartBulb, brightness: int, transition: int): | |||
| 343 | 331 | @pass_dev | |
| 344 | 332 | async def temperature(dev: SmartBulb, temperature: int, transition: int): | |
| 345 | 333 | """Get or set color temperature.""" | |
| 346 | - await dev.update() | ||
| 347 | 334 | if not dev.is_variable_color_temp: | |
| 348 | 335 | click.echo("Device does not support color temperature") | |
| 349 | 336 | return | |
| 337 | + | ||
| 350 | 338 | if temperature is None: | |
| 351 | 339 | click.echo(f"Color temperature: {dev.color_temp}") | |
| 352 | 340 | valid_temperature_range = dev.valid_temperature_range | |
@@ -359,7 +347,7 @@ async def temperature(dev: SmartBulb, temperature: int, transition: int): | |||
| 359 | 347 | ) | |
| 360 | 348 | else: | |
| 361 | 349 | click.echo(f"Setting color temperature to {temperature}") | |
| 362 | - await dev.set_color_temp(temperature, transition=transition) | ||
| 350 | + return await dev.set_color_temp(temperature, transition=transition) | ||
| 363 | 351 | ||
| 364 | 352 | ||
| 365 | 353 | @cli.command() | |
@@ -370,26 +358,28 @@ async def temperature(dev: SmartBulb, temperature: int, transition: int): | |||
| 370 | 358 | @click.pass_context | |
| 371 | 359 | @pass_dev | |
| 372 | 360 | async def hsv(dev, ctx, h, s, v, transition): | |
| 373 | - """Get or set color in HSV. (Bulb only).""" | ||
| 374 | - await dev.update() | ||
| 361 | + """Get or set color in HSV.""" | ||
| 362 | + if not dev.is_color: | ||
| 363 | + click.echo("Device does not support colors") | ||
| 364 | + return | ||
| 365 | + | ||
| 375 | 366 | if h is None or s is None or v is None: | |
| 376 | 367 | click.echo(f"Current HSV: {dev.hsv}") | |
| 377 | 368 | elif s is None or v is None: | |
| 378 | 369 | raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx) | |
| 379 | 370 | else: | |
| 380 | 371 | click.echo(f"Setting HSV: {h} {s} {v}") | |
| 381 | - click.echo(await dev.set_hsv(h, s, v, transition=transition)) | ||
| 372 | + return await dev.set_hsv(h, s, v, transition=transition) | ||
| 382 | 373 | ||
| 383 | 374 | ||
| 384 | 375 | @cli.command() | |
| 385 | 376 | @click.argument("state", type=bool, required=False) | |
| 386 | 377 | @pass_dev | |
| 387 | 378 | async def led(dev, state): | |
| 388 | 379 | """Get or set (Plug's) led state.""" | |
| 389 | - await dev.update() | ||
| 390 | 380 | if state is not None: | |
| 391 | 381 | click.echo(f"Turning led to {state}") | |
| 392 | - click.echo(await dev.set_led(state)) | ||
| 382 | + return await dev.set_led(state) | ||
| 393 | 383 | else: | |
| 394 | 384 | click.echo(f"LED state: {dev.led}") | |
| 395 | 385 | ||
@@ -398,7 +388,9 @@ async def led(dev, state): | |||
| 398 | 388 | @pass_dev | |
| 399 | 389 | async def time(dev): | |
| 400 | 390 | """Get the device time.""" | |
| 401 | - click.echo(await dev.get_time()) | ||
| 391 | + res = await dev.get_time() | ||
| 392 | + click.echo(f"Current time: {res}") | ||
| 393 | + return res | ||
| 402 | 394 | ||
| 403 | 395 | ||
| 404 | 396 | @cli.command() | |
@@ -408,19 +400,19 @@ async def time(dev): | |||
| 408 | 400 | @pass_dev | |
| 409 | 401 | async def on(dev: SmartDevice, index: int, name: str, transition: int): | |
| 410 | 402 | """Turn the device on.""" | |
| 411 | - await dev.update() | ||
| 412 | 403 | if index is not None or name is not None: | |
| 413 | 404 | if not dev.is_strip: | |
| 414 | 405 | click.echo("Index and name are only for power strips!") | |
| 415 | 406 | return | |
| 407 | + | ||
| 416 | 408 | dev = cast(SmartStrip, dev) | |
| 417 | 409 | if index is not None: | |
| 418 | 410 | dev = dev.get_plug_by_index(index) | |
| 419 | 411 | elif name: | |
| 420 | 412 | dev = dev.get_plug_by_name(name) | |
| 421 | 413 | ||
| 422 | 414 | click.echo(f"Turning on {dev.alias}") | |
| 423 | - await dev.turn_on(transition=transition) | ||
| 415 | + return await dev.turn_on(transition=transition) | ||
| 424 | 416 | ||
| 425 | 417 | ||
| 426 | 418 | @cli.command() | |
@@ -430,19 +422,19 @@ async def on(dev: SmartDevice, index: int, name: str, transition: int): | |||
| 430 | 422 | @pass_dev | |
| 431 | 423 | async def off(dev: SmartDevice, index: int, name: str, transition: int): | |
| 432 | 424 | """Turn the device off.""" | |
| 433 | - await dev.update() | ||
| 434 | 425 | if index is not None or name is not None: | |
| 435 | 426 | if not dev.is_strip: | |
| 436 | 427 | click.echo("Index and name are only for power strips!") | |
| 437 | 428 | return | |
| 429 | + | ||
| 438 | 430 | dev = cast(SmartStrip, dev) | |
| 439 | 431 | if index is not None: | |
| 440 | 432 | dev = dev.get_plug_by_index(index) | |
| 441 | 433 | elif name: | |
| 442 | 434 | dev = dev.get_plug_by_name(name) | |
| 443 | 435 | ||
| 444 | 436 | click.echo(f"Turning off {dev.alias}") | |
| 445 | - await dev.turn_off(transition=transition) | ||
| 437 | + return await dev.turn_off(transition=transition) | ||
| 446 | 438 | ||
| 447 | 439 | ||
| 448 | 440 | @cli.command() | |
@@ -451,7 +443,7 @@ async def off(dev: SmartDevice, index: int, name: str, transition: int): | |||
| 451 | 443 | async def reboot(plug, delay): | |
| 452 | 444 | """Reboot the device.""" | |
| 453 | 445 | click.echo("Rebooting the device..") | |
| 454 | - click.echo(await plug.reboot(delay)) | ||
| 446 | + return await plug.reboot(delay) | ||
| 455 | 447 | ||
| 456 | 448 | ||
| 457 | 449 | if __name__ == "__main__": | |
| Back | FazBrowse Home | New Git URL |
0 commit comments