/docs/development/power_management.md

https://github.com/postlund/pyatv · Markdown · 53 lines · 39 code · 14 blank · 0 comment · 0 complexity · 3635dda5e474d49992ae67408c98e43c MD5 · raw file

  1. ---
  2. layout: template
  3. title: Power Management
  4. permalink: /development/power_management/
  5. link_group: development
  6. ---
  7. # Power Management
  8. Power management of a device is done with the power interface,
  9. {% include api i="interface.Power" %}. It allows you to turn on, turn off and get current
  10. power state. This interface is currently only supported by devices running tvOS.
  11. ## Using the Power Management API
  12. After connecting to a device, you get the power management via {% include api i="interface.AppleTV.power" %}:
  13. ```python
  14. atv = await pyatv.connect(config, ...)
  15. pwrc = atv.power
  16. ```
  17. You can then control via the available functions:
  18. ```python
  19. await pwrc.turn_on()
  20. await pwrc.turn_off()
  21. ```
  22. To get current power state use following property:
  23. ```python
  24. @property
  25. def power_state(self) -> const.PowerState:
  26. ```
  27. ## Waiting for State Change
  28. It is possible to pass `await_new_state` set to `True` when turning on
  29. or off a device to have `pyatv` wait for a state change. E.g. calling
  30. {% include api i="interface.Power.turn_off" %} will block until the device
  31. has powered off:
  32. ```python
  33. await pwrc.turn_off(await_new_state=True)
  34. ```
  35. If the device is already off, it will return immedietly.
  36. To not block indefinitely, use `wait_for` with a timeout:
  37. ```python
  38. await asyncio.wait_for(pwrc.turn_off(await_new_state=True), timeout=5)
  39. ```