diff --git a/homeassistant/components/blackbird/media_player.py b/homeassistant/components/blackbird/media_player.py index 5e6e996c7dd..daa23553c96 100644 --- a/homeassistant/components/blackbird/media_player.py +++ b/homeassistant/components/blackbird/media_player.py @@ -164,10 +164,7 @@ class BlackbirdZone(MediaPlayerEntity): return self._attr_state = MediaPlayerState.ON if state.power else MediaPlayerState.OFF idx = state.av - if idx in self._source_id_name: - self._attr_source = self._source_id_name[idx] - else: - self._attr_source = None + self._attr_source = self._source_id_name.get(idx) @property def media_title(self): diff --git a/homeassistant/components/blinksticklight/light.py b/homeassistant/components/blinksticklight/light.py index 8373d2990a1..91d3d7d1b96 100644 --- a/homeassistant/components/blinksticklight/light.py +++ b/homeassistant/components/blinksticklight/light.py @@ -71,16 +71,14 @@ class BlinkStickLight(LightEntity): """Turn the device on.""" if ATTR_HS_COLOR in kwargs: self._attr_hs_color = kwargs[ATTR_HS_COLOR] - if ATTR_BRIGHTNESS in kwargs: - self._attr_brightness = kwargs[ATTR_BRIGHTNESS] - else: - self._attr_brightness = 255 - assert self.brightness is not None - self._attr_is_on = self.brightness > 0 + + brightness: int = kwargs.get(ATTR_BRIGHTNESS, 255) + self._attr_brightness = brightness + self._attr_is_on = bool(brightness) assert self.hs_color rgb_color = color_util.color_hsv_to_RGB( - self.hs_color[0], self.hs_color[1], self.brightness / 255 * 100 + self.hs_color[0], self.hs_color[1], brightness / 255 * 100 ) self._stick.set_color(red=rgb_color[0], green=rgb_color[1], blue=rgb_color[2]) diff --git a/homeassistant/components/google_assistant/trait.py b/homeassistant/components/google_assistant/trait.py index 71abdf1758d..af203906b86 100644 --- a/homeassistant/components/google_assistant/trait.py +++ b/homeassistant/components/google_assistant/trait.py @@ -1333,10 +1333,7 @@ class ArmDisArmTrait(_Trait): def query_attributes(self): """Return ArmDisarm query attributes.""" - if "next_state" in self.state.attributes: - armed_state = self.state.attributes["next_state"] - else: - armed_state = self.state.state + armed_state = self.state.attributes.get("next_state", self.state.state) response = {"isArmed": armed_state in self.state_to_service} if response["isArmed"]: response.update({"currentArmLevel": armed_state}) diff --git a/homeassistant/components/hdmi_cec/__init__.py b/homeassistant/components/hdmi_cec/__init__.py index 05d5ab57841..18885b81be2 100644 --- a/homeassistant/components/hdmi_cec/__init__.py +++ b/homeassistant/components/hdmi_cec/__init__.py @@ -261,14 +261,8 @@ def setup(hass: HomeAssistant, base_config: ConfigType) -> bool: # noqa: C901 if ATTR_RAW in data: command = CecCommand(data[ATTR_RAW]) else: - if ATTR_SRC in data: - src = data[ATTR_SRC] - else: - src = ADDR_UNREGISTERED - if ATTR_DST in data: - dst = data[ATTR_DST] - else: - dst = ADDR_BROADCAST + src = data.get(ATTR_SRC, ADDR_UNREGISTERED) + dst = data.get(ATTR_DST, ADDR_BROADCAST) if ATTR_CMD in data: cmd = data[ATTR_CMD] else: @@ -374,10 +368,7 @@ class CecEntity(Entity): self._logical_address = logical self.entity_id = "%s.%d" % (DOMAIN, self._logical_address) self._set_attr_name() - if self._device.type in ICONS_BY_TYPE: - self._attr_icon = ICONS_BY_TYPE[self._device.type] - else: - self._attr_icon = ICON_UNKNOWN + self._attr_icon = ICONS_BY_TYPE.get(self._device.type, ICON_UNKNOWN) def _set_attr_name(self): """Set name.""" diff --git a/homeassistant/components/light/device_action.py b/homeassistant/components/light/device_action.py index 2583bfa972f..2b4c32cb3b1 100644 --- a/homeassistant/components/light/device_action.py +++ b/homeassistant/components/light/device_action.py @@ -77,10 +77,7 @@ async def async_call_action_from_config( data[ATTR_BRIGHTNESS_PCT] = config[ATTR_BRIGHTNESS_PCT] if config[CONF_TYPE] == TYPE_FLASH: - if ATTR_FLASH in config: - data[ATTR_FLASH] = config[ATTR_FLASH] - else: - data[ATTR_FLASH] = FLASH_SHORT + data[ATTR_FLASH] = config.get(ATTR_FLASH, FLASH_SHORT) await hass.services.async_call( DOMAIN, SERVICE_TURN_ON, data, blocking=True, context=context diff --git a/homeassistant/components/monoprice/media_player.py b/homeassistant/components/monoprice/media_player.py index f6150ab1d22..52e33da54ed 100644 --- a/homeassistant/components/monoprice/media_player.py +++ b/homeassistant/components/monoprice/media_player.py @@ -165,10 +165,7 @@ class MonopriceZone(MediaPlayerEntity): self._attr_volume_level = state.volume / MAX_VOLUME self._attr_is_volume_muted = state.mute idx = state.source - if idx in self._source_id_name: - self._attr_source = self._source_id_name[idx] - else: - self._attr_source = None + self._attr_source = self._source_id_name.get(idx) @property def entity_registry_enabled_default(self) -> bool: diff --git a/homeassistant/components/owntracks/messages.py b/homeassistant/components/owntracks/messages.py index b31fbba8c8e..df61aa6e968 100644 --- a/homeassistant/components/owntracks/messages.py +++ b/homeassistant/components/owntracks/messages.py @@ -332,10 +332,7 @@ async def async_handle_waypoints_message(hass, context, message): if user not in context.waypoint_whitelist: return - if "waypoints" in message: - wayps = message["waypoints"] - else: - wayps = [message] + wayps = message.get("waypoints", [message]) _LOGGER.info("Got %d waypoints from %s", len(wayps), message["topic"]) diff --git a/pyproject.toml b/pyproject.toml index 9f16822970f..bdc04254ea9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -252,6 +252,7 @@ select = [ "PGH004", # Use specific rule codes when using noqa "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass "SIM117", # Merge with-statements that use the same scope + "SIM401", # Use get from dict with default instead of an if block "T20", # flake8-print "UP", # pyupgrade "W", # pycodestyle