Clean up async_update_entity helper usage (#68641)

This commit is contained in:
Franck Nijhof 2022-03-25 23:22:58 +01:00 committed by GitHub
parent 53245c6523
commit d645e80ccd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 87 additions and 103 deletions

View file

@ -23,6 +23,7 @@ from homeassistant.const import (
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
from homeassistant.helpers import config_validation as cv, recorder, restore_state from homeassistant.helpers import config_validation as cv, recorder, restore_state
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.service import ( from homeassistant.helpers.service import (
async_extract_config_entry_ids, async_extract_config_entry_ids,
async_extract_referenced_entity_ids, async_extract_referenced_entity_ids,
@ -199,8 +200,7 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
) )
tasks = [ tasks = [
hass.helpers.entity_component.async_update_entity(entity) async_update_entity(hass, entity) for entity in call.data[ATTR_ENTITY_ID]
for entity in call.data[ATTR_ENTITY_ID]
] ]
if tasks: if tasks:

View file

@ -60,6 +60,7 @@ from homeassistant.const import (
STATE_STANDBY, STATE_STANDBY,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util import slugify from homeassistant.util import slugify
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -170,7 +171,7 @@ async def test_reconnect(hass, caplog, config):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -182,7 +183,7 @@ async def test_reconnect(hass, caplog, config):
patch_key patch_key
], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER: ], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER:
for _ in range(5): for _ in range(5):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@ -195,7 +196,7 @@ async def test_reconnect(hass, caplog, config):
with patchers.patch_connect(True)[patch_key], patchers.patch_shell( with patchers.patch_connect(True)[patch_key], patchers.patch_shell(
SHELL_RESPONSE_STANDBY SHELL_RESPONSE_STANDBY
)[patch_key], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER: )[patch_key], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
@ -238,7 +239,7 @@ async def test_adb_shell_returns_none(hass, config):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
@ -246,7 +247,7 @@ async def test_adb_shell_returns_none(hass, config):
with patchers.patch_shell(None)[patch_key], patchers.patch_shell(error=True)[ with patchers.patch_shell(None)[patch_key], patchers.patch_shell(error=True)[
patch_key patch_key
], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER: ], patchers.PATCH_ANDROIDTV_OPEN, patchers.PATCH_SIGNER:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@ -267,7 +268,7 @@ async def test_setup_with_adbkey(hass):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -301,7 +302,7 @@ async def test_sources(hass, config0):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -325,7 +326,7 @@ async def test_sources(hass, config0):
) )
with patch_update: with patch_update:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_PLAYING assert state.state == STATE_PLAYING
@ -351,7 +352,7 @@ async def test_sources(hass, config0):
) )
with patch_update: with patch_update:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_PLAYING assert state.state == STATE_PLAYING
@ -380,7 +381,7 @@ async def _test_exclude_sources(hass, config0, expected_sources):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -416,7 +417,7 @@ async def _test_exclude_sources(hass, config0, expected_sources):
) )
with patch_update: with patch_update:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_PLAYING assert state.state == STATE_PLAYING
@ -461,7 +462,7 @@ async def _test_select_source(hass, config0, source, expected_arg, method_patch)
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -688,7 +689,7 @@ async def test_setup_fail(hass, config):
assert await hass.config_entries.async_setup(config_entry.entry_id) is False assert await hass.config_entries.async_setup(config_entry.entry_id) is False
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is None assert state is None
@ -851,7 +852,7 @@ async def test_update_lock_not_acquired(hass):
await hass.async_block_till_done() await hass.async_block_till_done()
with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]: with patchers.patch_shell(SHELL_RESPONSE_OFF)[patch_key]:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
@ -860,13 +861,13 @@ async def test_update_lock_not_acquired(hass):
"androidtv.androidtv.androidtv_async.AndroidTVAsync.update", "androidtv.androidtv.androidtv_async.AndroidTVAsync.update",
side_effect=LockNotAcquiredException, side_effect=LockNotAcquiredException,
), patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key]: ), patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key]:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
with patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key]: with patchers.patch_shell(SHELL_RESPONSE_STANDBY)[patch_key]:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_STANDBY assert state.state == STATE_STANDBY
@ -1003,7 +1004,7 @@ async def test_get_image(hass, hass_ws_client):
await hass.async_block_till_done() await hass.async_block_till_done()
with patchers.patch_shell("11")[patch_key]: with patchers.patch_shell("11")[patch_key]:
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
@ -1208,20 +1209,20 @@ async def test_exception(hass):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF
# When an unforeseen exception occurs, we close the ADB connection and raise the exception # When an unforeseen exception occurs, we close the ADB connection and raise the exception
with patchers.PATCH_ANDROIDTV_UPDATE_EXCEPTION, pytest.raises(Exception): with patchers.PATCH_ANDROIDTV_UPDATE_EXCEPTION, pytest.raises(Exception):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
# On the next update, HA will reconnect to the device # On the next update, HA will reconnect to the device
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.state == STATE_OFF assert state.state == STATE_OFF

View file

@ -28,6 +28,7 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from .const import ( from .const import (
AWAIR_UUID, AWAIR_UUID,
@ -336,9 +337,7 @@ async def test_awair_unavailable(hass):
) )
with patch("python_awair.AwairClient.query", side_effect=OFFLINE_FIXTURE): with patch("python_awair.AwairClient.query", side_effect=OFFLINE_FIXTURE):
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "sensor.living_room_awair_score")
"sensor.living_room_awair_score"
)
assert_expected_properties( assert_expected_properties(
hass, hass,
registry, registry,

View file

@ -4,6 +4,7 @@ from datetime import timedelta
from homeassistant.components.broadlink.const import DOMAIN from homeassistant.components.broadlink.const import DOMAIN
from homeassistant.components.broadlink.updater import BroadlinkSP4UpdateManager from homeassistant.components.broadlink.updater import BroadlinkSP4UpdateManager
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.entity_registry import async_entries_for_device from homeassistant.helpers.entity_registry import async_entries_for_device
from homeassistant.util import dt from homeassistant.util import dt
@ -81,9 +82,7 @@ async def test_a1_sensor_update(hass):
"light": 3, "light": 3,
"noise": 2, "noise": 2,
} }
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, next(iter(sensors)).entity_id)
next(iter(sensors)).entity_id
)
assert mock_setup.api.check_sensors_raw.call_count == 2 assert mock_setup.api.check_sensors_raw.call_count == 2
sensors_and_states = { sensors_and_states = {
@ -144,9 +143,7 @@ async def test_rm_pro_sensor_update(hass):
assert len(sensors) == 1 assert len(sensors) == 1
mock_setup.api.check_sensors.return_value = {"temperature": 25.8} mock_setup.api.check_sensors.return_value = {"temperature": 25.8}
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, next(iter(sensors)).entity_id)
next(iter(sensors)).entity_id
)
assert mock_setup.api.check_sensors.call_count == 2 assert mock_setup.api.check_sensors.call_count == 2
sensors_and_states = { sensors_and_states = {
@ -178,9 +175,7 @@ async def test_rm_pro_filter_crazy_temperature(hass):
assert len(sensors) == 1 assert len(sensors) == 1
mock_setup.api.check_sensors.return_value = {"temperature": -7} mock_setup.api.check_sensors.return_value = {"temperature": -7}
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, next(iter(sensors)).entity_id)
next(iter(sensors)).entity_id
)
assert mock_setup.api.check_sensors.call_count == 2 assert mock_setup.api.check_sensors.call_count == 2
sensors_and_states = { sensors_and_states = {
@ -258,9 +253,7 @@ async def test_rm4_pro_hts2_sensor_update(hass):
assert len(sensors) == 2 assert len(sensors) == 2
mock_setup.api.check_sensors.return_value = {"temperature": 16.8, "humidity": 34.0} mock_setup.api.check_sensors.return_value = {"temperature": 16.8, "humidity": 34.0}
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, next(iter(sensors)).entity_id)
next(iter(sensors)).entity_id
)
assert mock_setup.api.check_sensors.call_count == 2 assert mock_setup.api.check_sensors.call_count == 2
sensors_and_states = { sensors_and_states = {

View file

@ -16,6 +16,7 @@ from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_DISARMED,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import mock_device, mock_location, mock_mode from . import mock_device, mock_location, mock_mode
@ -59,7 +60,7 @@ async def test_alarm_control_panel(hass, canary) -> None:
# test private system # test private system
type(mocked_location).is_private = PropertyMock(return_value=True) type(mocked_location).is_private = PropertyMock(return_value=True)
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
@ -74,7 +75,7 @@ async def test_alarm_control_panel(hass, canary) -> None:
return_value=mock_mode(4, LOCATION_MODE_HOME) return_value=mock_mode(4, LOCATION_MODE_HOME)
) )
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
@ -86,7 +87,7 @@ async def test_alarm_control_panel(hass, canary) -> None:
return_value=mock_mode(5, LOCATION_MODE_AWAY) return_value=mock_mode(5, LOCATION_MODE_AWAY)
) )
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
@ -98,7 +99,7 @@ async def test_alarm_control_panel(hass, canary) -> None:
return_value=mock_mode(6, LOCATION_MODE_NIGHT) return_value=mock_mode(6, LOCATION_MODE_NIGHT)
) )
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)

