Skip to content

Commit

Permalink
Fix us customary properly and small bug in coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey0000 committed Aug 13, 2024
1 parent d49bc4d commit ba10c17
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
5 changes: 3 additions & 2 deletions custom_components/mammotion/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pymammotion.data.model.device import MowingDevice
from pymammotion.mammotion.devices import MammotionBaseBLEDevice
from pymammotion.proto.luba_msg import LubaMsg
from pymammotion.proto.mctrl_sys import RptInfoType, RptAct
from pymammotion.proto.mctrl_sys import RptAct, RptInfoType
from pymammotion.utility.constant import WorkMode

from .const import COMMAND_EXCEPTIONS, DOMAIN, LOGGER
Expand Down Expand Up @@ -108,7 +108,7 @@ async def _async_update_data(self) -> MowingDevice:
self.device.update_device(ble_device)
try:
await self.async_request_iot_sync()
if self.device.luba_msg.device.sys.toapp_report_data.dev.sys_status != WorkMode.MODE_WORKING:
if self.device.luba_msg.sys.toapp_report_data.dev.sys_status != WorkMode.MODE_WORKING:
await self.async_send_command("get_report_cfg")
except COMMAND_EXCEPTIONS as exc:
self.update_failures += 1
Expand All @@ -122,3 +122,4 @@ async def _async_update_data(self) -> MowingDevice:

self.update_failures = 0
return self.device.luba_msg

2 changes: 1 addition & 1 deletion 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.20",
"version": "0.0.19",
"integration_type": "device",
"bluetooth": [
{
Expand Down
14 changes: 9 additions & 5 deletions custom_components/mammotion/number.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from typing import Callable, Awaitable

from dataclasses import dataclass
from homeassistant.components.number import NumberEntity, NumberEntityDescription, NumberMode
from typing import Awaitable, Callable

from homeassistant.components.number import (
NumberEntity,
NumberEntityDescription,
NumberMode,
)
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory

from homeassistant.helpers.entity_platform import AddEntitiesCallback
from pymammotion.data.model.device_config import DeviceLimits

from . import MammotionConfigEntry
from .coordinator import MammotionDataUpdateCoordinator

from .entity import MammotionBaseEntity


@dataclass(frozen=True, kw_only=True)
class MammotionNumberEntityDescription(NumberEntityDescription):
"""Describes Mammotion number entity."""

set_fn: Callable[[MammotionDataUpdateCoordinator, float], Awaitable[None]]


Expand Down Expand Up @@ -102,6 +105,7 @@ async def async_set_native_value(self, value: float) -> None:

class MammotionWorkingNumberEntity(MammotionNumberEntity):
"""Mammotion working number entity."""

entity_description: MammotionNumberEntityDescription
_attr_has_entity_name = True

Expand Down
9 changes: 8 additions & 1 deletion custom_components/mammotion/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from pymammotion.data.model.mowing_modes import CuttingMode, BorderPatrolMode, ObstacleLapsMode, MowOrder
from pymammotion.data.model.mowing_modes import (
BorderPatrolMode,
CuttingMode,
MowOrder,
ObstacleLapsMode,
)

from . import MammotionConfigEntry
from .coordinator import MammotionDataUpdateCoordinator
Expand All @@ -15,6 +20,7 @@
@dataclass(frozen=True, kw_only=True)
class MammotionSelectEntityDescription(SelectEntityDescription):
"""Describes Mammotion select entity."""

key: str
options: list[str]
select_fn: Callable[[MammotionDataUpdateCoordinator, str], Awaitable[None]]
Expand Down Expand Up @@ -66,6 +72,7 @@ async def async_setup_entry(
# Define the select entity class with entity_category: config
class MammotionSelectEntity(MammotionBaseEntity, SelectEntity):
"""Representation of a Mammotion select entities."""

_attr_entity_category = EntityCategory.CONFIG

entity_description: MammotionSelectEntityDescription
Expand Down
28 changes: 12 additions & 16 deletions custom_components/mammotion/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfLength, AREA_SQUARE_METERS, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, \
UnitOfSpeed, UnitOfTime
from homeassistant.const import (
AREA_SQUARE_METERS,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfLength,
UnitOfSpeed,
UnitOfTime,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.unit_conversion import DistanceConverter, SpeedConverter
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM, LENGTH_UNITS
from homeassistant.util.unit_conversion import SpeedConverter
from pymammotion.data.model.enums import RTKStatus
from pymammotion.proto.luba_msg import ReportInfoData
from pymammotion.utility.constant.device_constant import device_mode, PosType
from pymammotion.utility.constant.device_constant import PosType, device_mode
from pymammotion.utility.device_type import DeviceType

from . import MammotionConfigEntry
Expand All @@ -37,7 +42,7 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
MammotionSensorEntityDescription(
key="blade_height",
state_class=SensorStateClass.MEASUREMENT,
device_class=None,
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement=UnitOfLength.MILLIMETERS,
value_fn=lambda mower_data: mower_data.work.knife_height,
),
Expand Down Expand Up @@ -163,7 +168,7 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
# - WiFi status
# - Side LED
# - Possibly more I forgot about
# 'real_pos_x': -142511, 'real_pos_y': -20548, 'real_toward': 50915, 'pos_type': 3 PosType(IntEnm) (robot position)
# 'real_pos_x': -142511, 'real_pos_y': -20548, 'real_toward': 50915, (robot position)
)


Expand Down Expand Up @@ -207,13 +212,4 @@ def native_value(self) -> StateType:
current_value = self.entity_description.value_fn(
self.coordinator.data.sys.toapp_report_data
)
unit = self.entity_description.native_unit_of_measurement
unit_system = self.hass.config.units

if unit_system is US_CUSTOMARY_SYSTEM:
if unit in LENGTH_UNITS:
return DistanceConverter.convert(current_value, UnitOfLength[unit], UnitOfLength.FEET)
if unit in SPEED_UNITS:
return SpeedConverter.convert(current_value, UnitOfSpeed[unit], UnitOfSpeed.FEET_PER_SECOND)

return current_value
7 changes: 4 additions & 3 deletions custom_components/mammotion/switch.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from dataclasses import dataclass
from typing import Awaitable, Callable

from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from typing import Callable, Awaitable

from . import MammotionConfigEntry
from .coordinator import MammotionDataUpdateCoordinator

from .entity import MammotionBaseEntity


@dataclass(frozen=True, kw_only=True)
class MammotionSwitchEntityDescription(SwitchEntityDescription):
"""Describes Mammotion switch entity."""

key: str
set_fn: Callable[[MammotionDataUpdateCoordinator, bool], Awaitable[None]]

Expand Down Expand Up @@ -95,4 +96,4 @@ async def async_turn_off(self, **kwargs) -> None:

async def async_update(self) -> None:
"""Update the entity state."""
pass
pass

0 comments on commit ba10c17

Please sign in to comment.