Пример #1
0
 def setup_method(self, method):
     """Set up things to be run when tests are started."""
     self.entity = entity.Entity()
     self.entity.entity_id = "test.overwrite_hidden_true"
     self.opp = self.entity.opp = get_test_open_peer_power()
     self.entity.schedule_update_op_state()
     self.opp.block_till_done()
Пример #2
0
async def test_warn_disabled(opp, caplog):
    """Test we warn once if we write to a disabled entity."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=entity_registry.DISABLED_USER,
    )
    mock_registry(opp, {"hello.world": entry})

    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    ent.platform = MagicMock(platform_name="test-platform")

    caplog.clear()
    ent.async_write_op_state()
    assert opp.states.get("hello.world") is None
    assert "Entity hello.world is incorrectly being triggered" in caplog.text

    caplog.clear()
    ent.async_write_op_state()
    assert opp.states.get("hello.world") is None
    assert caplog.text == ""
Пример #3
0
async def test_disabled_in_entity_registry(opp):
    """Test entity is removed if we disable entity registry entry."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=None,
    )
    registry = mock_registry(opp, {"hello.world": entry})

    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    assert ent.enabled is True

    ent.add_to_platform_start(opp, MagicMock(platform_name="test-platform"),
                              None)
    await ent.add_to_platform_finish()
    assert opp.states.get("hello.world") is not None

    entry2 = registry.async_update_entity(
        "hello.world", disabled_by=entity_registry.DISABLED_USER)
    await opp.async_block_till_done()
    assert entry2 != entry
    assert ent.registry_entry == entry2
    assert ent.enabled is False
    assert opp.states.get("hello.world") is None

    entry3 = registry.async_update_entity("hello.world", disabled_by=None)
    await opp.async_block_till_done()
    assert entry3 != entry2
    # Entry is no longer updated, entity is no longer tracking changes
    assert ent.registry_entry == entry2
Пример #4
0
async def test_set_context(opp):
    """Test setting context."""
    context = Context()
    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "hello.world"
    ent.async_set_context(context)
    await ent.async_update_op_state()
    assert opp.states.get("hello.world").context == context
Пример #5
0
async def test_async_remove_no_platform(opp):
    """Test async_remove method when no platform set."""
    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "test.test"
    await ent.async_update_op_state()
    assert len(opp.states.async_entity_ids()) == 1
    await ent.async_remove()
    assert len(opp.states.async_entity_ids()) == 0
Пример #6
0
async def test_async_remove_runs_callbacks(opp):
    """Test async_remove method when no platform set."""
    result = []

    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "test.test"
    ent.async_on_remove(lambda: result.append(1))
    await ent.async_remove()
    assert len(result) == 1
Пример #7
0
async def test_float_conversion(opp):
    """Test conversion of float state to string rounds."""
    assert 2.4 + 1.2 != 3.6
    with patch.object(entity.Entity, "state",
                      PropertyMock(return_value=2.4 + 1.2)):
        ent = entity.Entity()
        ent.opp = opp
        ent.entity_id = "hello.world"
        ent.async_write_op_state()

    state = opp.states.get("hello.world")
    assert state is not None
    assert state.state == "3.6"
Пример #8
0
async def test_warn_slow_write_state(opp, caplog):
    """Check that we log a warning if reading properties takes too long."""
    mock_entity = entity.Entity()
    mock_entity.opp = opp
    mock_entity.entity_id = "comp_test.test_entity"
    mock_entity.platform = MagicMock(platform_name="hue")

    with patch("openpeerpower.helpers.entity.timer", side_effect=[0, 10]):
        mock_entity.async_write_op_state()

    assert ("Updating state for comp_test.test_entity "
            "(<class 'openpeerpower.helpers.entity.Entity'>) "
            "took 10.000 seconds. Please create a bug report at "
            "https://github.com/openpeerpower/core/issues?"
            "q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+hue%22"
            ) in caplog.text
Пример #9
0
async def test_capability_attrs(opp):
    """Test we still include capabilities even when unavailable."""
    with patch.object(entity.Entity, "available",
                      PropertyMock(return_value=False)), patch.object(
                          entity.Entity,
                          "capability_attributes",
                          PropertyMock(return_value={"always": "there"}),
                      ):
        ent = entity.Entity()
        ent.opp = opp
        ent.entity_id = "hello.world"
        ent.async_write_op_state()

    state = opp.states.get("hello.world")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE
    assert state.attributes["always"] == "there"