View file

@ -16,6 +16,7 @@ from homeassistant.const import (
SIGNAL_STRENGTH_DECIBELS_MILLIWATT, SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -126,7 +127,7 @@ async def test_sensors_attributes_pro(hass, canary) -> None:
future = utcnow() + timedelta(seconds=30) future = utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state2 = hass.states.get(entity_id) state2 = hass.states.get(entity_id)
@ -142,7 +143,7 @@ async def test_sensors_attributes_pro(hass, canary) -> None:
future += timedelta(seconds=30) future += timedelta(seconds=30)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state3 = hass.states.get(entity_id) state3 = hass.states.get(entity_id)

View file

@ -10,6 +10,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.helpers.entity_component import async_update_entity
from tests.components.dexcom import GLUCOSE_READING, init_integration from tests.components.dexcom import GLUCOSE_READING, init_integration
@ -36,12 +37,8 @@ async def test_sensors_unknown(hass):
"homeassistant.components.dexcom.Dexcom.get_current_glucose_reading", "homeassistant.components.dexcom.Dexcom.get_current_glucose_reading",
return_value=None, return_value=None,
): ):
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "sensor.dexcom_test_username_glucose_value")
"sensor.dexcom_test_username_glucose_value" await async_update_entity(hass, "sensor.dexcom_test_username_glucose_trend")
)
await hass.helpers.entity_component.async_update_entity(
"sensor.dexcom_test_username_glucose_trend"
)
test_username_glucose_value = hass.states.get( test_username_glucose_value = hass.states.get(
"sensor.dexcom_test_username_glucose_value" "sensor.dexcom_test_username_glucose_value"
@ -61,12 +58,8 @@ async def test_sensors_update_failed(hass):
"homeassistant.components.dexcom.Dexcom.get_current_glucose_reading", "homeassistant.components.dexcom.Dexcom.get_current_glucose_reading",
side_effect=SessionError, side_effect=SessionError,
): ):
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "sensor.dexcom_test_username_glucose_value")
"sensor.dexcom_test_username_glucose_value" await async_update_entity(hass, "sensor.dexcom_test_username_glucose_trend")
)
await hass.helpers.entity_component.async_update_entity(
"sensor.dexcom_test_username_glucose_trend"
)
test_username_glucose_value = hass.states.get( test_username_glucose_value = hass.states.get(
"sensor.dexcom_test_username_glucose_value" "sensor.dexcom_test_username_glucose_value"

View file

@ -14,6 +14,7 @@ from homeassistant.const import (
STATE_OPEN, STATE_OPEN,
) )
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -149,7 +150,7 @@ async def test_cover_close(
"homeassistant.components.freedompro.get_states", "homeassistant.components.freedompro.get_states",
return_value=states_response, return_value=states_response,
): ):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -10,6 +10,7 @@ from homeassistant.components.fan import (
) )
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, STATE_OFF, STATE_ON from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, STATE_OFF, STATE_ON
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -78,7 +79,7 @@ async def test_fan_set_off(hass, init_integration):
"homeassistant.components.freedompro.get_states", "homeassistant.components.freedompro.get_states",
return_value=states_response, return_value=states_response,
): ):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()
@ -107,7 +108,7 @@ async def test_fan_set_off(hass, init_integration):
"homeassistant.components.freedompro.get_states", "homeassistant.components.freedompro.get_states",
return_value=states_response, return_value=states_response,
): ):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -9,6 +9,7 @@ from homeassistant.components.lock import (
) )
from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -73,7 +74,7 @@ async def test_lock_set_unlock(hass, init_integration):
"homeassistant.components.freedompro.get_states", "homeassistant.components.freedompro.get_states",
return_value=states_response, return_value=states_response,
): ):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -5,6 +5,7 @@ from unittest.mock import ANY, patch
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SERVICE_TURN_ON from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, STATE_OFF, STATE_ON from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, STATE_OFF, STATE_ON
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -61,7 +62,7 @@ async def test_switch_set_off(hass, init_integration):
"homeassistant.components.freedompro.get_states", "homeassistant.components.freedompro.get_states",
return_value=states_response, return_value=states_response,
): ):
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -11,6 +11,7 @@ from homeassistant.components.history_stats import DOMAIN
from homeassistant.components.history_stats.sensor import HistoryStatsSensor from homeassistant.components.history_stats.sensor import HistoryStatsSensor
from homeassistant.const import SERVICE_RELOAD, STATE_UNKNOWN from homeassistant.const import SERVICE_RELOAD, STATE_UNKNOWN
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.setup import async_setup_component, setup_component from homeassistant.setup import async_setup_component, setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -339,7 +340,7 @@ async def test_measure_multiple(hass):
return_value=fake_states, return_value=fake_states,
), patch("homeassistant.components.recorder.history.get_state", return_value=None): ), patch("homeassistant.components.recorder.history.get_state", return_value=None):
for i in range(1, 5): for i in range(1, 5):
await hass.helpers.entity_component.async_update_entity(f"sensor.sensor{i}") await async_update_entity(hass, f"sensor.sensor{i}")
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("sensor.sensor1").state == "0.5" assert hass.states.get("sensor.sensor1").state == "0.5"
@ -416,7 +417,7 @@ async def async_test_measure(hass):
return_value=fake_states, return_value=fake_states,
), patch("homeassistant.components.recorder.history.get_state", return_value=None): ), patch("homeassistant.components.recorder.history.get_state", return_value=None):
for i in range(1, 5): for i in range(1, 5):
await hass.helpers.entity_component.async_update_entity(f"sensor.sensor{i}") await async_update_entity(hass, f"sensor.sensor{i}")
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("sensor.sensor1").state == "0.5" assert hass.states.get("sensor.sensor1").state == "0.5"

