async def test_polling_updates_entities_with_exception(hass): """Test the updated entities that not break with an exception.""" component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20)) update_ok = [] update_err = [] def update_mock(): """Mock normal update.""" update_ok.append(None) def update_mock_err(): """Mock error update.""" update_err.append(None) raise AssertionError("Fake error update") ent1 = MockEntity(should_poll=True) ent1.update = update_mock_err ent2 = MockEntity(should_poll=True) ent2.update = update_mock ent3 = MockEntity(should_poll=True) ent3.update = update_mock ent4 = MockEntity(should_poll=True) ent4.update = update_mock await component.async_add_entities([ent1, ent2, ent3, ent4]) update_ok.clear() update_err.clear() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=20)) await hass.async_block_till_done() assert len(update_ok) == 3 assert len(update_err) == 1
def test_raise_error_on_update(hass): """Test the add entity if they raise an error on update.""" updates = [] component = EntityComponent(_LOGGER, DOMAIN, hass) entity1 = MockEntity(name='test_1') entity2 = MockEntity(name='test_2') def _raise(): """Helper to raise an exception.""" raise AssertionError entity1.update = _raise entity2.update = lambda: updates.append(1) yield from component.async_add_entities([entity1, entity2], True) assert len(updates) == 1 assert 1 in updates
async def test_update_state_adds_entities_with_update_before_add_false(hass): """Test if not call update before add to state machine.""" component = EntityComponent(_LOGGER, DOMAIN, hass) ent = MockEntity() ent.update = Mock(spec_set=True) await component.async_add_entities([ent], False) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == 1 assert not ent.update.called
def test_update_state_adds_entities_with_update_before_add_false(self): """Test if not call update before add to state machine.""" component = EntityComponent(_LOGGER, DOMAIN, self.hass) ent = MockEntity() ent.update = Mock(spec_set=True) component.add_entities([ent], False) self.hass.block_till_done() assert 1 == len(self.hass.states.entity_ids()) assert not ent.update.called
async def test_update_state_adds_entities_with_update_before_add_true(opp): """Test if call update before add to state machine.""" component = EntityComponent(_LOGGER, DOMAIN, opp) ent = MockEntity() ent.update = Mock(spec_set=True) await component.async_add_entities([ent], True) await opp.async_block_till_done() assert len(opp.states.async_entity_ids()) == 1 assert ent.update.called
async def test_raise_error_on_update(hass): """Test the add entity if they raise an error on update.""" updates = [] component = EntityComponent(_LOGGER, DOMAIN, hass) entity1 = MockEntity(name="test_1") entity2 = MockEntity(name="test_2") def _raise(): """Raise an exception.""" raise AssertionError entity1.update = _raise entity2.update = lambda: updates.append(1) await component.async_add_entities([entity1, entity2], True) assert len(updates) == 1 assert 1 in updates assert entity1.hass is None assert entity1.platform is None assert entity2.hass is not None assert entity2.platform is not None
async def test_update_state_adds_entities(hass): """Test if updating poll entities cause an entity to be added works.""" component = EntityComponent(_LOGGER, DOMAIN, hass) ent1 = MockEntity() ent2 = MockEntity(should_poll=True) await component.async_add_entities([ent2]) assert len(hass.states.async_entity_ids()) == 1 ent2.update = lambda *_: component.add_entities([ent1]) async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL) await hass.async_block_till_done() assert len(hass.states.async_entity_ids()) == 2
def test_update_state_adds_entities(self): """Test if updating poll entities cause an entity to be added works.""" component = EntityComponent(_LOGGER, DOMAIN, self.hass) ent1 = MockEntity() ent2 = MockEntity(should_poll=True) component.add_entities([ent2]) assert 1 == len(self.hass.states.entity_ids()) ent2.update = lambda *_: component.add_entities([ent1]) fire_time_changed(self.hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL) self.hass.block_till_done() assert 2 == len(self.hass.states.entity_ids())
def test_polling_updates_entities_with_exception(self): """Test the updated entities that not break with an exception.""" component = EntityComponent( _LOGGER, DOMAIN, self.hass, timedelta(seconds=20)) update_ok = [] update_err = [] def update_mock(): """Mock normal update.""" update_ok.append(None) def update_mock_err(): """Mock error update.""" update_err.append(None) raise AssertionError("Fake error update") ent1 = MockEntity(should_poll=True) ent1.update = update_mock_err ent2 = MockEntity(should_poll=True) ent2.update = update_mock ent3 = MockEntity(should_poll=True) ent3.update = update_mock ent4 = MockEntity(should_poll=True) ent4.update = update_mock component.add_entities([ent1, ent2, ent3, ent4]) update_ok.clear() update_err.clear() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=20)) self.hass.block_till_done() assert len(update_ok) == 3 assert len(update_err) == 1
def test_update_state_adds_entities(self): """Test if updating poll entities cause an entity to be added works.""" component = EntityComponent(_LOGGER, DOMAIN, self.hass) ent1 = MockEntity() ent2 = MockEntity(should_poll=True) component.add_entities([ent2]) assert 1 == len(self.hass.states.entity_ids()) ent2.update = lambda *_: component.add_entities([ent1]) fire_time_changed( self.hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL ) self.hass.block_till_done() assert 2 == len(self.hass.states.entity_ids())