Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Merge branch 'main' of github.com:DCSBL/ha-homewizard-energy into main
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL committed Feb 14, 2021
2 parents b4f7426 + 276a4ad commit 5495a38
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions custom_components/homewizard_energy/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Config flow for Homewizard Energy."""
import logging
import asyncio

import logging
from typing import Any, Dict, Optional

import aiohwenergy
import async_timeout
import voluptuous as vol
from aiohwenergy.hwenergy import SUPPORTED_DEVICES
from homeassistant import config_entries
Expand All @@ -14,9 +15,6 @@
from voluptuous import All, Length, Required, Schema
from voluptuous.util import Lower

import aiohwenergy
import async_timeout

from .const import (
CONF_IP_ADDRESS,
CONF_OVERRIDE_POLL_INTERVAL,
Expand All @@ -28,6 +26,7 @@

Logger = logging.getLogger(__name__)


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for P1 meter."""

Expand All @@ -43,24 +42,24 @@ def async_get_options_flow(config_entry):
def __init__(self):
"""Set up the instance."""
Logger.debug("config_flow __init__")

async def async_step_user(
self, user_input: Optional[ConfigType] = None
) -> Dict[str, Any]:
"""Handle a flow initiated by the user."""
if user_input is None:
return self._show_setup_form()

# Check if data is IP (Volup?)

# Make connection with device
energy_api = aiohwenergy.HomeWizardEnergy(user_input[CONF_IP_ADDRESS])

initialized = False
try:
with async_timeout.timeout(10):
await energy_api.initialize()
if (energy_api.device != None):
if energy_api.device != None:
initialized = True
except (asyncio.TimeoutError, aiohwenergy.RequestError):
Logger.error(
Expand All @@ -79,17 +78,17 @@ async def async_step_user(
energy_api._host["host"],
)
return self.async_abort(reason="manual_config_unknown_error")

finally:
await energy_api.close()

if not initialized:
return self.async_abort(reason="manual_config_unknown_error")

# Validate metadata
if (energy_api.device.api_version != "v1"):
return self.async_abort(reason="manual_config_unsupported_api_version")
if energy_api.device.api_version != "v1":
return self.async_abort(reason="manual_config_unsupported_api_version")

# Configure device
entry_info = {
"host": user_input[CONF_IP_ADDRESS],
Expand All @@ -100,9 +99,9 @@ async def async_step_user(
"product_type": energy_api.device.product_type,
"serial": energy_api.device.serial,
}

Logger.debug(entry_info)

return await self.async_step_check(entry_info)

def _show_setup_form(self, errors: Optional[Dict] = None) -> Dict[str, Any]:
Expand Down Expand Up @@ -172,7 +171,7 @@ async def async_step_check(self, entry_info):
):
Logger.warning(f"Invalid discovery parameters")
return self.async_abort(reason="invalid_discovery_parameters")

self.context["host"] = entry_info["host"]
self.context["unique_id"] = "%s_%s" % (
entry_info["product_type"],
Expand All @@ -184,15 +183,15 @@ async def async_step_check(self, entry_info):
self.context["product_name"] = entry_info["product_name"]
self.context["product_type"] = entry_info["product_type"]
self.context["api_enabled"] = entry_info["api_enabled"]

self.context["name"] = "%s (%s)" % (
self.context["product_name"],
self.context["serial"][-6:],
)

await self.async_set_unique_id(self.context["unique_id"])
self._abort_if_unique_id_configured(updates=entry_info)
self._abort_if_unique_id_configured(updates=entry_info)

self.context["title_placeholders"] = {
"name": self.context["name"],
"unique_id": self.context["unique_id"],
Expand Down

0 comments on commit 5495a38

Please sign in to comment.