View file

@ -280,7 +280,7 @@ async def test_entity_update(hass):
await async_setup_component(hass, "homeassistant", {}) await async_setup_component(hass, "homeassistant", {})
with patch( with patch(
"homeassistant.helpers.entity_component.async_update_entity", "homeassistant.components.homeassistant.async_update_entity",
return_value=None, return_value=None,
) as mock_update: ) as mock_update:
await hass.services.async_call( await hass.services.async_call(

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.components.konnected import config_flow, panel from homeassistant.components.konnected import config_flow, panel
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import utcnow from homeassistant.util import utcnow
@ -654,15 +655,11 @@ async def test_connect_retry(hass, mock_panel):
# confirm switch is unavailable after second attempt # confirm switch is unavailable after second attempt
async_fire_time_changed(hass, utcnow() + timedelta(seconds=11)) async_fire_time_changed(hass, utcnow() + timedelta(seconds=11))
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "switch.konnected_445566_actuator_6")
"switch.konnected_445566_actuator_6"
)
assert hass.states.get("switch.konnected_445566_actuator_6").state == "unavailable" assert hass.states.get("switch.konnected_445566_actuator_6").state == "unavailable"
# confirm switch is available after third attempt # confirm switch is available after third attempt
async_fire_time_changed(hass, utcnow() + timedelta(seconds=21)) async_fire_time_changed(hass, utcnow() + timedelta(seconds=21))
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "switch.konnected_445566_actuator_6")
"switch.konnected_445566_actuator_6"
)
assert hass.states.get("switch.konnected_445566_actuator_6").state == "off" assert hass.states.get("switch.konnected_445566_actuator_6").state == "off"

