Skip to content

Commit

Permalink
Merge pull request #163 from henricm/fix-unknown-handling
Browse files Browse the repository at this point in the history
fix: handle unknown value for percentage sensor
  • Loading branch information
argoyle authored May 4, 2022
2 parents 78fa51c + 9625906 commit 5c3f2c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/ferroamp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def __init__(self, name, entity_prefix, key, device_id, device_name, interval, p

def update_state_from_events(self, events):
res = super().update_state_from_events(events)
if self.state is not None:
if self.state is not None and self.state != "unknown":
pct = int(float(self.state) / 10) * 10
if pct <= 90:
self._attr_icon = f"mdi:battery-{pct}"
Expand Down
30 changes: 30 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,36 @@ async def test_restore_state(hass, mqtt_mock):
}


async def test_restore_state_unknown(hass, mqtt_mock):
mock_restore_cache(
hass,
[
State("sensor.ferroamp_esm_1_state_of_charge", "unknown")
],
)

hass.state = CoreState.starting

config_entry = create_config()
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
topic = "extapi/data/esm"
msg = '{"id":{"val":"1"}}'
async_fire_mqtt_message(hass, topic, msg)
await hass.async_block_till_done()

state = hass.states.get("sensor.ferroamp_esm_1_state_of_charge")
assert state.state == "unknown"
assert state.attributes == {
'device_class': 'battery',
'friendly_name': 'ESM 1 State of Charge',
'icon': 'mdi:battery-low',
'state_class': 'measurement',
'unit_of_measurement': '%'
}


async def test_update_options(hass, mqtt_mock):
config_entry = create_config()
config_entry.add_to_hass(hass)
Expand Down

0 comments on commit 5c3f2c5

Please sign in to comment.