Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Time Zone Offset field when creating or editing Variable Timestamp Sensor #107

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading