async def test_lazy_state_handles_different_last_updated_and_last_changed( caplog): """Test that the LazyState handles different last_updated and last_changed.""" now = datetime(2021, 6, 12, 3, 4, 1, 323, tzinfo=dt_util.UTC) row = PropertyMock( entity_id="sensor.valid", state="off", shared_attrs='{"shared":true}', last_updated=now, last_changed=now - timedelta(seconds=60), ) lstate = LazyState(row) assert lstate.as_dict() == { "attributes": { "shared": True }, "entity_id": "sensor.valid", "last_changed": "2021-06-12T03:03:01.000323+00:00", "last_updated": "2021-06-12T03:04:01.000323+00:00", "state": "off", } assert lstate.last_updated == row.last_updated assert lstate.last_changed == row.last_changed assert lstate.as_dict() == { "attributes": { "shared": True }, "entity_id": "sensor.valid", "last_changed": "2021-06-12T03:03:01.000323+00:00", "last_updated": "2021-06-12T03:04:01.000323+00:00", "state": "off", }
async def test_lazy_state_handles_include_json(caplog): """Test that the LazyState class handles invalid json.""" row = PropertyMock( entity_id="sensor.invalid", shared_attrs="{INVALID_JSON}", ) assert LazyState(row).attributes == {} assert "Error converting row to state attributes" in caplog.text
async def test_lazy_state_prefers_shared_attrs_over_attrs(caplog): """Test that the LazyState prefers shared_attrs over attributes.""" row = PropertyMock( entity_id="sensor.invalid", shared_attrs='{"shared":true}', attributes='{"shared":false}', ) assert LazyState(row).attributes == {"shared": True}
def _get_states_with_session(): with session_scope(hass=hass) as session: attr_cache = {} return [ LazyState(row, attr_cache) for row in history._get_rows_with_session( hass, session, utc_point_in_time, entity_ids, run, None, no_attributes, ) ]