def test_async_schedule_update_ha_state(hass): """Warn we log when entity update takes a long time and trow exception.""" update_call = False @asyncio.coroutine def async_update(): """Mock async update.""" nonlocal update_call update_call = True mock_entity = entity.Entity() mock_entity.hass = hass mock_entity.entity_id = "comp_test.test_entity" mock_entity.async_update = async_update mock_entity.async_schedule_update_ha_state(True) yield from hass.async_block_till_done() assert update_call is True
async def test_warn_slow_device_update_disabled(hass): """Disable slow update warning with async_device_update.""" update_call = False async def async_update(): """Mock async update.""" nonlocal update_call update_call = True mock_entity = entity.Entity() mock_entity.hass = hass mock_entity.entity_id = "comp_test.test_entity" mock_entity.async_update = async_update with patch.object(hass.loop, "call_later", MagicMock()) as mock_call: await mock_entity.async_device_update(warning=False) assert not mock_call.called assert update_call
def test_reload_core_conf(self): """Test reload core conf service.""" ent = entity.Entity() ent.entity_id = 'test.entity' ent.hass = self.hass ent.update_ha_state() state = self.hass.states.get('test.entity') assert state is not None assert state.state == 'unknown' assert state.attributes == {} with TemporaryDirectory() as conf_dir: self.hass.config.config_dir = conf_dir conf_yaml = self.hass.config.path(config.YAML_CONFIG_FILE) with open(conf_yaml, 'a') as fp: fp.write( yaml.dump({ ha.DOMAIN: { 'latitude': 10, 'longitude': 20, 'customize': { 'test.Entity': { 'hello': 'world' } } } })) comps.reload_core_config(self.hass) self.hass.block_till_done() assert 10 == self.hass.config.latitude assert 20 == self.hass.config.longitude ent.update_ha_state() state = self.hass.states.get('test.entity') assert state is not None assert state.state == 'unknown' assert state.attributes.get('hello') == 'world'
def test_reload_core_conf(self): """Test reload core conf service.""" ent = entity.Entity() ent.entity_id = 'test.entity' ent.hass = self.hass ent.schedule_update_ha_state() self.hass.block_till_done() state = self.hass.states.get('test.entity') assert state is not None assert state.state == 'unknown' assert state.attributes == {} files = { config.YAML_CONFIG_FILE: yaml.dump({ ha.DOMAIN: { 'latitude': 10, 'longitude': 20, 'customize': { 'test.Entity': { 'hello': 'world' } } } }) } with patch_yaml_files(files, True): comps.reload_core_config(self.hass) self.hass.block_till_done() assert self.hass.config.latitude == 10 assert self.hass.config.longitude == 20 ent.schedule_update_ha_state() self.hass.block_till_done() state = self.hass.states.get('test.entity') assert state is not None assert state.state == 'unknown' assert state.attributes.get('hello') == 'world'
def test_reload_core_conf(self): """Test reload core conf service.""" ent = entity.Entity() ent.entity_id = "test.entity" ent.hass = self.hass ent.schedule_update_ha_state() self.hass.block_till_done() state = self.hass.states.get("test.entity") assert state is not None assert state.state == "unknown" assert state.attributes == {} files = { config.YAML_CONFIG_FILE: yaml.dump({ ha.DOMAIN: { "latitude": 10, "longitude": 20, "customize": { "test.Entity": { "hello": "world" } }, } }) } with patch_yaml_files(files, True): reload_core_config(self.hass) self.hass.block_till_done() assert self.hass.config.latitude == 10 assert self.hass.config.longitude == 20 ent.schedule_update_ha_state() self.hass.block_till_done() state = self.hass.states.get("test.entity") assert state is not None assert state.state == "unknown" assert state.attributes.get("hello") == "world"
def test_warn_slow_device_update_disabled(hass): """Disable slow update warning with async_device_update.""" update_call = False @asyncio.coroutine def async_update(): """Mock async update.""" nonlocal update_call update_call = True mock_entity = entity.Entity() mock_entity.hass = hass mock_entity.entity_id = 'comp_test.test_entity' mock_entity.async_update = async_update with patch.object(hass.loop, 'call_later', MagicMock()) \ as mock_call: yield from mock_entity.async_device_update(warning=False) assert not mock_call.called assert update_call
async def test_warn_slow_device_update_disabled(hass, 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.hass = hass 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
async def test_warn_slow_update(hass, 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.hass = hass 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_ha_state(True) assert str(fast_update_time) in caplog.text assert mock_entity.entity_id in caplog.text assert update_call
async def test_removing_entity_unavailable(hass): """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.hass = hass ent.entity_id = "hello.world" ent.registry_entry = entry ent.async_write_ha_state() state = hass.states.get("hello.world") assert state is not None assert state.state == STATE_UNKNOWN await ent.async_remove() state = hass.states.get("hello.world") assert state is not None assert state.state == STATE_UNAVAILABLE
def setup_method(self, method): """Setup things to be run when tests are started.""" self.entity = entity.Entity() self.entity.entity_id = 'test.overwrite_hidden_true' self.hass = self.entity.hass = get_test_home_assistant() self.entity.update_ha_state()
def setUp(self): # pylint: disable=invalid-name """ Init needed objects. """ self.entity = entity.Entity() self.entity.entity_id = 'test.overwrite_hidden_true' self.hass = self.entity.hass = ha.HomeAssistant() self.entity.update_ha_state()
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.entity = entity.Entity() self.entity.entity_id = 'test.overwrite_hidden_true' self.hass = self.entity.hass = get_test_home_assistant() self.entity.update_ha_state()