Skip to content

Commit

Permalink
Merge pull request #59 from jeverley/last_reset_for_total
Browse files Browse the repository at this point in the history
  • Loading branch information
megakid authored Oct 27, 2024
2 parents 2bc0330 + c44170a commit d186cef
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 91 deletions.
16 changes: 11 additions & 5 deletions custom_components/hildebrand_glow_ihd_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DEVICE_ID,
)
from homeassistant.const import CONF_DEVICE_ID
from homeassistant.core import HomeAssistant

from .const import DOMAIN, CONF_TOPIC_PREFIX
from .const import (
CONF_TIME_ZONE_ELECTRICITY,
CONF_TIME_ZONE_GAS,
CONF_TOPIC_PREFIX,
DEFAULT_TOPIC_PREFIX,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -28,7 +32,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][entry.entry_id] = {}

hass.data[DOMAIN][entry.entry_id][CONF_DEVICE_ID] = entry.data[CONF_DEVICE_ID].strip().upper().replace(":", "").replace(" ", "")
hass.data[DOMAIN][entry.entry_id][CONF_TOPIC_PREFIX] = entry.data.get(CONF_TOPIC_PREFIX, "glow").strip().replace("#", "").replace(" ", "")
hass.data[DOMAIN][entry.entry_id][CONF_TOPIC_PREFIX] = entry.data.get(CONF_TOPIC_PREFIX, DEFAULT_TOPIC_PREFIX).strip().replace("#", "").replace(" ", "")
hass.data[DOMAIN][entry.entry_id][CONF_TIME_ZONE_ELECTRICITY] = entry.data.get(CONF_TIME_ZONE_ELECTRICITY)
hass.data[DOMAIN][entry.entry_id][CONF_TIME_ZONE_GAS] = entry.data.get(CONF_TIME_ZONE_GAS)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
118 changes: 85 additions & 33 deletions custom_components/hildebrand_glow_ihd_mqtt/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,81 @@
"""Config flow for Hildebrand Glow IHD MQTT."""
import logging

import voluptuous as vol

from homeassistant import config_entries
import zoneinfo

from homeassistant.config_entries import (
ConfigFlow,
ConfigEntry,
CONN_CLASS_LOCAL_PUSH,
OptionsFlow,
)
from homeassistant.const import CONF_DEVICE_ID
from homeassistant.core import callback

from .const import DOMAIN, CONF_TOPIC_PREFIX
from homeassistant.helpers.selector import (
SelectSelector,
SelectSelectorConfig,
SelectSelectorMode,
)

from .const import (
CONF_TIME_ZONE_ELECTRICITY,
CONF_TIME_ZONE_GAS,
CONF_TOPIC_PREFIX,
DEFAULT_DEVICE_ID,
DEFAULT_TOPIC_PREFIX,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

class HildebrandGlowIHDMQTTConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class HildebrandGlowIHDMQTTConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH

MINOR_VERSION = 1
CONNECTION_CLASS = CONN_CLASS_LOCAL_PUSH

async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_DEVICE_ID, default='+'):str,
vol.Required(CONF_TOPIC_PREFIX, default='glow'):str
}), errors=errors
)

device_id = user_input[CONF_DEVICE_ID]
topic_prefix = user_input[CONF_TOPIC_PREFIX]

await self.async_set_unique_id('{}_{}'.format(DOMAIN, device_id))
self._abort_if_unique_id_configured()

return self.async_create_entry(
title="",
data={
CONF_DEVICE_ID: device_id,
CONF_TOPIC_PREFIX: topic_prefix
})

if user_input is not None:
device_id = user_input.get(CONF_DEVICE_ID)
topic_prefix = user_input.get(CONF_TOPIC_PREFIX)
time_zone_electricity = user_input.get(CONF_TIME_ZONE_ELECTRICITY)
time_zone_gas = user_input.get(CONF_TIME_ZONE_GAS)

await self.async_set_unique_id('{}_{}'.format(DOMAIN, device_id))
self._abort_if_unique_id_configured()

return self.async_create_entry(
title="",
data={
CONF_DEVICE_ID: device_id,
CONF_TOPIC_PREFIX: topic_prefix,
CONF_TIME_ZONE_ELECTRICITY: time_zone_electricity,
CONF_TIME_ZONE_GAS: time_zone_gas,
})

