lawn_mower.py 2.8 KB

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