Skip to content

Commit

Permalink
Changes to config flow
Browse files Browse the repository at this point in the history
Added some extra sensors
  • Loading branch information
sockless-coding committed May 16, 2020
1 parent 01bb504 commit 2889aeb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
14 changes: 8 additions & 6 deletions custom_components/garo_wallbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
from homeassistant.const import (
ATTR_NAME,
CONF_HOST,
CONF_PORT,
CONF_SSL,
CONF_VERIFY_SSL,
CONF_NAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand All @@ -43,14 +42,17 @@ async def async_setup(hass: HomeAssistant, config: Dict) -> bool:

async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
conf = entry.data
device = await garo_setup(hass, conf[CONF_HOST])
device = await garo_setup(hass, conf[CONF_HOST], conf[CONF_NAME])
if not device:
return False
hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: device})
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)

"""device_registry = await dr.async_get_registry(hass)
device_registry.async_get_or_create(**device.device_info)"""
return True

async def async_unload_entry(hass, config_entry):
Expand All @@ -66,12 +68,12 @@ async def async_unload_entry(hass, config_entry):
hass.data.pop(DOMAIN)
return True

async def garo_setup(hass, host):
async def garo_setup(hass, host, name):
"""Create a Garo instance only once."""
session = hass.helpers.aiohttp_client.async_get_clientsession()
try:
with timeout(TIMEOUT):
device = GaroDevice(host, session)
device = GaroDevice(host, name, session)
await device.init()
except asyncio.TimeoutError:
_LOGGER.debug("Connection to %s timed out", host)
Expand Down
23 changes: 13 additions & 10 deletions custom_components/garo_wallbox/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_HOST
from homeassistant.const import CONF_HOST, CONF_NAME

from .garo import GaroDevice

Expand All @@ -22,21 +22,21 @@ class FlowHandler(config_entries.ConfigFlow):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

async def _create_entry(self, host):
async def _create_entry(self, host, name):
"""Register new entry."""
# Check if ip already is registered
for entry in self._async_current_entries():
if entry.data[KEY_IP] == host:
return self.async_abort(reason="already_configured")

return self.async_create_entry(title=host, data={CONF_HOST: host})
return self.async_create_entry(title=host, data={CONF_HOST: host, CONF_NAME: name})

async def _create_device(self, host):
async def _create_device(self, host, name):
"""Create device."""

try:
device = GaroDevice(
host, self.hass.helpers.aiohttp_client.async_get_clientsession()
host, name, self.hass.helpers.aiohttp_client.async_get_clientsession()
)
with timeout(TIMEOUT):
await device.init()
Expand All @@ -49,24 +49,27 @@ async def _create_device(self, host):
_LOGGER.exception("Unexpected error creating device")
return self.async_abort(reason="device_fail")

return await self._create_entry(host)
return await self._create_entry(host, name)

async def async_step_user(self, user_input=None):
"""User initiated config flow."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=vol.Schema({vol.Required(CONF_HOST): str})
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_HOST): str,
vol.Optional(CONF_NAME): str
})
)
return await self._create_device(user_input[CONF_HOST])
return await self._create_device(user_input[CONF_HOST], user_input[CONF_NAME])

async def async_step_import(self, user_input):
"""Import a config entry."""
host = user_input.get(CONF_HOST)
if not host:
return await self.async_step_user()
return await self._create_device(host)
return await self._create_device(host, user_input[CONF_NAME])

async def async_step_discovery(self, user_input):
"""Initialize step from discovery."""
_LOGGER.info("Discovered device: %s", user_input)
return await self._create_entry(user_input[KEY_IP])
return await self._create_entry(user_input[KEY_IP], None)
10 changes: 9 additions & 1 deletion custom_components/garo_wallbox/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,13 @@
80: 'GLBPDC-T222WO',
81: 'GLBPDC-T222FC',
82: 'GLBDCM-T274FC',
83: 'GLBDCW-T237FC-A VO'
83: 'GLBDCW-T237FC-A VO',
84: 'GLBDCWM-T137FC-A',
85: 'GLBDCWM-T237FC-A',
86: 'GGLBDCWM-T274WO-A',
87: 'GLBDCWM-T222FC',
88: 'GLBDCWM-T222WO',
89: 'GLBDCWM-T274FC-A',
90: 'GLBDCW-T211FC',
91: 'GLBDCW-T222FC'
}
11 changes: 7 additions & 4 deletions custom_components/garo_wallbox/garo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import asyncio

from homeassistant.util import Throttle
from .const import GARO_PRODUCT_MAP
from .const import GARO_PRODUCT_MAP, DOMAIN

current_milli_time = lambda: int(round(time.time() * 1000))

Expand Down Expand Up @@ -42,14 +42,17 @@ class Status(Enum):

class GaroDevice:

def __init__(self, host, session):
def __init__(self, host, name, session):
self.host = host
self.name = name
self._status = None
self._session = session

async def init(self):
await self.async_get_info()
self.id = 'garo_{}'.format(self.info.serial)
if self.name is None:
self.name = f'{self.info.model} ({self.host})'
await self.async_update()

@property
Expand All @@ -60,10 +63,10 @@ def status(self):
def device_info(self):
"""Return a device description for device registry."""
return {
"identifieres": self.id,
"identifiers": { (DOMAIN, self.id) },
"manufacturer": "Garo",
"model": self.info.model,
"name": self.host
"name": self.name,
}

def _request(self, parameter_list):
Expand Down
8 changes: 5 additions & 3 deletions custom_components/garo_wallbox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=


async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Daikin climate based on config_entry."""
"""Set up using config_entry."""
device = hass.data[GARO_DOMAIN].get(entry.entry_id)
async_add_entities([
GaroSensor(device, 'Status', 'status'),
Expand All @@ -25,14 +25,16 @@ async def async_setup_entry(hass, entry, async_add_entities):
GaroSensor(device, "Phases", 'nr_of_phases'),
GaroSensor(device, "Current Limit", 'current_limit', 'A'),
GaroSensor(device, "Pilot Level", 'pilot_level', 'A'),
GaroSensor(device, "Temperature", 'current_temperature', TEMP_CELSIUS)
GaroSensor(device, "Session Energy", 'acc_session_energy', "Wh"),
GaroSensor(device, "Total Energy", 'latest_reading', "Wh"),
GaroSensor(device, "Temperature", 'current_temperature', TEMP_CELSIUS),
])

class GaroSensor(Entity):
def __init__(self, device: GaroDevice, name, sensor, unit = None):
"""Initialize the sensor."""
self._device = device
self._name = f"{device.id} {name}"
self._name = f"{device.name} {name}"
self._sensor = sensor
self._unit = unit

Expand Down
2 changes: 1 addition & 1 deletion custom_components/garo_wallbox/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"user": {
"title": "Configure Garo Wallbox",
"description": "Enter IP address of your Garo Wallbox.",
"data": { "host": "Host" }
"data": { "host": "Host", "name": "Friendly name" }
}
},
"abort": {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/garo_wallbox/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"user": {
"title": "Configure Garo Wallbox",
"description": "Enter IP address of your Garo Wallbox.",
"data": { "host": "Host" }
"data": { "host": "Host", "name": "Friendly name" }
}
},
"abort": {
Expand Down

0 comments on commit 2889aeb

Please sign in to comment.