예제 #1
0
    def time_fired_isoformat(self):
        """Time event was fired in utc isoformat."""
        if not self._time_fired_isoformat:
            self._time_fired_isoformat = process_timestamp_to_utc_isoformat(
                self._row.time_fired or dt_util.utcnow())

        return self._time_fired_isoformat
예제 #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,
        }]
    }
예제 #3
0
async def test_process_timestamp_to_utc_isoformat():
    """Test processing time stamp to UTC isoformat."""
    datetime_with_tzinfo = datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC)
    datetime_without_tzinfo = datetime(2016, 7, 9, 11, 0, 0)
    est = dt_util.get_time_zone("US/Eastern")
    datetime_est_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=est)
    est = dt_util.get_time_zone("US/Eastern")
    datetime_est_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=est)
    nst = dt_util.get_time_zone("Canada/Newfoundland")
    datetime_nst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=nst)
    hst = dt_util.get_time_zone("US/Hawaii")
    datetime_hst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=hst)

    assert (process_timestamp_to_utc_isoformat(datetime_with_tzinfo) ==
            "2016-07-09T11:00:00+00:00")
    assert (process_timestamp_to_utc_isoformat(datetime_without_tzinfo) ==
            "2016-07-09T11:00:00+00:00")
    assert (process_timestamp_to_utc_isoformat(datetime_est_timezone) ==
            "2016-07-09T15:00:00+00:00")
    assert (process_timestamp_to_utc_isoformat(datetime_nst_timezone) ==
            "2016-07-09T13:30:00+00:00")
    assert (process_timestamp_to_utc_isoformat(datetime_hst_timezone) ==
            "2016-07-09T21:00:00+00:00")
    assert process_timestamp_to_utc_isoformat(None) is None
예제 #4
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),
            },
        ]
    }