Skip to content

Commit

Permalink
feat: fix accuracy, units and display, hide action progress
Browse files Browse the repository at this point in the history
  • Loading branch information
vermut committed May 23, 2024
1 parent 1ff29fc commit 64f1a6c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
16 changes: 12 additions & 4 deletions custom_components/openmower/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
),
]
)
Expand All @@ -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
1 change: 0 additions & 1 deletion custom_components/openmower/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 43 additions & 10 deletions custom_components/openmower/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +17,7 @@
UnitOfTemperature,
UnitOfElectricPotential,
UnitOfElectricCurrent,
UnitOfLength,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit 64f1a6c

Please sign in to comment.