View file

@ -29,6 +29,7 @@ from homeassistant.const import (
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.helpers.entity_component import async_update_entity
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed from tests.common import MockConfigEntry, async_fire_time_changed
@ -102,7 +103,7 @@ async def test_update_exception(hass, mock_light):
"""Test platform setup.""" """Test platform setup."""
mock_light.get_color.side_effect = pykulersky.PykulerskyException mock_light.get_color.side_effect = pykulersky.PykulerskyException
await hass.helpers.entity_component.async_update_entity("light.bedroom") await async_update_entity(hass, "light.bedroom")
state = hass.states.get("light.bedroom") state = hass.states.get("light.bedroom")
assert state is not None assert state is not None
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE

View file

@ -8,6 +8,7 @@ from homeassistant.const import (
STATE_ON, STATE_ON,
) )
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from . import init_integration from . import init_integration
@ -32,7 +33,7 @@ async def test_download_switch(hass, nzbget_api) -> None:
# test download paused # test download paused
instance.status.return_value["DownloadPaused"] = True instance.status.return_value["DownloadPaused"] = True
await hass.helpers.entity_component.async_update_entity(entity_id) await async_update_entity(hass, entity_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(entity_id) state = hass.states.get(entity_id)

View file

@ -4,6 +4,7 @@ from unittest.mock import MagicMock
from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_component import async_update_entity
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -41,9 +42,7 @@ async def test_anna_climate_binary_sensor_change(
assert state assert state
assert state.state == STATE_ON assert state.state == STATE_ON
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "binary_sensor.opentherm_dhw_state")
"binary_sensor.opentherm_dhw_state"
)
state = hass.states.get("binary_sensor.opentherm_dhw_state") state = hass.states.get("binary_sensor.opentherm_dhw_state")
assert state assert state

View file

@ -3,6 +3,7 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_component import async_update_entity
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -27,9 +28,7 @@ async def test_adam_climate_sensor_entities(
assert state assert state
assert float(state.state) == 7.37 assert float(state.state) == 7.37
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "sensor.zone_lisa_wk_battery")
"sensor.zone_lisa_wk_battery"
)
state = hass.states.get("sensor.zone_lisa_wk_battery") state = hass.states.get("sensor.zone_lisa_wk_battery")
assert state assert state

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
from homeassistant.components.ruckus_unleashed import API_MAC, DOMAIN from homeassistant.components.ruckus_unleashed import API_MAC, DOMAIN
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util import utcnow from homeassistant.util import utcnow
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed
@ -33,7 +34,7 @@ async def test_client_connected(hass):
): ):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID) await async_update_entity(hass, TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID) test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_HOME assert test_client.state == STATE_HOME
@ -51,7 +52,7 @@ async def test_client_disconnected(hass):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID) await async_update_entity(hass, TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID) test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_NOT_HOME assert test_client.state == STATE_NOT_HOME
@ -68,7 +69,7 @@ async def test_clients_update_failed(hass):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID) await async_update_entity(hass, TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID) test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_UNAVAILABLE assert test_client.state == STATE_UNAVAILABLE

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
) )
from homeassistant.core import Context, CoreState, State from homeassistant.core import Context, CoreState, State
from homeassistant.helpers import entity_registry from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -753,14 +754,10 @@ async def test_no_update_template_match_all(hass, caplog):
assert hass.states.get("binary_sensor.all_entity_picture").state == OFF assert hass.states.get("binary_sensor.all_entity_picture").state == OFF
assert hass.states.get("binary_sensor.all_attribute").state == OFF assert hass.states.get("binary_sensor.all_attribute").state == OFF
await hass.helpers.entity_component.async_update_entity("binary_sensor.all_state") await async_update_entity(hass, "binary_sensor.all_state")
await hass.helpers.entity_component.async_update_entity("binary_sensor.all_icon") await async_update_entity(hass, "binary_sensor.all_icon")
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "binary_sensor.all_entity_picture")
"binary_sensor.all_entity_picture" await async_update_entity(hass, "binary_sensor.all_attribute")
)
await hass.helpers.entity_component.async_update_entity(
"binary_sensor.all_attribute"
)
assert hass.states.get("binary_sensor.all_state").state == ON assert hass.states.get("binary_sensor.all_state").state == ON
assert hass.states.get("binary_sensor.all_icon").state == OFF assert hass.states.get("binary_sensor.all_icon").state == OFF

