From 111ffdd77e79ec5ec662a10ba3deb069d1a65ac8 Mon Sep 17 00:00:00 2001 From: Federico D'Amico <48856240+FedDam@users.noreply.github.com> Date: Mon, 8 Apr 2024 05:35:46 +0200 Subject: [PATCH] Improve microBees code quality (#114939) microBees code quality --- homeassistant/components/microbees/cover.py | 14 +++++++++++--- homeassistant/components/microbees/light.py | 16 ++++++++-------- homeassistant/components/microbees/strings.json | 5 ++++- homeassistant/components/microbees/switch.py | 16 ++++++++-------- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/microbees/cover.py b/homeassistant/components/microbees/cover.py index bdf6e815af1..b6d5d366d89 100644 --- a/homeassistant/components/microbees/cover.py +++ b/homeassistant/components/microbees/cover.py @@ -19,6 +19,8 @@ from .const import DOMAIN from .coordinator import MicroBeesUpdateCoordinator from .entity import MicroBeesEntity +COVER_IDS = {47: "roller_shutter"} + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback @@ -32,11 +34,17 @@ async def async_setup_entry( MBCover( coordinator, bee_id, - next(filter(lambda x: x.deviceID == 551, bee.actuators)).id, - next(filter(lambda x: x.deviceID == 552, bee.actuators)).id, + next( + (actuator.id for actuator in bee.actuators if actuator.deviceID == 551), + None, + ), + next( + (actuator.id for actuator in bee.actuators if actuator.deviceID == 552), + None, + ), ) for bee_id, bee in coordinator.data.bees.items() - if bee.productID == 47 + if bee.productID in COVER_IDS ) diff --git a/homeassistant/components/microbees/light.py b/homeassistant/components/microbees/light.py index 411eab22324..654cdc37182 100644 --- a/homeassistant/components/microbees/light.py +++ b/homeassistant/components/microbees/light.py @@ -60,19 +60,19 @@ class MBLight(MicroBeesActuatorEntity, LightEntity): sendCommand = await self.coordinator.microbees.sendCommand( self.actuator_id, 1, color=self._attr_rgbw_color ) - if sendCommand: - self.actuator.value = True - self.async_write_ha_state() - else: + if not sendCommand: raise HomeAssistantError(f"Failed to turn on {self.name}") + self.actuator.value = True + self.async_write_ha_state() + async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the light.""" sendCommand = await self.coordinator.microbees.sendCommand( self.actuator_id, 0, color=self._attr_rgbw_color ) - if sendCommand: - self.actuator.value = False - self.async_write_ha_state() - else: + if not sendCommand: raise HomeAssistantError(f"Failed to turn off {self.name}") + + self.actuator.value = False + self.async_write_ha_state() diff --git a/homeassistant/components/microbees/strings.json b/homeassistant/components/microbees/strings.json index 6f17a12834e..49d42af83d3 100644 --- a/homeassistant/components/microbees/strings.json +++ b/homeassistant/components/microbees/strings.json @@ -19,7 +19,10 @@ "missing_configuration": "[%key:common::config_flow::abort::oauth2_missing_configuration%]", "authorize_url_timeout": "[%key:common::config_flow::abort::oauth2_authorize_url_timeout%]", "no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]", - "user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]" + "user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "unknown": "[%key:common::config_flow::error::unknown%]", + "wrong_account": "You can only reauthenticate this entry with the same microBees account." }, "create_entry": { "default": "[%key:common::config_flow::create_entry::authenticated%]" diff --git a/homeassistant/components/microbees/switch.py b/homeassistant/components/microbees/switch.py index 8e3c03e9ba4..1d668d041e1 100644 --- a/homeassistant/components/microbees/switch.py +++ b/homeassistant/components/microbees/switch.py @@ -56,17 +56,17 @@ class MBSwitch(MicroBeesActuatorEntity, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" send_command = await self.coordinator.microbees.sendCommand(self.actuator_id, 1) - if send_command: - self.actuator.value = True - self.async_write_ha_state() - else: + if not send_command: raise HomeAssistantError(f"Failed to turn on {self.name}") + self.actuator.value = True + self.async_write_ha_state() + async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" send_command = await self.coordinator.microbees.sendCommand(self.actuator_id, 0) - if send_command: - self.actuator.value = False - self.async_write_ha_state() - else: + if not send_command: raise HomeAssistantError(f"Failed to turn off {self.name}") + + self.actuator.value = False + self.async_write_ha_state()