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

Fix restoring state class in mobile app #126868

Merged
merged 2 commits into from
Sep 27, 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
2 changes: 2 additions & 0 deletions homeassistant/components/mobile_app/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ async def async_setup_entry(
ATTR_SENSOR_UOM: entry.unit_of_measurement,
ATTR_SENSOR_ENTITY_CATEGORY: entry.entity_category,
}
if capabilities := entry.capabilities:
config[ATTR_SENSOR_STATE_CLASS] = capabilities.get(ATTR_SENSOR_STATE_CLASS)
entities.append(MobileAppSensor(config, config_entry))

async_add_entities(entities)
Expand Down
75 changes: 75 additions & 0 deletions tests/components/mobile_app/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,78 @@ async def test_updating_disabled_sensor(
json = await update_resp.json()
assert json["battery_state"]["success"] is True
assert json["battery_state"]["is_disabled"] is True


async def test_recreate_correct_from_entity_registry(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be re-created from entity registry."""
webhook_id = create_registrations[1]["webhook_id"]
webhook_url = f"/api/webhook/{webhook_id}"

reg_resp = await webhook_client.post(
webhook_url,
json={
"type": "register_sensor",
"data": {
"device_class": "battery",
"icon": "mdi:battery",
"name": "Battery State",
"state": 100,
"type": "sensor",
"unique_id": "battery_state",
"unit_of_measurement": PERCENTAGE,
"state_class": "measurement",
},
},
)

assert reg_resp.status == HTTPStatus.CREATED

update_resp = await webhook_client.post(
webhook_url,
json={
"type": "update_sensor_states",
"data": [
{
"icon": "mdi:battery-unknown",
"state": 123,
"type": "sensor",
"unique_id": "battery_state",
},
],
},
)

assert update_resp.status == HTTPStatus.OK

entity = hass.states.get("sensor.test_1_battery_state")

assert entity is not None
entity_entry = entity_registry.async_get("sensor.test_1_battery_state")
assert entity_entry is not None

assert entity_entry.capabilities == {
"state_class": "measurement",
}

entry = hass.config_entries.async_entries("mobile_app")[1]

assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()

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

assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

entity_entry = entity_registry.async_get("sensor.test_1_battery_state")
assert entity_entry is not None
assert hass.states.get("sensor.test_1_battery_state") is not None

assert entity_entry.capabilities == {
"state_class": "measurement",
}