Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The lawn mower integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import final
8 
9 from propcache import cached_property
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import config_validation as cv
14 from homeassistant.helpers.entity import Entity, EntityDescription
15 from homeassistant.helpers.entity_component import EntityComponent
16 from homeassistant.helpers.typing import ConfigType
17 from homeassistant.util.hass_dict import HassKey
18 
19 from .const import (
20  DOMAIN,
21  SERVICE_DOCK,
22  SERVICE_PAUSE,
23  SERVICE_START_MOWING,
24  LawnMowerActivity,
25  LawnMowerEntityFeature,
26 )
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 DATA_COMPONENT: HassKey[EntityComponent[LawnMowerEntity]] = HassKey(DOMAIN)
31 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
32 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
33 SCAN_INTERVAL = timedelta(seconds=60)
34 
35 
36 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
37  """Set up the lawn_mower component."""
38  component = hass.data[DATA_COMPONENT] = EntityComponent[LawnMowerEntity](
39  _LOGGER, DOMAIN, hass, SCAN_INTERVAL
40  )
41  await component.async_setup(config)
42 
43  component.async_register_entity_service(
44  SERVICE_START_MOWING,
45  None,
46  "async_start_mowing",
47  [LawnMowerEntityFeature.START_MOWING],
48  )
49  component.async_register_entity_service(
50  SERVICE_PAUSE, None, "async_pause", [LawnMowerEntityFeature.PAUSE]
51  )
52  component.async_register_entity_service(
53  SERVICE_DOCK, None, "async_dock", [LawnMowerEntityFeature.DOCK]
54  )
55 
56  return True
57 
58 
59 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
60  """Set up lawn mower devices."""
61  return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
62 
63 
64 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
65  """Unload a config entry."""
66  return await hass.data[DATA_COMPONENT].async_unload_entry(entry)
67 
68 
70  """A class that describes lawn mower entities."""
71 
72 
73 CACHED_PROPERTIES_WITH_ATTR_ = {
74  "activity",
75  "supported_features",
76 }
77 
78 
79 class LawnMowerEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
80  """Base class for lawn mower entities."""
81 
82  entity_description: LawnMowerEntityEntityDescription
83  _attr_activity: LawnMowerActivity | None = None
84  _attr_supported_features: LawnMowerEntityFeature = LawnMowerEntityFeature(0)
85 
86  @final
87  @property
88  def state(self) -> str | None:
89  """Return the current state."""
90  return self.activityactivity
91 
92  @cached_property
93  def activity(self) -> LawnMowerActivity | None:
94  """Return the current lawn mower activity."""
95  return self._attr_activity
96 
97  @cached_property
98  def supported_features(self) -> LawnMowerEntityFeature:
99  """Flag lawn mower features that are supported."""
100  return self._attr_supported_features
101 
102  def start_mowing(self) -> None:
103  """Start or resume mowing."""
104  raise NotImplementedError
105 
106  async def async_start_mowing(self) -> None:
107  """Start or resume mowing."""
108  await self.hasshass.async_add_executor_job(self.start_mowingstart_mowing)
109 
110  def dock(self) -> None:
111  """Dock the mower."""
112  raise NotImplementedError
113 
114  async def async_dock(self) -> None:
115  """Dock the mower."""
116  await self.hasshass.async_add_executor_job(self.dockdock)
117 
118  def pause(self) -> None:
119  """Pause the lawn mower."""
120  raise NotImplementedError
121 
122  async def async_pause(self) -> None:
123  """Pause the lawn mower."""
124  await self.hasshass.async_add_executor_job(self.pausepause)
LawnMowerEntityFeature supported_features(self)
Definition: __init__.py:98
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:36
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:64
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:59