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

Add more battery sensors #353

Merged
merged 5 commits into from
Jan 21, 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
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,22 @@ depending on your preference.

### Battery

| Entity name | Unit | Description |
| ------------------------------ | ---- | ----------------------------------------------------------------------------- |
| Battery Power | W | the instantaneous power consumed from (`> 0`) or fed into (`< 0`) the battery |
| Battery Current | A | the instantaneous current flowing from or to the battery |
| Battery Voltage | V | the instantaneous voltage of the battery |
| Battery Temperature | °C | the instantaneous temperature of the battery |
| Battery Cycles | | the recorded full charge/discharge cycles of the battery |
| Battery State of Charge | % | the instantaneous state of charge of the battery |
| Battery State of Charge Target | % | the state of charge of the battery aimed for by the system |
| Battery State of Health | % | the estimated state of health of the battery |
| Battery Stored Energy | Wh | the cumulative energy fed into the battery |
| Battery Used Energy | Wh | the cumulative energy consumed from the battery |
| Battery Status | | the current battery status (incomplete) |
| Entity name | Unit | Description |
| ------------------------------- | ---- | ----------------------------------------------------------------------------- |
| Battery Power | W | the instantaneous power consumed from (`> 0`) or fed into (`< 0`) the battery |
| Battery Current | A | the instantaneous current flowing from or to the battery |
| Battery Voltage | V | the instantaneous voltage of the battery |
| Battery Temperature | °C | the instantaneous temperature of the battery |
| Battery Cycles | | the recorded full charge/discharge cycles of the battery |
| Battery State of Charge | % | the instantaneous state of charge of the battery |
| Battery State of Charge Target | % | the state of charge of the battery aimed for by the system |
| Battery Minimum State of Charge | % | the minimum state of charge of the battery to maintain as an island backup |
| Battery Maximum State of Charge | % | the maximum state of charge of the battery to aim for to improve battery life |
| Battery State of Health | % | the estimated state of health of the battery |
| Battery Stored Energy | Wh | the cumulative energy fed into the battery |
| Battery Used Energy | Wh | the cumulative energy consumed from the battery |
| Battery Status | | the current battery status (incomplete) |
| Next Battery Calibration Date | | the date and time of the next planned battery calibration |

### Household consumers and producers

Expand Down
23 changes: 23 additions & 0 deletions custom_components/rct_power/lib/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .state_helpers import available_battery_status
from .state_helpers import get_first_api_reponse_value_as_absolute_state
from .state_helpers import get_first_api_response_value_as_battery_status
from .state_helpers import get_first_api_response_value_as_timestamp
from .state_helpers import sum_api_response_values_as_state


Expand Down Expand Up @@ -158,6 +159,20 @@ def get_matching_names(expression: str):
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.BATTERY,
),
RctPowerSensorEntityDescription(
get_device_info=get_battery_device_info,
key="power_mng.soc_min_island",
name="Battery Minimum State of Charge",
update_priority=EntityUpdatePriority.FREQUENT,
state_class=SensorStateClass.MEASUREMENT,
),
RctPowerSensorEntityDescription(
get_device_info=get_battery_device_info,
key="power_mng.soc_max",
name="Battery Maximum State of Charge",
update_priority=EntityUpdatePriority.FREQUENT,
state_class=SensorStateClass.MEASUREMENT,
),
RctPowerSensorEntityDescription(
get_device_info=get_battery_device_info,
key="battery.soc_target",
Expand Down Expand Up @@ -193,6 +208,14 @@ def get_matching_names(expression: str):
update_priority=EntityUpdatePriority.INFREQUENT,
state_class=SensorStateClass.TOTAL_INCREASING,
),
RctPowerSensorEntityDescription(
get_device_info=get_battery_device_info,
key="power_mng.bat_next_calib_date",
name="Next Battery Calibration Date",
update_priority=EntityUpdatePriority.INFREQUENT,
device_class=SensorDeviceClass.TIMESTAMP,
get_native_value=get_first_api_response_value_as_timestamp,
),
]

inverter_sensor_entity_descriptions: List[RctPowerSensorEntityDescription] = [
Expand Down
25 changes: 25 additions & 0 deletions custom_components/rct_power/lib/state_helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime
from typing import get_args
from typing import Literal
from typing import Optional

from homeassistant.components.sensor import SensorEntity
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import as_local

from .api import ApiResponseValue
from .const import BatteryStatusFlag
Expand Down Expand Up @@ -122,3 +124,26 @@ def get_api_response_values_as_bitfield(
values: list[Optional[ApiResponseValue]],
) -> StateType:
return "".join(f"{value:b}" for value in values if isinstance(value, int))


#
# Timestamp
#
def get_first_api_response_value_as_timestamp(
entity: SensorEntity,
values: list[Optional[ApiResponseValue]],
) -> StateType:
if len(values) <= 0:
return None

return get_api_response_value_as_timestamp(entity=entity, value=values[0])


def get_api_response_value_as_timestamp(
entity: SensorEntity,
value: Optional[ApiResponseValue],
) -> StateType:
if isinstance(value, int):
return as_local(datetime.fromtimestamp(value))

return None
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "RCT Power",
"hacs": "1.6.0",
"homeassistant": "2024.1.2"
"homeassistant": "2024.1.2",
"render_readme": true
}
Loading
Loading