Пример #10
0
async def test_set_context_expired(opp):
    """Test setting context."""
    context = Context()

    with patch.object(entity.Entity,
                      "context_recent_time",
                      new_callable=PropertyMock) as recent:
        recent.return_value = timedelta(seconds=-5)
        ent = entity.Entity()
        ent.opp = opp
        ent.entity_id = "hello.world"
        ent.async_set_context(context)
        await ent.async_update_op_state()

    assert opp.states.get("hello.world").context != context
    assert ent._context is None
    assert ent._context_set is None
Пример #11
0
async def test_async_schedule_update_op_state(opp):
    """Warn we log when entity update takes a long time and trow exception."""
    update_call = False

    async def async_update():
        """Mock async update."""
        nonlocal update_call
        update_call = True

    mock_entity = entity.Entity()
    mock_entity.opp = opp
    mock_entity.entity_id = "comp_test.test_entity"
    mock_entity.async_update = async_update

    mock_entity.async_schedule_update_op_state(True)
    await opp.async_block_till_done()

    assert update_call is True
Пример #12
0
    def test_reload_core_conf(self):
        """Test reload core conf service."""
        ent = entity.Entity()
        ent.entity_id = "test.entity"
        ent.opp = self.opp
        ent.schedule_update_op_state()
        self.opp.block_till_done()

        state = self.opp.states.get("test.entity")
        assert state is not None
        assert state.state == "unknown"
        assert state.attributes == {}

        files = {
            config.YAML_CONFIG_FILE:
            yaml.dump({
                op.DOMAIN: {
                    "latitude": 10,
                    "longitude": 20,
                    "customize": {
                        "test.Entity": {
                            "hello": "world"
                        }
                    },
                }
            })
        }
        with patch_yaml_files(files, True):
            reload_core_config(self.opp)
            self.opp.block_till_done()

        assert self.opp.config.latitude == 10
        assert self.opp.config.longitude == 20

        ent.schedule_update_op_state()
        self.opp.block_till_done()

        state = self.opp.states.get("test.entity")
        assert state is not None
        assert state.state == "unknown"
        assert state.attributes.get("hello") == "world"
Пример #13
0
async def test_warn_slow_device_update_disabled(opp, caplog):
    """Disable slow update warning with async_device_update."""
    update_call = False

    async def async_update():
        """Mock async update."""
        nonlocal update_call
        await asyncio.sleep(0.00001)
        update_call = True

    mock_entity = entity.Entity()
    mock_entity.opp = opp
    mock_entity.entity_id = "comp_test.test_entity"
    mock_entity.async_update = async_update

    fast_update_time = 0.0000001

    with patch.object(entity, "SLOW_UPDATE_WARNING", fast_update_time):
        await mock_entity.async_device_update(warning=False)
        assert str(fast_update_time) not in caplog.text
        assert mock_entity.entity_id not in caplog.text
        assert update_call
Пример #14
0
async def test_warn_slow_update(opp, caplog):
    """Warn we log when entity update takes a long time."""
    update_call = False

    async def async_update():
        """Mock async update."""
        nonlocal update_call
        await asyncio.sleep(0.00001)
        update_call = True

    mock_entity = entity.Entity()
    mock_entity.opp = opp
    mock_entity.entity_id = "comp_test.test_entity"
    mock_entity.async_update = async_update

    fast_update_time = 0.0000001

    with patch.object(entity, "SLOW_UPDATE_WARNING", fast_update_time):
        await mock_entity.async_update_op_state(True)
        assert str(fast_update_time) in caplog.text
        assert mock_entity.entity_id in caplog.text
        assert update_call
Пример #15
0
async def test_removing_entity_unavailable(opp):
    """Test removing an entity that is still registered creates an unavailable state."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=None,
    )

    ent = entity.Entity()
    ent.opp = opp
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    ent.async_write_op_state()

    state = opp.states.get("hello.world")
    assert state is not None
    assert state.state == STATE_UNKNOWN

    await ent.async_remove()

    state = opp.states.get("hello.world")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE