Use dict.get instead of inline if (#109693)

This commit is contained in:
Marc Mueller 2024-02-05 13:56:10 +01:00 committed by GitHub
parent 30710815f0
commit bf8bd5ff21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 15 additions and 29 deletions

View File

@ -39,7 +39,7 @@ class IHCDevice(Entity):
self.ihc_name = product["name"] self.ihc_name = product["name"]
self.ihc_note = product["note"] self.ihc_note = product["note"]
self.ihc_position = product["position"] self.ihc_position = product["position"]
self.suggested_area = product["group"] if "group" in product else None self.suggested_area = product.get("group")
if "id" in product: if "id" in product:
product_id = product["id"] product_id = product["id"]
self.device_id = f"{controller_id}_{product_id }" self.device_id = f"{controller_id}_{product_id }"

View File

@ -90,7 +90,7 @@ class LGDevice(MediaPlayerEntity):
def handle_event(self, response): def handle_event(self, response):
"""Handle responses from the speakers.""" """Handle responses from the speakers."""
data = response["data"] if "data" in response else {} data = response.get("data") or {}
if response["msg"] == "EQ_VIEW_INFO": if response["msg"] == "EQ_VIEW_INFO":
if "i_bass" in data: if "i_bass" in data:
self._bass = data["i_bass"] self._bass = data["i_bass"]

View File

@ -172,9 +172,7 @@ async def async_modbus_setup(
slave = int(float(service.data[ATTR_SLAVE])) slave = int(float(service.data[ATTR_SLAVE]))
address = int(float(service.data[ATTR_ADDRESS])) address = int(float(service.data[ATTR_ADDRESS]))
value = service.data[ATTR_VALUE] value = service.data[ATTR_VALUE]
hub = hub_collect[ hub = hub_collect[service.data.get(ATTR_HUB, DEFAULT_HUB)]
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
if isinstance(value, list): if isinstance(value, list):
await hub.async_pb_call( await hub.async_pb_call(
slave, slave,
@ -196,9 +194,7 @@ async def async_modbus_setup(
slave = int(float(service.data[ATTR_SLAVE])) slave = int(float(service.data[ATTR_SLAVE]))
address = service.data[ATTR_ADDRESS] address = service.data[ATTR_ADDRESS]
state = service.data[ATTR_STATE] state = service.data[ATTR_STATE]
hub = hub_collect[ hub = hub_collect[service.data.get(ATTR_HUB, DEFAULT_HUB)]
service.data[ATTR_HUB] if ATTR_HUB in service.data else DEFAULT_HUB
]
if isinstance(state, list): if isinstance(state, list):
await hub.async_pb_call(slave, address, state, CALL_TYPE_WRITE_COILS) await hub.async_pb_call(slave, address, state, CALL_TYPE_WRITE_COILS)
else: else:

View File

@ -544,7 +544,7 @@ async def websocket_subscribe(
) )
# Perform UTF-8 decoding directly in callback routine # Perform UTF-8 decoding directly in callback routine
qos: int = msg["qos"] if "qos" in msg else DEFAULT_QOS qos: int = msg.get("qos", DEFAULT_QOS)
connection.subscriptions[msg["id"]] = await async_subscribe( connection.subscriptions[msg["id"]] = await async_subscribe(
hass, msg["topic"], forward_messages, encoding=None, qos=qos hass, msg["topic"], forward_messages, encoding=None, qos=qos
) )

View File

@ -646,8 +646,7 @@ class MqttAvailability(Entity):
self._available_latest = False self._available_latest = False
self._available = { self._available = {
topic: (self._available[topic] if topic in self._available else False) topic: (self._available.get(topic, False)) for topic in self._avail_topics
for topic in self._avail_topics
} }
topics: dict[str, dict[str, Any]] = { topics: dict[str, dict[str, Any]] = {
f"availability_{topic}": { f"availability_{topic}": {

View File

@ -159,12 +159,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Load the data.""" """Load the data."""
self._data = config self._data = config
self._data[CONF_PORT] = ( self._data[CONF_PORT] = self._data.get(CONF_PORT, DEFAULT_PORT)
self._data[CONF_PORT] if CONF_PORT in self._data else DEFAULT_PORT self._data[CONF_ON_ACTION] = self._data.get(CONF_ON_ACTION)
)
self._data[CONF_ON_ACTION] = (
self._data[CONF_ON_ACTION] if CONF_ON_ACTION in self._data else None
)
await self.async_set_unique_id(self._data[CONF_HOST]) await self.async_set_unique_id(self._data[CONF_HOST])
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()

View File

@ -44,7 +44,7 @@ class StarlineDeviceTracker(StarlineEntity, TrackerEntity, RestoreEntity):
@property @property
def location_accuracy(self): def location_accuracy(self):
"""Return the gps accuracy of the device.""" """Return the gps accuracy of the device."""
return self._device.position["r"] if "r" in self._device.position else 0 return self._device.position.get("r", 0)
@property @property
def latitude(self): def latitude(self):

View File

@ -602,12 +602,8 @@ class TelegramNotificationService:
if keys: if keys:
params[ATTR_REPLYMARKUP] = ReplyKeyboardMarkup( params[ATTR_REPLYMARKUP] = ReplyKeyboardMarkup(
[[key.strip() for key in row.split(",")] for row in keys], [[key.strip() for key in row.split(",")] for row in keys],
resize_keyboard=data[ATTR_RESIZE_KEYBOARD] resize_keyboard=data.get(ATTR_RESIZE_KEYBOARD, False),
if ATTR_RESIZE_KEYBOARD in data one_time_keyboard=data.get(ATTR_ONE_TIME_KEYBOARD, False),
else False,
one_time_keyboard=data[ATTR_ONE_TIME_KEYBOARD]
if ATTR_ONE_TIME_KEYBOARD in data
else False,
) )
else: else:
params[ATTR_REPLYMARKUP] = ReplyKeyboardRemove(True) params[ATTR_REPLYMARKUP] = ReplyKeyboardRemove(True)

View File

@ -56,13 +56,12 @@ def _get_config_schema(
vol.Required(CONF_API_KEY, default=input_dict.get(CONF_API_KEY)): str, vol.Required(CONF_API_KEY, default=input_dict.get(CONF_API_KEY)): str,
} }
default_location = ( default_location = input_dict.get(
input_dict[CONF_LOCATION] CONF_LOCATION,
if CONF_LOCATION in input_dict {
else {
CONF_LATITUDE: hass.config.latitude, CONF_LATITUDE: hass.config.latitude,
CONF_LONGITUDE: hass.config.longitude, CONF_LONGITUDE: hass.config.longitude,
} },
) )
return vol.Schema( return vol.Schema(
{ {