Skip to content

Commit

Permalink
Merge e13c0b8 into 3dca698
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Sep 24, 2022
2 parents 3dca698 + e13c0b8 commit cd6b8bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -357,7 +357,7 @@ def on_state_change(self, event):
for state in states
if state and state.state == STATE_UNAVAILABLE
]
if unavailable_entities:
if unavailable_entities and isinstance(self, GroupedEnergySensor):
_LOGGER.error(
"%s: One or more members of the group are unavailable, setting group to unavailable (%s)",
self.entity_id,
Expand All @@ -368,7 +368,7 @@ def on_state_change(self, event):
return

available_states = [
state for state in states if state and state.state != STATE_UNKNOWN
state for state in states if state and state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE]
]

# Remove members with an incompatible unit of measurement for now
Expand Down
14 changes: 11 additions & 3 deletions tests/sensors/test_group.py
Expand Up @@ -21,6 +21,7 @@
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import entity_registry as er
Expand Down Expand Up @@ -335,6 +336,10 @@ async def test_mega_watt_hour(hass: HomeAssistant):


async def test_group_unavailable_when_members_unavailable(hass: HomeAssistant):
"""
When any of the group members becomes unavailable the energy group should also be unavailable
Group power sensor must only be unavailable when ALL group members are unavailable
"""
await create_input_booleans(hass, ["test1", "test2"])

await run_powercalc_setup_yaml_config(
Expand All @@ -356,14 +361,17 @@ async def test_group_unavailable_when_members_unavailable(hass: HomeAssistant):
power_state = hass.states.get("sensor.testgroup_power")
assert power_state.state == STATE_UNAVAILABLE

energy_state = hass.states.get("sensor.testgroup_power")
assert energy_state.state == STATE_UNAVAILABLE
energy_state = hass.states.get("sensor.testgroup_energy")
assert energy_state.state == STATE_UNKNOWN

hass.states.async_set("input_boolean.test1", STATE_ON)
await hass.async_block_till_done()

power_state = hass.states.get("sensor.testgroup_power")
assert power_state.state == STATE_UNAVAILABLE
assert power_state.state != STATE_UNAVAILABLE

energy_state = hass.states.get("sensor.testgroup_energy")
assert energy_state.state == STATE_UNAVAILABLE


async def test_hide_members(hass: HomeAssistant):
Expand Down

0 comments on commit cd6b8bf

Please sign in to comment.