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