Repository: home-assistant/core
File: tests/components/homematicip_cloud/test_sensor.py (Python)

Code before:
    # Need to wait for entity to be added after enabling
    await hass.async_block_till_done()

    ha_state, hmip_device = get_and_check_entity_basics(
        hass, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == "15.0"
    assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
    assert ha_state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT

    await async_manipulate_test_data(hass, hmip_device, "dirtLevel", 0.25)
    ha_state = hass.states.get(entity_id)
    assert ha_state.state == "25.0"


async def test_hmip_smoke_detector_alarm_counter(
    hass: HomeAssistant, default_mock_hap_factory: HomeFactory
) -> None:
    """Test HomematicipSmokeDetectorAlarmCounter."""
    entity_id = "sensor.rauchwarnmelder_smoke_alarm_counter"
    entity_name = "Rauchwarnmelder smoke_alarm_counter"
    device_model = "HmIP-SWSD"

    # Pre-register the entity as enabled before platform loads
    entity_registry = er.async_get(hass)
    entity_registry.async_get_or_create(
        "sensor",
        DOMAIN,
        "3014F7110000000000000018_smoke_alarm_counter",
        suggested_object_id="rauchwarnmelder_smoke_alarm_counter",
        disabled_by=None,
    )

    mock_hap = await default_mock_hap_factory.async_get_mock_hap(
        test_devices=["Rauchwarnmelder"]
    )

    await hass.async_block_till_done()

    ha_state, hmip_device = get_and_check_entity_basics(
        hass, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == "2"
    assert ha_state.attributes[ATTR_STATE_CLASS] == SensorStateClass.TOTAL_INCREASING

    await async_manipulate_test_data(hass, hmip_device, "smokeAlarmCounter", 3)
    ha_state = hass.states.get(entity_id)
    assert ha_state.state == "3"


Review comment (suggestion):
```suggestion
    hass: HomeAssistant,
    default_mock_hap_factory: HomeFactory,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test HomematicipSmokeDetectorAlarmCounter."""
    entity_id = "sensor.rauchwarnmelder_smoke_alarm_counter"
    entity_name = "Rauchwarnmelder smoke_alarm_counter"
    device_model = "HmIP-SWSD"

    # Pre-register the entity as enabled before platform loads
```

Code after:
    # Need to wait for entity to be added after enabling
    await hass.async_block_till_done()

    ha_state, hmip_device = get_and_check_entity_basics(
        hass, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == "15.0"
    assert ha_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
    assert ha_state.attributes[ATTR_STATE_CLASS] == SensorStateClass.MEASUREMENT

    await async_manipulate_test_data(hass, hmip_device, "dirtLevel", 0.25)
    ha_state = hass.states.get(entity_id)
    assert ha_state.state == "25.0"


async def test_hmip_smoke_detector_alarm_counter(
    hass: HomeAssistant,
    default_mock_hap_factory: HomeFactory,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test HomematicipSmokeDetectorAlarmCounter."""
    entity_id = "sensor.rauchwarnmelder_smoke_alarm_counter"
    entity_name = "Rauchwarnmelder smoke_alarm_counter"
    device_model = "HmIP-SWSD"

    # Pre-register the entity as enabled before platform loads
    entity_registry.async_get_or_create(
        "sensor",
        DOMAIN,
        "3014F7110000000000000018_smoke_alarm_counter",
        suggested_object_id="rauchwarnmelder_smoke_alarm_counter",
        disabled_by=None,
    )

    mock_hap = await default_mock_hap_factory.async_get_mock_hap(
        test_devices=["Rauchwarnmelder"]
    )

    await hass.async_block_till_done()

    ha_state, hmip_device = get_and_check_entity_basics(
        hass, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == "2"
    assert ha_state.attributes[ATTR_STATE_CLASS] == SensorStateClass.TOTAL_INCREASING

    await async_manipulate_test_data(hass, hmip_device, "smokeAlarmCounter", 3)
    ha_state = hass.states.get(entity_id)
    assert ha_state.state == "3"