Skip to content

Commit

Permalink
Add tz offset to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Dec 17, 2023
1 parent b7ae428 commit c356eaa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
88 changes: 82 additions & 6 deletions custom_components/variable/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import datetime
from enum import Enum
import logging
import re
from typing import Any

from homeassistant import config_entries
Expand All @@ -22,6 +24,7 @@
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import selector
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
from iso4217 import Currency
import voluptuous as vol

Expand All @@ -31,6 +34,7 @@
CONF_EXCLUDE_FROM_RECORDER,
CONF_FORCE_UPDATE,
CONF_RESTORE,
CONF_TZOFFSET,
CONF_UPDATED,
CONF_VALUE,
CONF_VALUE_TYPE,
Expand Down Expand Up @@ -256,9 +260,27 @@ async def async_step_sensor_page_2(self, user_input=None):
user_input.update({CONF_VALUE: self.add_sensor_input.get(CONF_VALUE)})
yaml_value_type = self.yaml_import_get_value_type()
self.add_sensor_input.update({CONF_VALUE_TYPE: yaml_value_type})
if (
user_input.get(CONF_VALUE) is not None
and isinstance(user_input.get(CONF_VALUE), str)
and self.add_sensor_input.get(CONF_VALUE_TYPE) == "datetime"
):
if (
user_input.get(CONF_TZOFFSET) is not None
and re.match(
r"^[+-]?\d\d\:?\d\d\s*$", user_input.get(CONF_TZOFFSET)
)
is not None
):
val = user_input.get(CONF_VALUE) + user_input.get(CONF_TZOFFSET)
else:
val = user_input.get(CONF_VALUE) + "+0000"
else:
val = user_input.get(CONF_VALUE)
_LOGGER.debug(f"[New Sensor Page 2] val: {val}")
try:
newval = value_to_type(
user_input.get(CONF_VALUE),
val,
self.add_sensor_input.get(CONF_VALUE_TYPE),
)
except ValueError:
Expand Down Expand Up @@ -381,11 +403,21 @@ def build_add_sensor_page_2(self):
elif self.add_sensor_input.get(CONF_DEVICE_CLASS) in [
sensor.SensorDeviceClass.TIMESTAMP
]:
DEFAULT_TZOFFSET = datetime.datetime.now(
dt_util.get_time_zone(self.hass.config.time_zone)
).strftime("%z")
if DEFAULT_TZOFFSET is None:
DEFAULT_TZOFFSET = "+0000"
_LOGGER.debug(f"DEFAULT_TZOFFSET: {DEFAULT_TZOFFSET}")
SENSOR_PAGE_2_SCHEMA = SENSOR_PAGE_2_SCHEMA.extend(
{
vol.Optional(CONF_VALUE): selector.DateTimeSelector(
selector.DateTimeSelectorConfig()
)
),
vol.Optional(
CONF_TZOFFSET,
default=DEFAULT_TZOFFSET,
): selector.TextSelector(selector.TextSelectorConfig()),
}
)
value_type = "datetime"
Expand Down Expand Up @@ -620,9 +652,27 @@ async def async_step_sensor_options_page_2(self, user_input=None):
errors = {}
if user_input is not None:
_LOGGER.debug(f"[Sensor Options Page 2] user_input: {user_input}")
if (
user_input.get(CONF_VALUE) is not None
and isinstance(user_input.get(CONF_VALUE), str)
and self.add_sensor_input.get(CONF_VALUE_TYPE) == "datetime"
):
if (
user_input.get(CONF_TZOFFSET) is not None
and re.match(
r"^[+-]?\d\d\:?\d\d\s*$", user_input.get(CONF_TZOFFSET)
)
is not None
):
val = user_input.get(CONF_VALUE) + user_input.get(CONF_TZOFFSET)
else:
val = user_input.get(CONF_VALUE) + "+0000"
else:
val = user_input.get(CONF_VALUE)
_LOGGER.debug(f"[New Sensor Page 2] val: {val}")
try:
newval = value_to_type(
user_input.get(CONF_VALUE),
val,
self.sensor_options_page_1.get(CONF_VALUE_TYPE),
)
except ValueError:
Expand Down Expand Up @@ -763,24 +813,50 @@ def build_sensor_options_page_2(self):
]:
value_type = "datetime"
if val_default:
_LOGGER.debug(f"val_default_value: {val_default_value}")
dt = value_to_type(val_default_value, value_type)
if dt is not None and isinstance(dt, datetime.datetime):
tz_offset = dt.strftime("%z")
if tz_offset is None:
tz_offset = "+0000"
ts_val = dt.strftime("%Y-%m-%d %H:%M:%S")
else:
ts_val = None
tz_offset = "+0000"
_LOGGER.debug(f"ts_val: {ts_val}")
_LOGGER.debug(f"tz_offset: {tz_offset}")
SENSOR_OPTIONS_PAGE_2_SCHEMA = SENSOR_OPTIONS_PAGE_2_SCHEMA.extend(
{
vol.Optional(
CONF_VALUE,
default=val_default_value,
default=ts_val,
): selector.DateTimeSelector(
selector.DateTimeSelectorConfig()
)
),
vol.Optional(
CONF_TZOFFSET,
default=tz_offset,
): selector.TextSelector(selector.TextSelectorConfig()),
}
)
else:
DEFAULT_TZOFFSET = datetime.datetime.now(
dt_util.get_time_zone(self.hass.config.time_zone)
).strftime("%z")
if DEFAULT_TZOFFSET is None:
DEFAULT_TZOFFSET = "+0000"
_LOGGER.debug(f"DEFAULT_TZOFFSET: {DEFAULT_TZOFFSET}")
SENSOR_OPTIONS_PAGE_2_SCHEMA = SENSOR_OPTIONS_PAGE_2_SCHEMA.extend(
{
vol.Optional(
CONF_VALUE,
): selector.DateTimeSelector(
selector.DateTimeSelectorConfig()
)
),
vol.Optional(
CONF_TZOFFSET,
default=DEFAULT_TZOFFSET,
): selector.TextSelector(selector.TextSelectorConfig()),
}
)
else:
Expand Down
1 change: 1 addition & 0 deletions custom_components/variable/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CONF_ENTITY_PLATFORM = "entity_platform"
CONF_FORCE_UPDATE = "force_update"
CONF_RESTORE = "restore"
CONF_TZOFFSET = "tz_offset"
CONF_VALUE = "value"
CONF_VALUE_TYPE = "value_type"
CONF_VARIABLE_ID = "variable_id"
Expand Down
2 changes: 2 additions & 0 deletions custom_components/variable/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"title": "Variables+History - Sensor Page 2",
"data": {
"value": "Initial Value",
"tz_offset": "Initial Time Zone Offset",
"attributes": "Initial Attributes",
"state_class": "State Class",
"unit_of_measurement": "Unit of Measurement"
Expand Down Expand Up @@ -87,6 +88,7 @@
"title": "Variables+History - Sensor Page 2",
"data": {
"value": "Value (typically only useful if Restore on Restart is False)",
"tz_offset": "Time Zone Offset (typically only useful if Restore on Restart is False)",
"attributes": "Attributes (typically only useful if Restore on Restart is False)",
"state_class": "State Class",
"unit_of_measurement": "Unit of Measurement"
Expand Down
2 changes: 2 additions & 0 deletions custom_components/variable/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"title": "Variables+History - Sensor Page 2",
"data": {
"value": "Initial Value",
"tz_offset": "Initial Time Zone Offset",
"attributes": "Initial Attributes",
"state_class": "State Class",
"unit_of_measurement": "Unit of Measurement"
Expand Down Expand Up @@ -87,6 +88,7 @@
"title": "Variables+History - Sensor Page 2",
"data": {
"value": "Value (typically only useful if Restore on Restart is False)",
"tz_offset": "Time Zone Offset (typically only useful if Restore on Restart is False)",
"attributes": "Attributes (typically only useful if Restore on Restart is False)",
"state_class": "State Class",
"unit_of_measurement": "Unit of Measurement"
Expand Down

0 comments on commit c356eaa

Please sign in to comment.