Exemplo n.º 1
0
def test_compile_hourly_statistics_unavailable(opp_recorder):
    """Test compiling hourly statistics, with the sensor being unavailable."""
    opp = opp_recorder()
    recorder = opp.data[DATA_INSTANCE]
    setup_component(opp, "sensor", {})
    zero, four, states = record_states_partially_unavailable(opp)
    hist = history.get_significant_states(opp, zero, four)
    assert dict(states) == dict(hist)

    recorder.do_adhoc_statistics(period="hourly", start=four)
    wait_recording_done(opp)
    stats = statistics_during_period(opp, four)
    assert stats == {}
Exemplo n.º 2
0
def test_compile_hourly_statistics(opp_recorder):
    """Test compiling hourly statistics."""
    opp = opp_recorder()
    recorder = opp.data[DATA_INSTANCE]
    setup_component(opp, "sensor", {})
    zero, four, states = record_states(opp)
    hist = history.get_significant_states(opp, zero, four)
    assert dict(states) == dict(hist)

    recorder.do_adhoc_statistics(period="hourly", start=zero)
    wait_recording_done(opp)
    stats = statistics_during_period(opp, zero)
    assert stats == {
        "sensor.test1": [{
            "statistic_id": "sensor.test1",
            "start": process_timestamp_to_utc_isoformat(zero),
            "mean": approx(14.915254237288135),
            "min": approx(10.0),
            "max": approx(20.0),
            "last_reset": None,
            "state": None,
            "sum": None,
        }]
    }
Exemplo n.º 3
0
def test_compile_hourly_energy_statistics(opp_recorder):
    """Test compiling hourly statistics."""
    opp = opp_recorder()
    recorder = opp.data[DATA_INSTANCE]
    setup_component(opp, "sensor", {})
    sns1_attr = {"device_class": "energy", "state_class": "measurement"}
    sns2_attr = {"device_class": "energy"}
    sns3_attr = {}

    zero, four, eight, states = record_energy_states(opp, sns1_attr, sns2_attr,
                                                     sns3_attr)
    hist = history.get_significant_states(opp, zero - timedelta.resolution,
                                          eight + timedelta.resolution)
    assert dict(states)["sensor.test1"] == dict(hist)["sensor.test1"]

    recorder.do_adhoc_statistics(period="hourly", start=zero)
    wait_recording_done(opp)
    recorder.do_adhoc_statistics(period="hourly",
                                 start=zero + timedelta(hours=1))
    wait_recording_done(opp)
    recorder.do_adhoc_statistics(period="hourly",
                                 start=zero + timedelta(hours=2))
    wait_recording_done(opp)
    stats = statistics_during_period(opp, zero)
    assert stats == {
        "sensor.test1": [
            {
                "statistic_id": "sensor.test1",
                "start": process_timestamp_to_utc_isoformat(zero),
                "max": None,
                "mean": None,
                "min": None,
                "last_reset": process_timestamp_to_utc_isoformat(zero),
                "state": approx(20.0),
                "sum": approx(10.0),
            },
            {
                "statistic_id":
                "sensor.test1",
                "start":
                process_timestamp_to_utc_isoformat(zero + timedelta(hours=1)),
                "max":
                None,
                "mean":
                None,
                "min":
                None,
                "last_reset":
                process_timestamp_to_utc_isoformat(four),
                "state":
                approx(40.0),
                "sum":
                approx(10.0),
            },
            {
                "statistic_id":
                "sensor.test1",
                "start":
                process_timestamp_to_utc_isoformat(zero + timedelta(hours=2)),
                "max":
                None,
                "mean":
                None,
                "min":
                None,
                "last_reset":
                process_timestamp_to_utc_isoformat(four),
                "state":
                approx(70.0),
                "sum":
                approx(40.0),
            },
        ]
    }