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. SERVICE_DOCK,
  7. SERVICE_PAUSE,
  8. SERVICE_START_MOWING,
  9. LawnMowerActivity,
  10. LawnMowerEntityFeature,
  11. )
  12. from .device import TuyaLocalDevice
  13. from .entity import TuyaLocalEntity
  14. from .helpers.config import async_tuya_setup_platform
  15. from .helpers.device_config import TuyaEntityConfig
  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. if self._command_dp:
  40. available_commands = self._command_dp.values(self._device)
  41. if SERVICE_START_MOWING in available_commands:
  42. self._attr_supported_features |= LawnMowerEntityFeature.START_MOWING
  43. if SERVICE_PAUSE in available_commands:
  44. self._attr_supported_features |= LawnMowerEntityFeature.PAUSE
  45. if SERVICE_DOCK in available_commands:
  46. self._attr_supported_features |= LawnMowerEntityFeature.DOCK
  47. @property
  48. def activity(self) -> LawnMowerActivity | None:
  49. """Return the status of the lawn mower."""
  50. return LawnMowerActivity(self._activity_dp.get_value(self._device))
  51. async def async_start_mowing(self) -> None:
  52. """Start mowing the lawn."""
  53. if self._command_dp:
  54. await self._command_dp.async_set_value(self._device, SERVICE_START_MOWING)
  55. async def async_pause(self):
  56. """Pause lawn mowing."""
  57. if self._command_dp:
  58. await self._command_dp.async_set_value(self._device, SERVICE_PAUSE)
  59. async def async_dock(self):
  60. """Stop mowing and return to dock."""
  61. if self._command_dp:
  62. await self._command_dp.async_set_value(self._device, SERVICE_DOCK)