View file

@ -19,6 +19,7 @@ from homeassistant.const import (
) )
from homeassistant.core import Context, CoreState, callback from homeassistant.core import Context, CoreState, callback
from homeassistant.helpers import entity_registry from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.setup import ATTR_COMPONENT, async_setup_component from homeassistant.setup import ATTR_COMPONENT, async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -407,7 +408,7 @@ async def test_invalid_attribute_template(hass, caplog, start_ha, caplog_setup_t
hass.bus.async_fire(EVENT_HOMEASSISTANT_START) hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity("sensor.invalid_template") await async_update_entity(hass, "sensor.invalid_template")
assert "TemplateError" in caplog_setup_text assert "TemplateError" in caplog_setup_text
assert "test_attribute" in caplog.text assert "test_attribute" in caplog.text
@ -506,15 +507,11 @@ async def test_no_template_match_all(hass, caplog):
assert hass.states.get("sensor.invalid_friendly_name").state == "hello" assert hass.states.get("sensor.invalid_friendly_name").state == "hello"
assert hass.states.get("sensor.invalid_attribute").state == "hello" assert hass.states.get("sensor.invalid_attribute").state == "hello"
await hass.helpers.entity_component.async_update_entity("sensor.invalid_state") await async_update_entity(hass, "sensor.invalid_state")
await hass.helpers.entity_component.async_update_entity("sensor.invalid_icon") await async_update_entity(hass, "sensor.invalid_icon")
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "sensor.invalid_entity_picture")
"sensor.invalid_entity_picture" await async_update_entity(hass, "sensor.invalid_friendly_name")
) await async_update_entity(hass, "sensor.invalid_attribute")
await hass.helpers.entity_component.async_update_entity(
"sensor.invalid_friendly_name"
)
await hass.helpers.entity_component.async_update_entity("sensor.invalid_attribute")
assert hass.states.get("sensor.invalid_state").state == "2" assert hass.states.get("sensor.invalid_state").state == "2"
assert hass.states.get("sensor.invalid_icon").state == "hello" assert hass.states.get("sensor.invalid_icon").state == "hello"

View file

@ -11,6 +11,7 @@ from homeassistant.components.vacuum import (
STATE_RETURNING, STATE_RETURNING,
) )
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.helpers.entity_component import async_update_entity
from tests.common import assert_setup_component from tests.common import assert_setup_component
from tests.components.vacuum import common from tests.components.vacuum import common
@ -244,9 +245,7 @@ async def test_attribute_templates(hass, start_ha):
hass.states.async_set("sensor.test_state", "Works") hass.states.async_set("sensor.test_state", "Works")
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity( await async_update_entity(hass, "vacuum.test_template_vacuum")
"vacuum.test_template_vacuum"
)
state = hass.states.get("vacuum.test_template_vacuum") state = hass.states.get("vacuum.test_template_vacuum")
assert state.attributes["test_attribute"] == "It Works." assert state.attributes["test_attribute"] == "It Works."

View file

@ -16,7 +16,7 @@ from homeassistant.const import (
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent, async_update_entity
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -385,7 +385,7 @@ async def test_update_entity(hass):
# Called as part of async_add_entities # Called as part of async_add_entities
assert len(entity.async_write_ha_state.mock_calls) == 1 assert len(entity.async_write_ha_state.mock_calls) == 1
await hass.helpers.entity_component.async_update_entity(entity.entity_id) await async_update_entity(hass, entity.entity_id)
assert len(entity.async_update_ha_state.mock_calls) == 1 assert len(entity.async_update_ha_state.mock_calls) == 1
assert entity.async_update_ha_state.mock_calls[-1][1][0] is True assert entity.async_update_ha_state.mock_calls[-1][1][0] is True