2018-08-21 13:49:58 +00:00
|
|
|
"""Test Home Assistant remote methods and classes."""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant import core
|
|
|
|
from homeassistant.helpers.json import JSONEncoder
|
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
|
|
|
|
|
|
|
def test_json_encoder(hass):
|
|
|
|
"""Test the JSON Encoder."""
|
|
|
|
ha_json_enc = JSONEncoder()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = core.State("test.test", "hello")
|
2018-08-21 13:49:58 +00:00
|
|
|
|
2021-03-16 16:12:51 +00:00
|
|
|
# Test serializing a datetime
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
assert ha_json_enc.default(now) == now.isoformat()
|
|
|
|
|
|
|
|
# Test serializing a set()
|
|
|
|
data = {"milk", "beer"}
|
2021-03-19 12:41:09 +00:00
|
|
|
assert sorted(ha_json_enc.default(data)) == sorted(data)
|
2021-03-16 16:12:51 +00:00
|
|
|
|
|
|
|
# Test serializing an object which implements as_dict
|
2018-08-21 13:49:58 +00:00
|
|
|
assert ha_json_enc.default(state) == state.as_dict()
|
|
|
|
|
|
|
|
# Default method raises TypeError if non HA object
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
ha_json_enc.default(1)
|