Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #442 from Thomas55555/set-battery-level-to-unavail…
Browse files Browse the repository at this point in the history
…able-to-avoid-wrong-zeros

set bat_level to unavailable to avoid wrong zeros
  • Loading branch information
Thomas55555 authored May 7, 2023
2 parents e5cef00 + 20e78ac commit d5b20f0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion custom_components/husqvarna_automower/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AutomowerSensorRequiredKeysMixin:
"""Mixin for required keys."""

value_fn: Callable[[AutomowerEntity], int]
available_fn: Callable[[AutomowerEntity], bool]


@dataclass
Expand Down Expand Up @@ -94,6 +95,7 @@ def problem_list() -> list:
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.SECONDS,
value_fn=lambda data: data["statistics"]["cuttingBladeUsageTime"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalChargingTime",
Expand All @@ -105,6 +107,7 @@ def problem_list() -> list:
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.SECONDS,
value_fn=lambda data: data["statistics"]["totalChargingTime"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalCuttingTime",
Expand All @@ -116,6 +119,7 @@ def problem_list() -> list:
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.SECONDS,
value_fn=lambda data: data["statistics"]["totalCuttingTime"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalRunningTime",
Expand All @@ -127,6 +131,7 @@ def problem_list() -> list:
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.SECONDS,
value_fn=lambda data: data["statistics"]["totalRunningTime"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalSearchingTime",
Expand All @@ -138,6 +143,7 @@ def problem_list() -> list:
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.SECONDS,
value_fn=lambda data: data["statistics"]["totalSearchingTime"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="numberOfChargingCycles",
Expand All @@ -147,6 +153,7 @@ def problem_list() -> list:
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data["statistics"]["numberOfChargingCycles"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="numberOfCollisions",
Expand All @@ -156,6 +163,7 @@ def problem_list() -> list:
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data["statistics"]["numberOfCollisions"],
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalSearchingTime_percentage",
Expand All @@ -169,6 +177,7 @@ def problem_list() -> list:
value_fn=lambda data: data["statistics"]["totalSearchingTime"]
/ data["statistics"]["totalRunningTime"]
* 100,
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="totalCuttingTime_percentage",
Expand All @@ -182,16 +191,20 @@ def problem_list() -> list:
value_fn=lambda data: data["statistics"]["totalCuttingTime"]
/ data["statistics"]["totalRunningTime"]
* 100,
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="battery_level",
name="Battery level",
entity_registry_enabled_default=True,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: data["battery"]["batteryPercent"],
available_fn=lambda data: False
if (data["battery"]["batteryPercent"] is 0)
and (data["metadata"]["connected"] is False)
else True,
),
AutomowerSensorEntityDescription(
key="next_start",
Expand All @@ -201,6 +214,7 @@ def problem_list() -> list:
value_fn=lambda data: AutomowerEntity.datetime_object(
data, data["planner"]["nextStartTimestamp"]
),
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="mode",
Expand All @@ -210,6 +224,7 @@ def problem_list() -> list:
options=["main_area", "secondary_area", "home", "demo", "unknown"],
translation_key="mode_list",
value_fn=lambda data: data["mower"]["mode"].lower(),
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="problem_sensor",
Expand All @@ -221,6 +236,7 @@ def problem_list() -> list:
value_fn=lambda data: None
if get_problem(data) is None
else get_problem(data).lower(),
available_fn=lambda data: True,
),
AutomowerSensorEntityDescription(
key="cuttingHeight",
Expand All @@ -229,6 +245,7 @@ def problem_list() -> list:
icon="mdi:grass",
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["cuttingHeight"],
available_fn=lambda data: True,
),
)

Expand Down Expand Up @@ -273,3 +290,9 @@ def native_value(self):
"""Return the state of the sensor."""
mower_attributes = AutomowerEntity.get_mower_attributes(self)
return self.entity_description.value_fn(mower_attributes)

@property
def available(self):
"""Return the availability of the sensor."""
mower_attributes = AutomowerEntity.get_mower_attributes(self)
return self.entity_description.available_fn(mower_attributes)

0 comments on commit d5b20f0

Please sign in to comment.