diff --git a/custom_components/mammotion/coordinator.py b/custom_components/mammotion/coordinator.py index 6227eca..f24c2ce 100644 --- a/custom_components/mammotion/coordinator.py +++ b/custom_components/mammotion/coordinator.py @@ -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 @@ -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( diff --git a/custom_components/mammotion/sensor.py b/custom_components/mammotion/sensor.py index cec43ba..906103e 100644 --- a/custom_components/mammotion/sensor.py +++ b/custom_components/mammotion/sensor.py @@ -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 @@ -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.""" @@ -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( @@ -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( @@ -90,14 +96,14 @@ 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), ), @@ -105,7 +111,7 @@ class MammotionSensorEntityDescription(SensorEntityDescription): 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( @@ -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