Skip to content

Commit

Permalink
Add back YAML setup
Browse files Browse the repository at this point in the history
  • Loading branch information
turbokongen committed Apr 8, 2020
1 parent 1a07e1d commit ae5464e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 31 deletions.
3 changes: 3 additions & 0 deletions custom_components/ams/.translations/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"config": {
"abort": {
"single_instance_allowed": "Only one instance of hass-AMS is allowed."
},
"title": "Norwegian AMS",
"step": {
"user": {
Expand Down
3 changes: 3 additions & 0 deletions custom_components/ams/.translations/nb.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"config": {
"abort": {
"single_instance_allowed": "Kun en oppføring av hass-AMS er tillatt"
},
"title": "AMS Norge",
"step": {
"user": {
Expand Down
62 changes: 51 additions & 11 deletions custom_components/ams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,72 @@
import logging
import threading

import homeassistant.helpers.config_validation as cv
import serial
from homeassistant.config_entries import ConfigEntry
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, SOURCE_IMPORT
from homeassistant.core import Config, HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_send

from .const import (AMS_DEVICES, CONF_METER_MANUFACTURER, CONF_PARITY,
CONF_SERIAL_PORT, DEFAULT_BAUDRATE, DEFAULT_TIMEOUT,
DOMAIN, FRAME_FLAG, SIGNAL_NEW_AMS_SENSOR,
SIGNAL_UPDATE_AMS)
from .const import (
AMS_DEVICES,
CONF_METER_MANUFACTURER,
CONF_PARITY,
CONF_SERIAL_PORT,
DEFAULT_BAUDRATE,
DEFAULT_METER_MANUFACTURER,
DEFAULT_PARITY,
DEFAULT_SERIAL_PORT,
DEFAULT_TIMEOUT,
DOMAIN,
FRAME_FLAG,
SIGNAL_NEW_AMS_SENSOR,
SIGNAL_UPDATE_AMS,
)
from .parsers import aidon as Aidon
from .parsers import kaifa as Kaifa
from .parsers import kamstrup as Kamstrup

_LOGGER = logging.getLogger(__name__)

{
DOMAIN: vol.Schema(
{
vol.Required(CONF_SERIAL_PORT, default=DEFAULT_SERIAL_PORT): cv.string,
vol.Required(
CONF_METER_MANUFACTURER, default=DEFAULT_METER_MANUFACTURER
): cv.string,
vol.Optional(CONF_PARITY, default=DEFAULT_PARITY): cv.string,
}
)
}


def _setup(hass, config):
"""Setup helper for the component."""
if DOMAIN not in hass.data:
hub = AmsHub(hass, config)
hass.data[DOMAIN] = hub


async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""AMS hub YAML setup."""
hass.data[DOMAIN] = {}
if config.get(DOMAIN) is None:
_LOGGER.info("No YAML config available, using config_entries")
return True
_setup(hass, config[DOMAIN])
if not hass.config_entries.async_entries(DOMAIN):
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config[DOMAIN]
)
)
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up AMS as config entry."""
hub = AmsHub(hass, entry)
hass.data[DOMAIN] = hub
_setup(hass, entry.data)
hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry, "sensor"))
return True

Expand All @@ -50,10 +90,10 @@ class AmsHub:
def __init__(self, hass, entry):
"""Initialize the AMS hub."""
self._hass = hass
port = (entry.data[CONF_SERIAL_PORT].split(":"))[0]
port = entry[CONF_SERIAL_PORT]
_LOGGER.debug("Using port %s", port)
parity = entry.data[CONF_PARITY]
self.meter_manufacturer = entry.data[CONF_METER_MANUFACTURER]
parity = entry[CONF_PARITY]
self.meter_manufacturer = entry[CONF_METER_MANUFACTURER]
self.sensor_data = {}
self._running = True
self._ser = serial.Serial(
Expand Down
55 changes: 35 additions & 20 deletions custom_components/ams/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""Adds config flow for hass-HAN."""
"""Adds config flow for hass-AMS."""
import logging

import serial.tools.list_ports as devices
import voluptuous as vol
from homeassistant import config_entries

from .const import (CONF_METER_MANUFACTURER, CONF_PARITY, CONF_SERIAL_PORT,
DEFAULT_METER_MANUFACTURER, DEFAULT_PARITY, DOMAIN)
from .const import (
CONF_METER_MANUFACTURER,
CONF_PARITY,
CONF_SERIAL_PORT,
DEFAULT_METER_MANUFACTURER,
DEFAULT_PARITY,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -23,25 +29,34 @@ def __init__(self):

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

portdata = await self.hass.async_add_executor_job(devices.comports)
ports = [(comport.device + ": " + comport.description)
for comport in portdata]
ports = [comport.device for comport in portdata]

if user_input is not None:
return self.async_create_entry(
title="Norwegian AMS", data=user_input)
return self.async_create_entry(title="Norwegian AMS", data=user_input)
_LOGGER.debug(ports)
return self.async_show_form(step_id="user", data_schema=vol.Schema({
vol.Required(CONF_SERIAL_PORT,
default=None): vol.In(ports),
vol.Required(CONF_METER_MANUFACTURER,
default=DEFAULT_METER_MANUFACTURER): vol.All(str),
vol.Optional(CONF_PARITY, default=DEFAULT_PARITY): vol.All(str)
}),
description_placeholders={
CONF_SERIAL_PORT: ports,
}, errors=self._errors)

async def async_step_import(self, user_input=None):
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_SERIAL_PORT, default=None): vol.In(ports),
vol.Required(
CONF_METER_MANUFACTURER, default=DEFAULT_METER_MANUFACTURER
): vol.All(str),
vol.Optional(CONF_PARITY, default=DEFAULT_PARITY): vol.All(str),
}
),
description_placeholders={CONF_SERIAL_PORT: ports,},
errors=self._errors,
)

async def async_step_import(self, import_config):
"""Import a config flow from configuration."""
return self.async_create_entry(title="configuration.yaml", data={})
if self._async_current_entries():
_LOGGER.warning("Only one configuration of hass-AMS is allowed.")
return self.async_abort(reason="singel_instance_allowed")

return await self.async_step_user(import_config)

0 comments on commit ae5464e

Please sign in to comment.