Cleanup rainbird controller setup as feedback from previous PR (#84504)

Additional rainbird cleanup from last PR
pull/84525/head
Allen Porter 2022-12-23 19:54:25 -08:00 committed by GitHub
parent 55b46bfa7a
commit 1807f44e03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 22 deletions

View File

@ -36,7 +36,6 @@ PLATFORMS = [Platform.SWITCH, Platform.SENSOR, Platform.BINARY_SENSOR]
_LOGGER = logging.getLogger(__name__)
DATA_RAINBIRD = "rainbird"
DOMAIN = "rainbird"
TRIGGER_TIME_SCHEMA = vol.All(
@ -65,13 +64,14 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Rain Bird component."""
hass.data[DATA_RAINBIRD] = []
tasks = []
for controller_config in config[DOMAIN]:
tasks.append(_setup_controller(hass, controller_config, config))
return all(await asyncio.gather(*tasks))
return all(
await asyncio.gather(
*[
_setup_controller(hass, controller_config, config)
for controller_config in config[DOMAIN]
]
)
)
async def _setup_controller(hass, controller_config, config):
@ -80,20 +80,18 @@ async def _setup_controller(hass, controller_config, config):
password = controller_config[CONF_PASSWORD]
client = AsyncRainbirdClient(async_get_clientsession(hass), server, password)
controller = AsyncRainbirdController(client)
position = len(hass.data[DATA_RAINBIRD])
try:
await controller.get_serial_number()
except RainbirdApiException as exc:
_LOGGER.error("Unable to setup controller: %s", exc)
return False
hass.data[DATA_RAINBIRD].append(controller)
rain_coordinator = RainbirdUpdateCoordinator(hass, controller.get_rain_sensor_state)
delay_coordinator = RainbirdUpdateCoordinator(hass, controller.get_rain_delay)
_LOGGER.debug("Rain Bird Controller %d set to: %s", position, server)
for platform in PLATFORMS:
discovery.load_platform(
hass.async_create_task(
discovery.async_load_platform(
hass,
platform,
DOMAIN,
@ -105,4 +103,5 @@ async def _setup_controller(hass, controller_config, config):
},
config,
)
)
return True