-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calculated entities for power generation (#127)
- Loading branch information
1 parent
75b2ce8
commit 3edf7f1
Showing
6 changed files
with
140 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Optional | ||
|
||
from homeassistant.components.sensor import SensorEntity | ||
from homeassistant.helpers.typing import StateType | ||
|
||
from .api import ApiResponseValue | ||
from .const import NUMERIC_STATE_DECIMAL_DIGITS | ||
|
||
|
||
def get_first_api_response_value_as_state( | ||
entity: SensorEntity, | ||
values: list[Optional[ApiResponseValue]], | ||
) -> StateType: | ||
if len(values) <= 0: | ||
return None | ||
|
||
return get_api_response_value_as_state(entity=entity, value=values[0]) | ||
|
||
|
||
def get_api_response_value_as_state( | ||
entity: SensorEntity, | ||
value: Optional[ApiResponseValue], | ||
) -> StateType: | ||
if isinstance(value, bytes): | ||
return value.hex() | ||
|
||
if isinstance(value, tuple): | ||
return None | ||
|
||
if isinstance(value, (int, float)) and entity.native_unit_of_measurement == "%": | ||
return round(value * 100, NUMERIC_STATE_DECIMAL_DIGITS) | ||
|
||
if isinstance(value, (int, float)): | ||
return round(value, NUMERIC_STATE_DECIMAL_DIGITS) | ||
|
||
return value | ||
|
||
|
||
def get_first_api_reponse_value_as_absolute_state( | ||
entity: SensorEntity, | ||
values: list[Optional[ApiResponseValue]], | ||
) -> StateType: | ||
value = get_first_api_response_value_as_state(entity=entity, values=values) | ||
|
||
if isinstance(value, (int, float)): | ||
return abs(value) | ||
|
||
return value | ||
|
||
|
||
def sum_api_response_values_as_state( | ||
entity: SensorEntity, | ||
values: list[Optional[ApiResponseValue]], | ||
) -> StateType: | ||
return sum(value for value in values if isinstance(value, (int, float))) |