get_timezones: list[str] = list(
await self.hass.async_add_executor_job(
zoneinfo.available_timezones
)
)
return self.async_show_form(
step_id="user", data_schema=vol.Schema({
vol.Required(CONF_DEVICE_ID, default=DEFAULT_DEVICE_ID):str,
vol.Required(CONF_TOPIC_PREFIX, default=DEFAULT_TOPIC_PREFIX):str,
vol.Required(CONF_TIME_ZONE_ELECTRICITY, default=self.hass.config.time_zone): SelectSelector(
SelectSelectorConfig(
options=get_timezones, mode=SelectSelectorMode.DROPDOWN, sort=True
)
),
vol.Required(CONF_TIME_ZONE_GAS, default=self.hass.config.time_zone): SelectSelector(
SelectSelectorConfig(
options=get_timezones, mode=SelectSelectorMode.DROPDOWN, sort=True
)
),
}), errors=errors
)

@staticmethod
@callback
Expand All @@ -47,10 +84,10 @@ def async_get_options_flow(config_entry):
return HildebrandGlowIHDMQTTOptionsFlowHandler(config_entry)


class HildebrandGlowIHDMQTTOptionsFlowHandler(config_entries.OptionsFlow):
class HildebrandGlowIHDMQTTOptionsFlowHandler(OptionsFlow):
"""Handle a option flow for HildebrandGlowIHDMQTT."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

Expand All @@ -59,8 +96,23 @@ async def async_step_init(self, user_input=None):
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

get_timezones: list[str] = list(
await self.hass.async_add_executor_job(
zoneinfo.available_timezones
)
)
data_schema=vol.Schema({
vol.Required(CONF_DEVICE_ID, default=self.config_entry.options.get(CONF_DEVICE_ID, "+")):str,
vol.Required(CONF_TOPIC_PREFIX, default=self.config_entry.options.get(CONF_TOPIC_PREFIX, "glow")):str
vol.Required(CONF_DEVICE_ID, default=self.config_entry.options.get(CONF_DEVICE_ID, DEFAULT_DEVICE_ID)):str,
vol.Required(CONF_TOPIC_PREFIX, default=self.config_entry.options.get(CONF_TOPIC_PREFIX, DEFAULT_TOPIC_PREFIX)):str,
vol.Required(CONF_TIME_ZONE_ELECTRICITY, default=self.config_entry.options.get(CONF_TIME_ZONE_ELECTRICITY, self.hass.config.time_zone)): SelectSelector(
SelectSelectorConfig(
options=get_timezones, mode=SelectSelectorMode.DROPDOWN, sort=True
)
),
vol.Required(CONF_TIME_ZONE_GAS, default=self.config_entry.options.get(CONF_TIME_ZONE_GAS, self.hass.config.time_zone)): SelectSelector(
SelectSelectorConfig(
options=get_timezones, mode=SelectSelectorMode.DROPDOWN, sort=True
)
),
})
return self.async_show_form(step_id="init", data_schema=data_schema)
return self.async_show_form(step_id="init", data_schema=data_schema)
18 changes: 17 additions & 1 deletion custom_components/hildebrand_glow_ihd_mqtt/const.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from itertools import cycle, islice
from typing import Final
from enum import Enum

DOMAIN: Final = "hildebrand_glow_ihd"


ATTR_NAME = "name"
ATTR_ACTIVITY = "activity"
ATTR_BATTERY_STATE = "battery_state"
Expand All @@ -14,4 +16,18 @@
ATTR_ERROR = "error"
ATTR_STATE = "state"

CONF_TOPIC_PREFIX = "topic_prefix"
CONF_TIME_ZONE_ELECTRICITY = "time_zone_electricity"
CONF_TIME_ZONE_GAS = "time_zone_gas"
CONF_TOPIC_PREFIX = "topic_prefix"

DEFAULT_DEVICE_ID = "+"
DEFAULT_TOPIC_PREFIX= "glow"

# Meter intervals
class MeterInterval(Enum):
"""Meter intervals."""

DAY = "day"
WEEK = "week"
MONTH = "month"
YEAR = "year"
Loading

0 comments on commit d186cef

Please sign in to comment.