Skip to content

Commit

Permalink
change username to email (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdonenfeld authored Jul 1, 2024
1 parent 66e9cd3 commit cebe3cc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
10 changes: 5 additions & 5 deletions custom_components/aerogarden/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant

from .api import AerogardenAPI
from .const import CONF_PASSWORD, CONF_USERNAME, DEFAULT_HOST, DOMAIN
from .const import DEFAULT_HOST, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand All @@ -18,18 +18,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Aerogarden from a config entry."""
hass.data.setdefault(DOMAIN, {})

username = entry.data[CONF_USERNAME]
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]

# Use the username and password to set up aerogarden

# If the setup is successful:
hass.data[DOMAIN][entry.entry_id] = {
"username": username,
"email": email,
"password": password,
}

ag = AerogardenAPI(hass, username, password, DEFAULT_HOST)
ag = AerogardenAPI(hass, email, password, DEFAULT_HOST)
if not ag.is_valid_login():
_LOGGER.error("Invalid login: %s" % ag.error)
return False
Expand Down
8 changes: 4 additions & 4 deletions custom_components/aerogarden/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@


class AerogardenAPI:
def __init__(self, hass: HomeAssistant, username: str, password: str, host: str):
def __init__(self, hass: HomeAssistant, email: str, password: str, host: str):
self._hass = hass
self._username = username
self._email = email
self._password = password
self._host = host
self._userid: Optional[str] = None
Expand All @@ -39,7 +39,7 @@ def error(self) -> Optional[str]:
return self._error_msg

async def login(self) -> bool:
post_data = {"mail": self._username, "userPwd": self._password}
post_data = {"mail": self._email, "userPwd": self._password}
response = await self._post_request(self._login_url, post_data)

if not response:
Expand All @@ -55,7 +55,7 @@ async def login(self) -> bool:
_LOGGER.error(self._error_msg)
return False
elif userid == -4:
self._error_msg = f"Invalid Credentials for {self._username}"
self._error_msg = f"Invalid Credentials for {self._email}"
_LOGGER.error(self._error_msg)
return False
else:
Expand Down
21 changes: 11 additions & 10 deletions custom_components/aerogarden/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,45 @@

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import ConfigEntryNotReady

from .api import AerogardenAPI
from .const import CONF_PASSWORD, CONF_USERNAME, DEFAULT_HOST, DOMAIN
from .const import DEFAULT_HOST, DOMAIN

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_EMAIL): str,
vol.Required(CONF_PASSWORD): str,
}
)


class AerogardenConfigFlow(config_entries.ConfigFlow):
class AerogardenConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1

async def async_step_user(
self, user_input: Optional[Dict[str, Any]] = None
) -> FlowResult:
errors = {}
if user_input is not None:
username = user_input[CONF_USERNAME]
email = user_input[CONF_EMAIL]
password = user_input[CONF_PASSWORD]

ag = AerogardenAPI(self.hass, username, password, DEFAULT_HOST)
ag = AerogardenAPI(self.hass, email, password, DEFAULT_HOST)
try:
is_valid = await ag.login()
if is_valid:
# Check if this username is already configured
await self.async_set_unique_id(username)
# Check if this email is already configured
await self.async_set_unique_id(email)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=f"Aerogarden ({username})", data=user_input
title=f"Aerogarden ({email})", data=user_input
)
else:
errors["base"] = "invalid_auth"
Expand All @@ -59,10 +60,10 @@ async def async_setup_entry(
hass: HomeAssistant, entry: config_entries.ConfigEntry
) -> bool:
"""Set up Aerogarden from a config entry."""
username = entry.data[CONF_USERNAME]
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]

ag = AerogardenAPI(hass, username, password, DEFAULT_HOST)
ag = AerogardenAPI(hass, email, password, DEFAULT_HOST)

try:
is_valid = await ag.login()
Expand Down
2 changes: 0 additions & 2 deletions custom_components/aerogarden/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
DOMAIN: Final = "aerogarden"
MANUFACTURER: Final = "Aerogarden"
UPDATE_INTERVAL: float = 30.0
CONF_USERNAME: str = ""
CONF_PASSWORD = ""
2 changes: 1 addition & 1 deletion custom_components/aerogarden/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Identify the device",
"description": "Provide Aerogarden account credentials",
"data": {
"ip_address": "[%key:common::config_flow::data::ip%]",
"email": "[%key:common::config_flow::data::email%]",
"password": "[%key:common::config_flow::data::password%]"
},
"data_description": {
Expand Down

0 comments on commit cebe3cc

Please sign in to comment.