diff --git a/custom_components/openmower/binary_sensor.py b/custom_components/openmower/binary_sensor.py index c6fe32a..fabde30 100644 --- a/custom_components/openmower/binary_sensor.py +++ b/custom_components/openmower/binary_sensor.py @@ -8,7 +8,7 @@ BinarySensorDeviceClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_PREFIX, EntityCategory +from homeassistant.const import CONF_PREFIX from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from .entity import OpenMowerMqttEntity @@ -29,11 +29,11 @@ async def async_setup_entry( prefix = entry.data[CONF_PREFIX] async_add_entities( [ - OpenMowerMqttBinarySensorEntity( + OpenMowerEmergencySensor( "Emergency", prefix, "robot_state/json", "emergency" ), - OpenMowerMqttBinarySensorEntity( - "Is charging", prefix, "robot_state/json", "is_charging" + OpenMowerIsChargingSensor( + "Is Charging", prefix, "robot_state/json", "is_charging" ), ] ) @@ -42,3 +42,11 @@ async def async_setup_entry( class OpenMowerMqttBinarySensorEntity(OpenMowerMqttEntity, BinarySensorEntity): def _process_update(self, value): self._attr_is_on = bool(value) + + +class OpenMowerIsChargingSensor(OpenMowerMqttBinarySensorEntity): + _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING + + +class OpenMowerEmergencySensor(OpenMowerMqttBinarySensorEntity): + _attr_device_class = BinarySensorDeviceClass.PROBLEM diff --git a/custom_components/openmower/entity.py b/custom_components/openmower/entity.py index 3e2bbb8..0ec4eba 100644 --- a/custom_components/openmower/entity.py +++ b/custom_components/openmower/entity.py @@ -9,7 +9,6 @@ from homeassistant.helpers.entity import Entity from homeassistant.util import slugify, Throttle from homeassistant.util.json import json_loads_object - from .const import DOMAIN MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5) diff --git a/custom_components/openmower/sensor.py b/custom_components/openmower/sensor.py index d77d0dd..3b05a84 100644 --- a/custom_components/openmower/sensor.py +++ b/custom_components/openmower/sensor.py @@ -3,7 +3,12 @@ import logging from homeassistant.components import mqtt -from homeassistant.components.sensor import SensorEntity, SensorDeviceClass +from homeassistant.components.sensor import ( + SensorEntity, + SensorDeviceClass, + SensorStateClass, + SensorEntityDescription, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( PERCENTAGE, @@ -12,6 +17,7 @@ UnitOfTemperature, UnitOfElectricPotential, UnitOfElectricCurrent, + UnitOfLength, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -36,13 +42,13 @@ async def async_setup_entry( OpenMowerBatterySensor( "Battery", prefix, "robot_state/json", "battery_percentage" ), - OpenMowerDiagnosticSensor( + OpenMowerDisabledSensor( "Current action progress", prefix, "robot_state/json", "current_action_progress", ), - OpenMowerDiagnosticSensor( + OpenMowerGpsPercentageSensor( "GPS Percentage", prefix, "robot_state/json", "gps_percentage" ), OpenMowerMqttSensorEntity( @@ -51,7 +57,7 @@ async def async_setup_entry( OpenMowerCurrentSensor( "Charge Current", prefix, "sensors/om_charge_current/data", None ), - OpenMowerRawDiagnosticSensor( + OpenMowerGpsAccuracySensor( "GPS Accuracy", prefix, "sensors/om_gps_accuracy/data", None ), OpenMowerTemperatureSensor( @@ -87,33 +93,60 @@ def _process_update(self, value): class OpenMowerBatterySensor(OpenMowerMqttSensorEntity): _attr_device_class = SensorDeviceClass.BATTERY _attr_native_unit_of_measurement = PERCENTAGE + _attr_state_class = SensorStateClass.MEASUREMENT def _process_update(self, value): self._attr_native_value = int(float(value) * 100) -class OpenMowerDiagnosticSensor(OpenMowerMqttSensorEntity): - _attr_entity_category = EntityCategory.DIAGNOSTIC +class OpenMowerGpsPercentageSensor(OpenMowerMqttSensorEntity): + _attr_native_unit_of_measurement = PERCENTAGE + _attr_state_class = SensorStateClass.MEASUREMENT + + def _process_update(self, value): + self._attr_native_value = int(float(value) * 100) -class OpenMowerRawDiagnosticSensor(OpenMowerDiagnosticSensor): +class OpenMowerDisabledSensor(OpenMowerMqttSensorEntity): + entity_description = SensorEntityDescription( + key="currentStateProgress", + entity_registry_enabled_default=False, + entity_category=EntityCategory.DIAGNOSTIC, + ) + + +class OpenMowerRawDiagnosticSensor(OpenMowerMqttSensorEntity): + _attr_entity_category = EntityCategory.DIAGNOSTIC + _attr_state_class = SensorStateClass.MEASUREMENT + def _process_update(self, value): self._attr_native_value = float(value) class OpenMowerCurrentSensor(OpenMowerRawDiagnosticSensor): _attr_device_class = SensorDeviceClass.CURRENT - _attr_unit_of_measurement = UnitOfElectricCurrent.AMPERE + _attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE _attr_suggested_display_precision = 1 class OpenMowerTemperatureSensor(OpenMowerRawDiagnosticSensor): _attr_device_class = SensorDeviceClass.TEMPERATURE - _attr_unit_of_measurement = UnitOfTemperature.CELSIUS + _attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS _attr_suggested_display_precision = 0 class OpenMowerVoltageSensor(OpenMowerRawDiagnosticSensor): _attr_device_class = SensorDeviceClass.VOLTAGE - _attr_unit_of_measurement = UnitOfElectricPotential.VOLT + _attr_native_unit_of_measurement = UnitOfElectricPotential.VOLT _attr_suggested_display_precision = 1 + + +class OpenMowerGpsAccuracySensor(OpenMowerRawDiagnosticSensor): + _attr_device_class = SensorDeviceClass.DISTANCE + _attr_native_unit_of_measurement = UnitOfLength.METERS + _attr_suggested_display_precision = 4 + + def _process_update(self, value): + super()._process_update(value) + if self._attr_native_value == 999: + self._attr_native_value = None