Use assignment expressions 31 (#58715)
This commit is contained in:
parent
f7dea3aa1d
commit
a48ddcadd4
26 changed files with 27 additions and 58 deletions
|
@ -139,8 +139,7 @@ class EnOceanSensor(EnOceanEntity, RestoreEntity, SensorEntity):
|
||||||
if self._attr_native_value is not None:
|
if self._attr_native_value is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if (state := await self.async_get_last_state()) is not None:
|
||||||
if state is not None:
|
|
||||||
self._attr_native_value = state.state
|
self._attr_native_value = state.state
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
|
|
|
@ -198,8 +198,7 @@ async def async_setup(hass, config):
|
||||||
_LOGGER.info("Start envisalink")
|
_LOGGER.info("Start envisalink")
|
||||||
controller.start()
|
controller.start()
|
||||||
|
|
||||||
result = await sync_connect
|
if not await sync_connect:
|
||||||
if not result:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Load sub-components for Envisalink
|
# Load sub-components for Envisalink
|
||||||
|
|
|
@ -137,8 +137,7 @@ class RuntimeEntryData:
|
||||||
|
|
||||||
async def async_load_from_store(self) -> tuple[list[EntityInfo], list[UserService]]:
|
async def async_load_from_store(self) -> tuple[list[EntityInfo], list[UserService]]:
|
||||||
"""Load the retained data from store and return de-serialized data."""
|
"""Load the retained data from store and return de-serialized data."""
|
||||||
restored = await self.store.async_load()
|
if (restored := await self.store.async_load()) is None:
|
||||||
if restored is None:
|
|
||||||
return [], []
|
return [], []
|
||||||
restored = cast("dict[str, Any]", restored)
|
restored = cast("dict[str, Any]", restored)
|
||||||
self._storage_contents = restored.copy()
|
self._storage_contents = restored.copy()
|
||||||
|
|
|
@ -49,8 +49,7 @@ class SpeedtestSensor(RestoreEntity, SensorEntity):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if not (state := await self.async_get_last_state()):
|
||||||
if not state:
|
|
||||||
return
|
return
|
||||||
self._attr_native_value = state.state
|
self._attr_native_value = state.state
|
||||||
|
|
||||||
|
|
|
@ -171,8 +171,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity):
|
||||||
|
|
||||||
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
|
||||||
|
|
||||||
old_state = await self.async_get_last_state()
|
if (old_state := await self.async_get_last_state()) is not None:
|
||||||
if old_state is not None:
|
|
||||||
if old_state.attributes.get(ATTR_MODE) == MODE_AWAY:
|
if old_state.attributes.get(ATTR_MODE) == MODE_AWAY:
|
||||||
self._is_away = True
|
self._is_away = True
|
||||||
self._saved_target_humidity = self._target_humidity
|
self._saved_target_humidity = self._target_humidity
|
||||||
|
|
|
@ -144,8 +144,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity):
|
||||||
# Restore the last activity so we know
|
# Restore the last activity so we know
|
||||||
# how what to turn on if nothing
|
# how what to turn on if nothing
|
||||||
# is specified
|
# is specified
|
||||||
last_state = await self.async_get_last_state()
|
if not (last_state := await self.async_get_last_state()):
|
||||||
if not last_state:
|
|
||||||
return
|
return
|
||||||
if ATTR_LAST_ACTIVITY not in last_state.attributes:
|
if ATTR_LAST_ACTIVITY not in last_state.attributes:
|
||||||
return
|
return
|
||||||
|
|
|
@ -124,8 +124,7 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Handle entity which will be added."""
|
"""Handle entity which will be added."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
try:
|
try:
|
||||||
self._state = Decimal(state.state)
|
self._state = Decimal(state.state)
|
||||||
except (DecimalException, ValueError) as err:
|
except (DecimalException, ValueError) as err:
|
||||||
|
|
|
@ -60,8 +60,7 @@ class Iperf3Sensor(RestoreEntity, SensorEntity):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if not (state := await self.async_get_last_state()):
|
||||||
if not state:
|
|
||||||
return
|
return
|
||||||
self._attr_native_value = state.state
|
self._attr_native_value = state.state
|
||||||
|
|
||||||
|
|
|
@ -117,8 +117,7 @@ class ISYLightEntity(ISYNodeEntity, LightEntity, RestoreEntity):
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
self._last_brightness = self.brightness or 255
|
self._last_brightness = self.brightness or 255
|
||||||
last_state = await self.async_get_last_state()
|
if not (last_state := await self.async_get_last_state()):
|
||||||
if not last_state:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -231,8 +231,7 @@ class LimitlessLEDGroup(LightEntity, RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Handle entity about to be added to hass event."""
|
"""Handle entity about to be added to hass event."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
last_state = await self.async_get_last_state()
|
if last_state := await self.async_get_last_state():
|
||||||
if last_state:
|
|
||||||
self._is_on = last_state.state == STATE_ON
|
self._is_on = last_state.state == STATE_ON
|
||||||
self._brightness = last_state.attributes.get("brightness")
|
self._brightness = last_state.attributes.get("brightness")
|
||||||
self._temperature = last_state.attributes.get("color_temp")
|
self._temperature = last_state.attributes.get("color_temp")
|
||||||
|
|
|
@ -235,9 +235,7 @@ class DashboardsCollection(collection.StorageCollection):
|
||||||
|
|
||||||
async def _async_load_data(self) -> dict | None:
|
async def _async_load_data(self) -> dict | None:
|
||||||
"""Load the data."""
|
"""Load the data."""
|
||||||
data = await self.store.async_load()
|
if (data := await self.store.async_load()) is None:
|
||||||
|
|
||||||
if data is None:
|
|
||||||
return cast(Optional[dict], data)
|
return cast(Optional[dict], data)
|
||||||
|
|
||||||
updated = False
|
updated = False
|
||||||
|
|
|
@ -70,9 +70,7 @@ class ResourceStorageCollection(collection.StorageCollection):
|
||||||
|
|
||||||
async def _async_load_data(self) -> dict | None:
|
async def _async_load_data(self) -> dict | None:
|
||||||
"""Load the data."""
|
"""Load the data."""
|
||||||
data = await self.store.async_load()
|
if (data := await self.store.async_load()) is not None:
|
||||||
|
|
||||||
if data is not None:
|
|
||||||
return cast(Optional[dict], data)
|
return cast(Optional[dict], data)
|
||||||
|
|
||||||
# Import it from config.
|
# Import it from config.
|
||||||
|
|
|
@ -427,8 +427,7 @@ class ManualAlarm(alarm.AlarmControlPanelEntity, RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Run when entity about to be added to hass."""
|
"""Run when entity about to be added to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
if (
|
if (
|
||||||
state.state in (STATE_ALARM_PENDING, STATE_ALARM_ARMING)
|
state.state in (STATE_ALARM_PENDING, STATE_ALARM_ARMING)
|
||||||
and hasattr(state, "attributes")
|
and hasattr(state, "attributes")
|
||||||
|
|
|
@ -130,9 +130,7 @@ class OwnTracksEntity(TrackerEntity, RestoreEntity):
|
||||||
if self._data:
|
if self._data:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if (state := await self.async_get_last_state()) is None:
|
||||||
|
|
||||||
if state is None:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
attr = state.attributes
|
attr = state.attributes
|
||||||
|
|
|
@ -420,8 +420,7 @@ class Person(RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register device trackers."""
|
"""Register device trackers."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
self._parse_source_state(state)
|
self._parse_source_state(state)
|
||||||
|
|
||||||
if self.hass.is_running:
|
if self.hass.is_running:
|
||||||
|
|
|
@ -86,8 +86,7 @@ class PilightBaseDevice(RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Call when entity about to be added to hass."""
|
"""Call when entity about to be added to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
self._is_on = state.state == STATE_ON
|
self._is_on = state.state == STATE_ON
|
||||||
self._brightness = state.attributes.get("brightness")
|
self._brightness = state.attributes.get("brightness")
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
|
||||||
async def reload_service_handler(service):
|
async def reload_service_handler(service):
|
||||||
"""Remove all user-defined groups and load new ones from config."""
|
"""Remove all user-defined groups and load new ones from config."""
|
||||||
conf = await component.async_prepare_reload()
|
if (conf := await component.async_prepare_reload()) is None:
|
||||||
if conf is None:
|
|
||||||
return
|
return
|
||||||
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
|
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
|
||||||
_async_setup_shared_data(hass)
|
_async_setup_shared_data(hass)
|
||||||
|
|
|
@ -117,8 +117,7 @@ class PwmSimpleLed(LightEntity, RestoreEntity):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Handle entity about to be added to hass event."""
|
"""Handle entity about to be added to hass event."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
last_state = await self.async_get_last_state()
|
if last_state := await self.async_get_last_state():
|
||||||
if last_state:
|
|
||||||
self._is_on = last_state.state == STATE_ON
|
self._is_on = last_state.state == STATE_ON
|
||||||
self._brightness = last_state.attributes.get(
|
self._brightness = last_state.attributes.get(
|
||||||
"brightness", DEFAULT_BRIGHTNESS
|
"brightness", DEFAULT_BRIGHTNESS
|
||||||
|
@ -193,8 +192,7 @@ class PwmRgbLed(PwmSimpleLed):
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Handle entity about to be added to hass event."""
|
"""Handle entity about to be added to hass event."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
last_state = await self.async_get_last_state()
|
if last_state := await self.async_get_last_state():
|
||||||
if last_state:
|
|
||||||
self._color = last_state.attributes.get("hs_color", DEFAULT_COLOR)
|
self._color = last_state.attributes.get("hs_color", DEFAULT_COLOR)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -211,8 +211,7 @@ async def setup_smartapp_endpoint(hass: HomeAssistant):
|
||||||
|
|
||||||
# Get/create config to store a unique id for this hass instance.
|
# Get/create config to store a unique id for this hass instance.
|
||||||
store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
|
store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
|
||||||
config = await store.async_load()
|
if not (config := await store.async_load()):
|
||||||
if not config:
|
|
||||||
# Create config
|
# Create config
|
||||||
config = {
|
config = {
|
||||||
CONF_INSTANCE_ID: str(uuid4()),
|
CONF_INSTANCE_ID: str(uuid4()),
|
||||||
|
|
|
@ -100,6 +100,5 @@ class SpeedtestSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Handle entity which will be added."""
|
"""Handle entity which will be added."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
self._state = state.state
|
self._state = state.state
|
||||||
|
|
|
@ -117,8 +117,7 @@ class SwitchBotBotEntity(SwitchbotEntity, SwitchEntity, RestoreEntity):
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Run when entity about to be added."""
|
"""Run when entity about to be added."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
last_state = await self.async_get_last_state()
|
if not (last_state := await self.async_get_last_state()):
|
||||||
if not last_state:
|
|
||||||
return
|
return
|
||||||
self._attr_is_on = last_state.state == STATE_ON
|
self._attr_is_on = last_state.state == STATE_ON
|
||||||
self._last_run_success = last_state.attributes["last_run_success"]
|
self._last_run_success = last_state.attributes["last_run_success"]
|
||||||
|
|
|
@ -148,8 +148,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
|
||||||
|
|
||||||
# restore state after startup
|
# restore state after startup
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
self._state = state.state == STATE_ON
|
self._state = state.state == STATE_ON
|
||||||
|
|
||||||
# no need to listen for events
|
# no need to listen for events
|
||||||
|
|
|
@ -402,8 +402,7 @@ class TraccarEntity(TrackerEntity, RestoreEntity):
|
||||||
if self._latitude is not None or self._longitude is not None:
|
if self._latitude is not None or self._longitude is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if (state := await self.async_get_last_state()) is None:
|
||||||
if state is None:
|
|
||||||
self._latitude = None
|
self._latitude = None
|
||||||
self._longitude = None
|
self._longitude = None
|
||||||
self._accuracy = None
|
self._accuracy = None
|
||||||
|
|
|
@ -297,8 +297,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
|
||||||
|
|
||||||
async_dispatcher_connect(self.hass, SIGNAL_RESET_METER, self.async_reset_meter)
|
async_dispatcher_connect(self.hass, SIGNAL_RESET_METER, self.async_reset_meter)
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
if state := await self.async_get_last_state():
|
||||||
if state:
|
|
||||||
try:
|
try:
|
||||||
self._state = Decimal(state.state)
|
self._state = Decimal(state.state)
|
||||||
except InvalidOperation:
|
except InvalidOperation:
|
||||||
|
|
|
@ -73,8 +73,7 @@ class WebSocketHandler:
|
||||||
# Exceptions if Socket disconnected or cancelled by connection handler
|
# Exceptions if Socket disconnected or cancelled by connection handler
|
||||||
with suppress(RuntimeError, ConnectionResetError, *CANCELLATION_ERRORS):
|
with suppress(RuntimeError, ConnectionResetError, *CANCELLATION_ERRORS):
|
||||||
while not self.wsock.closed:
|
while not self.wsock.closed:
|
||||||
message = await self._to_write.get()
|
if (message := await self._to_write.get()) is None:
|
||||||
if message is None:
|
|
||||||
break
|
break
|
||||||
|
|
||||||
self._logger.debug("Sending %s", message)
|
self._logger.debug("Sending %s", message)
|
||||||
|
|
|
@ -37,10 +37,8 @@ async def async_migrator(
|
||||||
|
|
||||||
async def old_conf_migrate_func(old_data)
|
async def old_conf_migrate_func(old_data)
|
||||||
"""
|
"""
|
||||||
store_data = await store.async_load()
|
|
||||||
|
|
||||||
# If we already have store data we have already migrated in the past.
|
# If we already have store data we have already migrated in the past.
|
||||||
if store_data is not None:
|
if (store_data := await store.async_load()) is not None:
|
||||||
return store_data
|
return store_data
|
||||||
|
|
||||||
def load_old_config():
|
def load_old_config():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue