Skip to content

Commit

Permalink
Make Ohmpilot entity state translateable
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Jul 27, 2023
1 parent 5129aa0 commit 328283e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
27 changes: 27 additions & 0 deletions homeassistant/components/fronius/const.py
Expand Up @@ -98,3 +98,30 @@ def get_meter_location_description(code: StateType) -> MeterLocationCodeOption |
case _ as _code if 256 <= _code <= 511:
return MeterLocationCodeOption.SUBLOAD
return None

Check warning on line 100 in homeassistant/components/fronius/const.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/fronius/const.py#L94-L100

Added lines #L94 - L100 were not covered by tests


class OhmPilotStateCodeOption(StrEnum):
"""OhmPilot state codes for Fronius inverters."""

# these are keys for state translations - so snake_case is used
UP_AND_RUNNING = "up_and_running"
KEEP_MINIMUM_TEMPERATURE = "keep_minimum_temperature"
LEGIONELLA_PROTECTION = "legionella_protection"
CRITICAL_FAULT = "critical_fault"
FAULT = "fault"
BOOST_MODE = "boost_mode"


_OHMPILOT_STATE_CODES: Final[dict[int, OhmPilotStateCodeOption]] = {
0: OhmPilotStateCodeOption.UP_AND_RUNNING,
1: OhmPilotStateCodeOption.KEEP_MINIMUM_TEMPERATURE,
2: OhmPilotStateCodeOption.LEGIONELLA_PROTECTION,
3: OhmPilotStateCodeOption.CRITICAL_FAULT,
4: OhmPilotStateCodeOption.FAULT,
5: OhmPilotStateCodeOption.BOOST_MODE,
}


def get_ohmpilot_state_message(code: StateType) -> OhmPilotStateCodeOption | None:
"""Return a status message for a given status code."""
return _OHMPILOT_STATE_CODES.get(code) # type: ignore[arg-type]
6 changes: 6 additions & 0 deletions homeassistant/components/fronius/sensor.py
Expand Up @@ -36,8 +36,10 @@
SOLAR_NET_DISCOVERY_NEW,
InverterStatusCodeOption,
MeterLocationCodeOption,
OhmPilotStateCodeOption,
get_inverter_status_message,
get_meter_location_description,
get_ohmpilot_state_message,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -512,7 +514,11 @@ class FroniusSensorEntityDescription(SensorEntityDescription):
),
FroniusSensorEntityDescription(
key="state_message",
response_key="state_code",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=SensorDeviceClass.ENUM,
options=[opt.value for opt in OhmPilotStateCodeOption],
value_fn=get_ohmpilot_state_message,
),
]

Expand Down
10 changes: 9 additions & 1 deletion homeassistant/components/fronius/strings.json
Expand Up @@ -218,7 +218,15 @@
"name": "State code"
},
"state_message": {
"name": "State message"
"name": "State message",
"state": {
"up_and_running": "Up and running",
"keep_minimum_temperature": "Keep minimum temperature",
"legionella_protection": "Legionella protection",
"critical_fault": "Critical fault",
"fault": "Fault",
"boost_mode": "Boost mode"
}
},
"meter_mode": {
"name": "Meter mode"
Expand Down
2 changes: 1 addition & 1 deletion tests/components/fronius/test_sensor.py
Expand Up @@ -360,7 +360,7 @@ def assert_state(entity_id, expected_state):
assert_state("sensor.ohmpilot_power", 0.0)
assert_state("sensor.ohmpilot_temperature", 38.9)
assert_state("sensor.ohmpilot_state_code", 0.0)
assert_state("sensor.ohmpilot_state_message", "Up and running")
assert_state("sensor.ohmpilot_state_message", "up_and_running")
# power_flow
assert_state("sensor.solarnet_power_grid", 2274.9)
assert_state("sensor.solarnet_power_battery", 0.1591)
Expand Down

0 comments on commit 328283e

Please sign in to comment.