Skip to content

Commit

Permalink
refactor to const.py (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielhiversen authored Sep 30, 2020
1 parent 4041295 commit 2dcf5ee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
22 changes: 8 additions & 14 deletions custom_components/adax/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,28 @@
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .adax import Adax
from .const import ACCOUNT_ID

_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required("account_id"): cv.string, vol.Required(CONF_PASSWORD): cv.string}
{vol.Required(ACCOUNT_ID): cv.string, vol.Required(CONF_PASSWORD): cv.string}
)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Adax thermostat."""
account_id = config["account_id"]
password = config[CONF_PASSWORD]

adax_data_handler = Adax(
account_id, password, websession=async_get_clientsession(hass)
)

dev = []
for heater_data in await adax_data_handler.get_rooms():
dev.append(AdaxDevice(heater_data, adax_data_handler))
async_add_entities(dev)
await _setup(hass, config[ACCOUNT_ID], config[CONF_PASSWORD], async_add_entities)


async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Adax thermostat with config flow."""
account_id = entry.data["account_id"]
password = entry.data[CONF_PASSWORD]
await _setup(
hass, entry.data[ACCOUNT_ID], entry.data[CONF_PASSWORD], async_add_entities
)


async def _setup(hass, account_id, password, async_add_entities):
adax_data_handler = Adax(
account_id, password, websession=async_get_clientsession(hass)
)
Expand Down
11 changes: 5 additions & 6 deletions custom_components/adax/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
from homeassistant.const import CONF_PASSWORD
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .adax import get_adax_token

DOMAIN = "adax"
from .const import ACCOUNT_ID, DOMAIN

_LOGGER = logging.getLogger(__name__)

DATA_SCHEMA = vol.Schema(
{vol.Required("account_id"): str, vol.Required(CONF_PASSWORD): str}
{vol.Required(ACCOUNT_ID): str, vol.Required(CONF_PASSWORD): str}
)


async def validate_input(hass: core.HomeAssistant, account_id, password):
"""Validate the user input allows us to connect."""
for entry in hass.config_entries.async_entries(DOMAIN):
if entry.data["account_id"] == account_id:
if entry.data[ACCOUNT_ID] == account_id:
raise AlreadyConfigured

token = await get_adax_token(async_get_clientsession(hass), account_id, password)
Expand All @@ -41,7 +40,7 @@ async def async_step_user(self, user_input=None):

if user_input is not None:
try:
account_id = user_input["account_id"].replace(" ", "")
account_id = user_input[ACCOUNT_ID].replace(" ", "")
password = user_input[CONF_PASSWORD].replace(" ", "")
await validate_input(self.hass, account_id, password)
unique_id = account_id
Expand All @@ -50,7 +49,7 @@ async def async_step_user(self, user_input=None):

return self.async_create_entry(
title=unique_id,
data={"account_id": account_id, CONF_PASSWORD: password},
data={ACCOUNT_ID: account_id, CONF_PASSWORD: password},
)

except AlreadyConfigured:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/adax/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Constants for Adax integration."""

ACCOUNT_ID = "account_id"
DOMAIN = "adax"

0 comments on commit 2dcf5ee

Please sign in to comment.