lawn_mower.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. Setup for different kinds of Tuya lawn mowers
  3. """
  4. from homeassistant.components.lawn_mower import LawnMowerEntity
  5. from homeassistant.components.lawn_mower.const import (
  6. LawnMowerActivity,
  7. LawnMowerEntityFeature,
  8. SERVICE_DOCK,
  9. SERVICE_PAUSE,
  10. SERVICE_START_MOWING,
  11. )
  12. from .device import TuyaLocalDevice
  13. from .helpers.config import async_tuya_setup_platform
  14. from .helpers.device_config import TuyaEntityConfig
  15. from .helpers.mixin import TuyaLocalEntity
  16. async def async_setup_entry(hass, config_entry, async_add_entities):
  17. config = {**config_entry.data, **config_entry.options}
  18. await async_tuya_setup_platform(
  19. hass,
  20. async_add_entities,
  21. config,
  22. "lawn_mower",
  23. TuyaLocalLawnMower,
  24. )
  25. class TuyaLocalLawnMower(TuyaLocalEntity, LawnMowerEntity):
  26. """Representation of a Tuya Lawn Mower"""
  27. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  28. """
  29. Initialise the lawn mower.
  30. Args:
  31. device (TuyaLocalDevice): the device API instance.
  32. config (TuyaEntityConfig): the configuration for this entity
  33. """
  34. super().__init__()
  35. dps_map = self._init_begin(device, config)
  36. self._activity_dp = dps_map.pop("activity", None)
  37. self._command_dp = dps_map.pop("command", None)
  38. self._init_end(dps_map)
  39. self._attr_supported_features = 0
  40. if self._command_dp:
  41. available_commands = self._command_dp.values(self._device)
  42. if SERVICE_START_MOWING in available_commands:
  43. self._attr_supported_features |= LawnMowerEntityFeature.START_MOWING
  44. if SERVICE_PAUSE in available_commands:
  45. self._attr_supported_features |= LawnMowerEntityFeature.PAUSE
  46. if SERVICE_DOCK in available_commands:
  47. self._attr_supported_features |= LawnMowerEntityFeature.DOCK
  48. @property
  49. def activity(self) -> LawnMowerActivity | None:
  50. """Return the status of the lawn mower."""
  51. return LawnMowerActivity(self._activity_dp.get_value(self._device))
  52. async def async_start_mowing(self) -> None:
  53. """Start mowing the lawn."""
  54. if self._command_dp:
  55. await self._command_dp.async_set_value(self._device, SERVICE_START_MOWING)
  56. async def async_pause(self):
  57. """Pause lawn mowing."""
  58. if self._command_dp:
  59. await self._command_dp.async_set_value(self._device, SERVICE_PAUSE)
  60. async def async_dock(self):
  61. """Stop mowing and return to dock."""
  62. if self._command_dp:
  63. await self._command_dp.async_set_value(self._device, SERVICE_DOCK)