Skip to content

Commit

Permalink
Merge pull request #52 from mikey0000/maps-and-paths
Browse files Browse the repository at this point in the history
update paths to renamed file, bump version
  • Loading branch information
mikey0000 authored Aug 7, 2024
2 parents 32ce1a7 + 5063e2a commit fb6ac68
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 531 deletions.
1 change: 1 addition & 0 deletions custom_components/mammotion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Platform.BINARY_SENSOR,
Platform.LAWN_MOWER,
Platform.SENSOR,
Platform.BUTTON,
]

type MammotionConfigEntry = ConfigEntry[MammotionDataUpdateCoordinator]
Expand Down
63 changes: 63 additions & 0 deletions custom_components/mammotion/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Mammotion button sensor entities."""

from collections.abc import Callable
from dataclasses import dataclass
from typing import Awaitable

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import MammotionConfigEntry
from .coordinator import MammotionDataUpdateCoordinator
from .entity import MammotionBaseEntity


@dataclass(frozen=True, kw_only=True)
class MammotionButtonSensorEntityDescription(ButtonEntityDescription):
"""Describes Mammotion button sensor entity."""

press_fn: Callable[[MammotionDataUpdateCoordinator], Awaitable[None]]


BUTTON_SENSORS: tuple[MammotionButtonSensorEntityDescription, ...] = (
MammotionButtonSensorEntityDescription(
key="start_map_sync",
press_fn=lambda coordinator: coordinator.async_sync_maps(),
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: MammotionConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Mammotion button sensor entity."""
coordinator = entry.runtime_data

async_add_entities(
MammotionButtonSensorEntity(coordinator, entity_description)
for entity_description in BUTTON_SENSORS
)


class MammotionButtonSensorEntity(MammotionBaseEntity, ButtonEntity):
"""Mammotion button sensor entity."""

entity_description: MammotionButtonSensorEntityDescription
_attr_has_entity_name = True

def __init__(
self,
coordinator: MammotionDataUpdateCoordinator,
entity_description: MammotionButtonSensorEntityDescription,
) -> None:
"""Initialize the button sensor entity."""
super().__init__(coordinator, entity_description.key)
self.entity_description = entity_description
self._attr_translation_key = entity_description.key

async def async_press(self) -> None:
"""Handle the button press."""
await self.entity_description.press_fn(self.coordinator)
2 changes: 1 addition & 1 deletion custom_components/mammotion/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Final

from bleak_retry_connector import BleakError, BleakNotFoundError
from pymammotion.mammotion.devices.luba import CharacteristicMissingError
from pymammotion.mammotion.devices.mammotion import CharacteristicMissingError

DOMAIN: Final = "mammotion"

Expand Down
5 changes: 5 additions & 0 deletions custom_components/mammotion/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ async def async_setup(self) -> None:
except COMMAND_EXCEPTIONS as exc:
raise ConfigEntryNotReady("Unable to setup Mammotion device") from exc


async def async_sync_maps(self) -> None:
"""Get map data from the device."""
await self.device.start_map_sync()

async def _async_update_data(self) -> LubaMsg:
"""Get data from the device."""
if not (
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mammotion/lawn_mower.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from pymammotion.mammotion.devices.luba import has_field
from pymammotion.mammotion.devices.mammotion import has_field
from pymammotion.proto.luba_msg import RptDevStatus
from pymammotion.utility.constant.device_constant import WorkMode

Expand Down
4 changes: 2 additions & 2 deletions custom_components/mammotion/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "mammotion",
"name": "Mammotion",
"version": "0.0.5",
"version": "0.0.7",
"integration_type": "device",
"bluetooth": [
{
Expand Down Expand Up @@ -29,6 +29,6 @@
],
"iot_class": "local_push",
"requirements": [
"pymammotion==0.0.40"
"pymammotion==0.0.42"
]
}
7 changes: 7 additions & 0 deletions custom_components/mammotion/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from homeassistant.helpers.typing import StateType
from pymammotion.data.model.enums import RTKStatus
from pymammotion.proto.luba_msg import ReportInfoData
from pymammotion.utility.constant.device_constant import device_mode

from . import MammotionConfigEntry
from .coordinator import MammotionDataUpdateCoordinator
Expand Down Expand Up @@ -121,6 +122,12 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
native_unit_of_measurement=None,
value_fn=lambda mower_data: (mower_data.rtk.co_view_stars >> 8) & 255,
),
MammotionSensorEntityDescription(
key="activity_mode",
state_class=None,
device_class=SensorDeviceClass.ENUM,
value_fn=lambda mower_data: device_mode(mower_data.dev.sys_status),
),
MammotionSensorEntityDescription(
key="position_mode",
state_class=None,
Expand Down
8 changes: 8 additions & 0 deletions custom_components/mammotion/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
},
"position_mode": {
"name": "RTK position"
},
"activity_mode": {
"name": "Activity mode"
}
},
"button": {
"start_map_sync": {
"name": "Sync maps"
}
}
},
Expand Down
Loading

0 comments on commit fb6ac68

Please sign in to comment.