Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Measurement Units to Use Constants in Mammotion Sensor #57

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion custom_components/mammotion/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from pymammotion.data.model.device import MowingDevice
from pymammotion.mammotion.devices import MammotionBaseBLEDevice
from pymammotion.proto.luba_msg import LubaMsg

Expand Down Expand Up @@ -66,7 +67,7 @@ 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:
async def _async_update_data(self) -> MowingDevice:
"""Get data from the device."""
if not (
ble_device := bluetooth.async_ble_device_from_address(
Expand Down
36 changes: 26 additions & 10 deletions custom_components/mammotion/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE
from homeassistant.const import PERCENTAGE, UnitOfLength, AREA_SQUARE_METERS, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, \
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
from pymammotion.data.model.enums import RTKStatus
from pymammotion.proto.luba_msg import ReportInfoData
from pymammotion.utility.constant.device_constant import device_mode
Expand All @@ -22,6 +25,9 @@
from .entity import MammotionBaseEntity


LENGTH_UNITS = DistanceConverter.VALID_UNITS
SPEED_UNITS = SpeedConverter.VALID_UNITS

@dataclass(frozen=True, kw_only=True)
class MammotionSensorEntityDescription(SensorEntityDescription):
"""Describes Mammotion sensor entity."""
Expand All @@ -41,14 +47,14 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
key="ble_rssi",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement="dBm",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
value_fn=lambda mower_data: mower_data.connect.ble_rssi,
),
MammotionSensorEntityDescription(
key="wifi_rssi",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement="dBm",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
value_fn=lambda mower_data: mower_data.connect.wifi_rssi,
),
MammotionSensorEntityDescription(
Expand All @@ -62,21 +68,21 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
key="blade_height",
state_class=SensorStateClass.MEASUREMENT,
device_class=None,
native_unit_of_measurement="mm",
native_unit_of_measurement=UnitOfLength.MILLIMETERS,
value_fn=lambda mower_data: mower_data.work.knife_height,
),
MammotionSensorEntityDescription(
key="area",
state_class=SensorStateClass.MEASUREMENT,
device_class=None,
native_unit_of_measurement="m²",
native_unit_of_measurement=AREA_SQUARE_METERS,
value_fn=lambda mower_data: mower_data.work.area & 65535,
),
MammotionSensorEntityDescription(
key="mowing_speed",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SPEED,
native_unit_of_measurement="m/s",
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
value_fn=lambda mower_data: mower_data.work.man_run_speed / 100,
),
MammotionSensorEntityDescription(
Expand All @@ -90,22 +96,22 @@ class MammotionSensorEntityDescription(SensorEntityDescription):
key="total_time",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement="min",
native_unit_of_measurement=UnitOfTime.MINUTES,
value_fn=lambda mower_data: mower_data.work.progress & 65535,
),
MammotionSensorEntityDescription(
key="elapsed_time",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement="min",
native_unit_of_measurement=UnitOfTime.MINUTES,
value_fn=lambda mower_data: (mower_data.work.progress & 65535)
- (mower_data.work.progress >> 16),
),
MammotionSensorEntityDescription(
key="left_time",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement="min",
native_unit_of_measurement=UnitOfTime.MINUTES,
value_fn=lambda mower_data: mower_data.work.progress >> 16,
),
MammotionSensorEntityDescription(
Expand Down Expand Up @@ -181,6 +187,16 @@ def __init__(
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.entity_description.value_fn(
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, LENGTH_UNITS[unit], UnitOfLength.FEET)
if unit in SPEED_UNITS:
return SpeedConverter.convert(current_value, SPEED_UNITS[unit], UnitOfSpeed.FEET_PER_SECOND)

return current_value