Small cleanups to rachio (#49404)
- Remove unused async_step - Reduce async callbacks from executor
This commit is contained in:
parent
e98f27ead6
commit
0b26294fb0
3 changed files with 53 additions and 55 deletions
|
@ -26,14 +26,6 @@ PLATFORMS = ["switch", "binary_sensor"]
|
|||
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
"""Set up the rachio component from YAML."""
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Unload a config entry."""
|
||||
unload_ok = all(
|
||||
|
@ -84,7 +76,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
|
||||
# Get the API user
|
||||
try:
|
||||
await hass.async_add_executor_job(person.setup, hass)
|
||||
await person.async_setup(hass)
|
||||
except ConnectTimeout as error:
|
||||
_LOGGER.error("Could not reach the Rachio API: %s", error)
|
||||
raise ConfigEntryNotReady from error
|
||||
|
@ -100,6 +92,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
)
|
||||
|
||||
# Enable platform
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = person
|
||||
async_register_webhook(hass, webhook_id, entry.entry_id)
|
||||
|
||||
|
|
|
@ -57,23 +57,65 @@ class RachioPerson:
|
|||
self._id = None
|
||||
self._controllers = []
|
||||
|
||||
def setup(self, hass):
|
||||
"""Rachio device setup."""
|
||||
all_devices = []
|
||||
async def async_setup(self, hass):
|
||||
"""Create rachio devices and services."""
|
||||
await hass.async_add_executor_job(self._setup, hass)
|
||||
can_pause = False
|
||||
response = self.rachio.person.info()
|
||||
for rachio_iro in self._controllers:
|
||||
# Generation 1 controllers don't support pause or resume
|
||||
if rachio_iro.model.split("_")[0] != MODEL_GENERATION_1:
|
||||
can_pause = True
|
||||
break
|
||||
|
||||
if not can_pause:
|
||||
return
|
||||
|
||||
all_devices = [rachio_iro.name for rachio_iro in self._controllers]
|
||||
|
||||
def pause_water(service):
|
||||
"""Service to pause watering on all or specific controllers."""
|
||||
duration = service.data[ATTR_DURATION]
|
||||
devices = service.data.get(ATTR_DEVICES, all_devices)
|
||||
for iro in self._controllers:
|
||||
if iro.name in devices:
|
||||
iro.pause_watering(duration)
|
||||
|
||||
def resume_water(service):
|
||||
"""Service to resume watering on all or specific controllers."""
|
||||
devices = service.data.get(ATTR_DEVICES, all_devices)
|
||||
for iro in self._controllers:
|
||||
if iro.name in devices:
|
||||
iro.resume_watering()
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_PAUSE_WATERING,
|
||||
pause_water,
|
||||
schema=PAUSE_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_RESUME_WATERING,
|
||||
resume_water,
|
||||
schema=RESUME_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
def _setup(self, hass):
|
||||
"""Rachio device setup."""
|
||||
rachio = self.rachio
|
||||
|
||||
response = rachio.person.info()
|
||||
assert int(response[0][KEY_STATUS]) == HTTP_OK, "API key error"
|
||||
self._id = response[1][KEY_ID]
|
||||
|
||||
# Use user ID to get user data
|
||||
data = self.rachio.person.get(self._id)
|
||||
data = rachio.person.get(self._id)
|
||||
assert int(data[0][KEY_STATUS]) == HTTP_OK, "User ID error"
|
||||
self.username = data[1][KEY_USERNAME]
|
||||
devices = data[1][KEY_DEVICES]
|
||||
for controller in devices:
|
||||
webhooks = self.rachio.notification.get_device_webhook(controller[KEY_ID])[
|
||||
1
|
||||
]
|
||||
webhooks = rachio.notification.get_device_webhook(controller[KEY_ID])[1]
|
||||
# The API does not provide a way to tell if a controller is shared
|
||||
# or if they are the owner. To work around this problem we fetch the webooks
|
||||
# before we setup the device so we can skip it instead of failing.
|
||||
|
@ -94,46 +136,12 @@ class RachioPerson:
|
|||
)
|
||||
continue
|
||||
|
||||
rachio_iro = RachioIro(hass, self.rachio, controller, webhooks)
|
||||
rachio_iro = RachioIro(hass, rachio, controller, webhooks)
|
||||
rachio_iro.setup()
|
||||
self._controllers.append(rachio_iro)
|
||||
all_devices.append(rachio_iro.name)
|
||||
# Generation 1 controllers don't support pause or resume
|
||||
if rachio_iro.model.split("_")[0] != MODEL_GENERATION_1:
|
||||
can_pause = True
|
||||
|
||||
_LOGGER.info('Using Rachio API as user "%s"', self.username)
|
||||
|
||||
def pause_water(service):
|
||||
"""Service to pause watering on all or specific controllers."""
|
||||
duration = service.data[ATTR_DURATION]
|
||||
devices = service.data.get(ATTR_DEVICES, all_devices)
|
||||
for iro in self._controllers:
|
||||
if iro.name in devices:
|
||||
iro.pause_watering(duration)
|
||||
|
||||
def resume_water(service):
|
||||
"""Service to resume watering on all or specific controllers."""
|
||||
devices = service.data.get(ATTR_DEVICES, all_devices)
|
||||
for iro in self._controllers:
|
||||
if iro.name in devices:
|
||||
iro.resume_watering()
|
||||
|
||||
if can_pause:
|
||||
hass.services.register(
|
||||
DOMAIN,
|
||||
SERVICE_PAUSE_WATERING,
|
||||
pause_water,
|
||||
schema=PAUSE_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
hass.services.register(
|
||||
DOMAIN,
|
||||
SERVICE_RESUME_WATERING,
|
||||
resume_water,
|
||||
schema=RESUME_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
@property
|
||||
def user_id(self) -> str:
|
||||
"""Get the user ID as defined by the Rachio API."""
|
||||
|
|
|
@ -38,8 +38,6 @@ async def test_form(hass):
|
|||
with patch(
|
||||
"homeassistant.components.rachio.config_flow.Rachio", return_value=rachio_mock
|
||||
), patch(
|
||||
"homeassistant.components.rachio.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.rachio.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
|
@ -60,7 +58,6 @@ async def test_form(hass):
|
|||
CONF_CUSTOM_URL: "http://custom.url",
|
||||
CONF_MANUAL_RUN_MINS: 5,
|
||||
}
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue