Repository: home-assistant/core
File: homeassistant/components/group/sensor.py (Python)

Code before:


def calc_last(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float | None]:
    """Calculate last value."""
    last_updated: datetime | None = None
    last_entity_id: str | None = None
    last: float | None = None
    for entity_id, state_f, state in sensor_values:
        if last_updated is None or state.last_updated > last_updated:
            last_updated = state.last_updated
            last = state_f
            last_entity_id = entity_id

    attributes = {ATTR_LAST_ENTITY_ID: last_entity_id}
    return attributes, last


def calc_first(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float | None]:
    """Calculate first value."""
    first_entity_id: str | None = None
    first: float | None = None
    if len(sensor_values) > 0:
        first_entity_id, first, _ = sensor_values[0]

    attributes = {ATTR_FIRST_ENTITY_ID: first_entity_id}
    return attributes, first


def calc_range(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float]:
    """Calculate range value."""
    max_result = max((sensor_value for _, sensor_value, _ in sensor_values))
    min_result = min((sensor_value for _, sensor_value, _ in sensor_values))

    value: float = max_result - min_result
    return {}, value


def calc_stdev(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float]:
    """Calculate standard deviation value."""
    result = (sensor_value for _, sensor_value, _ in sensor_values)

    value: float = statistics.stdev(result)
    return {}, value

Review comment (suggestion):
Empty sequences are false. Should be condensed to:
```suggestion
    if sensor_values:
```

Code after:


def calc_last(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float | None]:
    """Calculate last value."""
    last_updated: datetime | None = None
    last_entity_id: str | None = None
    last: float | None = None
    for entity_id, state_f, state in sensor_values:
        if last_updated is None or state.last_updated > last_updated:
            last_updated = state.last_updated
            last = state_f
            last_entity_id = entity_id

    attributes = {ATTR_LAST_ENTITY_ID: last_entity_id}
    return attributes, last


def calc_first_available(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float | None]:
    """Calculate first available value."""
    first_available_entity_id: str | None = None
    first_available: float | None = None
    if sensor_values:
        first_available_entity_id, first_available, _ = sensor_values[0]

    attributes = {ATTR_FIRST_AVAILABLE_ENTITY_ID: first_available_entity_id}
    return attributes, first_available


def calc_range(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float]:
    """Calculate range value."""
    max_result = max((sensor_value for _, sensor_value, _ in sensor_values))
    min_result = min((sensor_value for _, sensor_value, _ in sensor_values))

    value: float = max_result - min_result
    return {}, value


def calc_stdev(
    sensor_values: list[tuple[str, float, State]],
) -> tuple[dict[str, str | None], float]:
    """Calculate standard deviation value."""
    result = (sensor_value for _, sensor_value, _ in sensor_values)

    value: float = statistics.stdev(result)
    return {}, value