Skip to content

Commit

Permalink
add traversal mode and try to fix issue with mower being ready but st…
Browse files Browse the repository at this point in the history
…ill doing a job and resuming
  • Loading branch information
mikey0000 committed Nov 8, 2024
1 parent d647279 commit c0520a4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
4 changes: 4 additions & 0 deletions custom_components/mammotion/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ async def async_read_sidelight(self) -> None:
"read_and_set_sidelight", is_sidelight=False, operate=1
)

async def set_traversal_mode(self, id: int) -> None:
"""Set traversal mode."""
await self.async_send_command("traverse_mode", id=id)

async def async_blade_height(self, height: int) -> int:
"""Set blade height."""
await self.send_command_and_update("set_blade_height", height=float(height))
Expand Down
3 changes: 3 additions & 0 deletions custom_components/mammotion/lawn_mower.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ async def async_start_mowing(self, **kwargs: Any) -> None:
await self.coordinator.async_send_command("resume_execute_task")
if mode == WorkMode.MODE_READY:
trans_key = "start_failed"
if self.report_data.work.area >> 16 != 0:
await self.coordinator.async_send_command("start_job")
return
if await self.coordinator.async_plan_route(operational_settings):
await self.coordinator.async_send_command("start_job")

Expand Down
2 changes: 1 addition & 1 deletion custom_components/mammotion/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
],
"iot_class": "local_push",
"requirements": [
"pymammotion==0.2.98"
"pymammotion==0.3.0"
]
}
57 changes: 56 additions & 1 deletion custom_components/mammotion/select.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Callable
from typing import Awaitable, Callable

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
Expand All @@ -13,6 +13,7 @@
MowOrder,
ObstacleLapsMode,
PathAngleSetting,
TraversalMode,
)
from pymammotion.utility.device_type import DeviceType

Expand All @@ -25,11 +26,31 @@
class MammotionConfigSelectEntityDescription(SelectEntityDescription):
"""Describes Mammotion select entity."""

key: str
options: list[str]
set_fn: Callable[[MammotionDataUpdateCoordinator, str], Awaitable[None]]


@dataclass(frozen=True, kw_only=True)
class MammotionAsyncConfigSelectEntityDescription(MammotionBaseEntity, SelectEntity):
"""Describes Mammotion select entity with async functionality."""

key: str
options: list[str]
set_fn: Callable[[MammotionDataUpdateCoordinator, str], None]


ASYNC_SELECT_ENTITIES: tuple[MammotionAsyncConfigSelectEntityDescription, ...] = (
MammotionAsyncConfigSelectEntityDescription(
key="traversal_mode",
options=[mode.name for mode in TraversalMode],
set_fn=lambda coordinator, value: coordinator.set_traversal_mode(
TraversalMode[value]
),
),
)


SELECT_ENTITIES: tuple[MammotionConfigSelectEntityDescription, ...] = (
MammotionConfigSelectEntityDescription(
key="channel_mode",
Expand Down Expand Up @@ -117,6 +138,11 @@ async def async_setup_entry(
for entity_description in SELECT_ENTITIES:
entities.append(MammotionConfigSelectEntity(coordinator, entity_description))

for entity_description in ASYNC_SELECT_ENTITIES:
entities.append(
MammotionAsyncConfigSelectEntity(coordinator, entity_description)
)

if DeviceType.is_luba1(coordinator.device_name):
for entity_description in LUBA1_SELECT_ENTITIES:
entities.append(
Expand Down Expand Up @@ -156,3 +182,32 @@ async def async_select_option(self, option: str) -> None:
self._attr_current_option = option
self.entity_description.set_fn(self.coordinator, option)
self.async_write_ha_state()


# Define the select entity class with entity_category: config
class MammotionAsyncConfigSelectEntity(
MammotionBaseEntity, SelectEntity, RestoreEntity
):
"""Representation of a Mammotion select entities."""

_attr_entity_category = EntityCategory.CONFIG

entity_description: MammotionConfigSelectEntityDescription
_attr_has_entity_name = True

def __init__(
self,
coordinator: MammotionDataUpdateCoordinator,
entity_description: MammotionConfigSelectEntityDescription,
) -> None:
super().__init__(coordinator, entity_description.key)
self.coordinator = coordinator
self.entity_description = entity_description
self._attr_translation_key = entity_description.key
self._attr_options = entity_description.options
self._attr_current_option = entity_description.options[0]

async def async_select_option(self, option: str) -> None:
self._attr_current_option = option
await self.entity_description.set_fn(self.coordinator, option)
self.async_write_ha_state()
11 changes: 9 additions & 2 deletions custom_components/mammotion/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
value_fn=lambda mower_data: (mower_data.report_data.rtk.co_view_stars >> 8)
& 255,
),
# MammotionSensorEntityDescription(
# key="vlsam_status",
# state_class=SensorStateClass.MEASUREMENT,
# device_class=None,
# native_unit_of_measurement=None,
# value_fn=lambda mower_data: (mower_data.report_data.dev.vslam_status & 65280) >> 8,
# ),
MammotionSensorEntityDescription(
key="activity_mode",
state_class=None,
Expand All @@ -184,7 +191,7 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
native_unit_of_measurement=None,
value_fn=lambda mower_data: str(
RTKStatus.from_value(mower_data.report_data.rtk.status)
), # Note: This will not work for Luba2 & Yuka. Only for Luba1
),
),
MammotionSensorEntityDescription(
key="position_type",
Expand All @@ -193,7 +200,7 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
native_unit_of_measurement=None,
value_fn=lambda mower_data: str(
PosType(mower_data.location.position_type).name
), # Note: This will not work for Luba2 & Yuka. Only for Luba1
),
),
MammotionSensorEntityDescription(
key="work_area",
Expand Down
7 changes: 7 additions & 0 deletions custom_components/mammotion/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@
"absolute_angle": "Absolute angle",
"random_angle": "Random angle"
}
},
"traversal_mode": {
"name": "Traversal Mode",
"state": {
"direct": "Direct",
"follow_perimeter ": "Follow the perimeter"
}
}
},
"number": {
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "~3.12.0"
pymammotion = "0.2.98"
pymammotion = "0.3.0"
homeassistant = "^2024.9.0"
autotyping = "^24.3.0"

Expand Down

0 comments on commit c0520a4

Please sign in to comment.