Skip to content

Commit

Permalink
Add Date Format
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow authored and Snuffy2 committed Oct 8, 2023
1 parent f0428b9 commit a31f041
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
31 changes: 29 additions & 2 deletions custom_components/places/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from homeassistant.helpers import selector

from .const import (
CONF_DATE_FORMAT,
CONF_DEVICETRACKER_ID,
CONF_DISPLAY_OPTIONS,
CONF_EXTENDED_ATTR,
Expand All @@ -25,6 +26,7 @@
CONF_MAP_ZOOM,
CONF_SHOW_TIME,
CONF_USE_GPS,
DEFAULT_DATE_FORMAT,
DEFAULT_DISPLAY_OPTIONS,
DEFAULT_EXTENDED_ATTR,
DEFAULT_HOME_ZONE,
Expand All @@ -41,6 +43,7 @@
_LOGGER = logging.getLogger(__name__)
MAP_PROVIDER_OPTIONS = ["apple", "google", "osm"]
STATE_OPTIONS = ["zone, place", "formatted_place", "zone_name, place"]
DATE_FORMAT_OPTIONS = ["mm/dd", "dd/mm"]
MAP_ZOOM_MIN = 1
MAP_ZOOM_MAX = 20
COMPONENT_CONFIG_URL = (
Expand Down Expand Up @@ -143,7 +146,6 @@ async def validate_input(hass: core.HomeAssistant, data: dict) -> dict[str, Any]


class PlacesConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 1
# Connection classes in homeassistant/config_entries.py are now deprecated

Expand All @@ -157,7 +159,6 @@ async def async_step_user(self, user_input=None) -> FlowResult:
# `validate_input` above.
errors = {}
if user_input is not None:

try:
info = await validate_input(self.hass, user_input)
# _LOGGER.debug(f"[New Sensor] info: {info}")
Expand Down Expand Up @@ -229,6 +230,16 @@ async def async_step_user(self, user_input=None) -> FlowResult:
vol.Optional(
CONF_SHOW_TIME, default=DEFAULT_SHOW_TIME
): selector.BooleanSelector(selector.BooleanSelectorConfig()),
vol.Optional(
CONF_DATE_FORMAT, default=DEFAULT_DATE_FORMAT
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=DATE_FORMAT_OPTIONS,
multiple=False,
custom_value=False,
mode=selector.SelectSelectorMode.DROPDOWN,
)
),
vol.Optional(
CONF_USE_GPS, default=DEFAULT_USE_GPS
): selector.BooleanSelector(selector.BooleanSelectorConfig()),
Expand Down Expand Up @@ -403,6 +414,22 @@ async def async_step_init(self, user_input=None):
else DEFAULT_SHOW_TIME
),
): selector.BooleanSelector(selector.BooleanSelectorConfig()),
vol.Optional(
CONF_DATE_FORMAT,
default=DEFAULT_DATE_FORMAT,
description={
"suggested_value": self.config_entry.data[CONF_DATE_FORMAT]
if CONF_DATE_FORMAT in self.config_entry.data
else DEFAULT_DATE_FORMAT
},
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=DATE_FORMAT_OPTIONS,
multiple=False,
custom_value=False,
mode=selector.SelectSelectorMode.DROPDOWN,
)
),
vol.Optional(
CONF_USE_GPS,
default=(
Expand Down
4 changes: 3 additions & 1 deletion custom_components/places/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DEFAULT_MAP_ZOOM = 18
DEFAULT_DISPLAY_OPTIONS = "zone_name, place"
DEFAULT_SHOW_TIME = False
DEFAULT_DATE_FORMAT = "mm/dd"
DEFAULT_USE_GPS = True

# Settings
Expand Down Expand Up @@ -49,6 +50,7 @@
CONF_NATIVE_VALUE = "native_value"
CONF_DISPLAY_OPTIONS = "options"
CONF_SHOW_TIME = "show_time"
CONF_DATE_FORMAT = "date_format"
CONF_USE_GPS = "use_gps_accuracy"

# Attributes
Expand Down Expand Up @@ -108,7 +110,6 @@
ATTR_STREET = "street"
ATTR_STREET_REF = "street_ref"
ATTR_STREET_NUMBER = "street_number"
# ATTR_UPDATES_SKIPPED = "updates_skipped"
ATTR_WIKIDATA_DICT = "wikidata_dict"
ATTR_WIKIDATA_ID = "wikidata_id"

Expand All @@ -126,6 +127,7 @@
CONF_NAME,
CONF_DISPLAY_OPTIONS,
CONF_SHOW_TIME,
CONF_DATE_FORMAT,
CONF_USE_GPS,
CONF_UNIQUE_ID,
]
Expand Down
23 changes: 16 additions & 7 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
ATTR_STREET_REF,
ATTR_WIKIDATA_DICT,
ATTR_WIKIDATA_ID,
CONF_DATE_FORMAT,
CONF_DEVICETRACKER_ID,
CONF_DISPLAY_OPTIONS,
CONF_EXTENDED_ATTR,
Expand All @@ -119,6 +120,7 @@
CONF_SHOW_TIME,
CONF_USE_GPS,
CONFIG_ATTRIBUTES_LIST,
DEFAULT_DATE_FORMAT,
DEFAULT_DISPLAY_OPTIONS,
DEFAULT_EXTENDED_ATTR,
DEFAULT_HOME_ZONE,
Expand Down Expand Up @@ -244,6 +246,10 @@ def __init__(self, hass, config, config_entry, name, unique_id):
self.set_attr(
CONF_SHOW_TIME, config.setdefault(CONF_SHOW_TIME, DEFAULT_SHOW_TIME)
)
self.set_attr(
CONF_DATE_FORMAT,
config.setdefault(CONF_DATE_FORMAT, DEFAULT_DATE_FORMAT).lower(),
)
self.set_attr(CONF_USE_GPS, config.setdefault(CONF_USE_GPS, DEFAULT_USE_GPS))
self.set_attr(
ATTR_JSON_FILENAME,
Expand Down Expand Up @@ -2353,16 +2359,19 @@ def change_dot_to_stationary(self, now, changed_diff_sec):

def change_show_time_to_date(self):
if not self.is_attr_blank(ATTR_NATIVE_VALUE) and self.get_attr(CONF_SHOW_TIME):
localedate = str(locale.nl_langinfo(locale.D_FMT)).replace(" ", "")
if localedate.lower().endswith("%y"):
localemmdd = localedate[:-3]
elif localedate.lower().startswith("%y"):
localemmdd = localedate[3:]
# localedate = str(locale.nl_langinfo(locale.D_FMT)).replace(" ", "")
# if localedate.lower().endswith("%y"):
# localemmdd = localedate[:-3]
# elif localedate.lower().startswith("%y"):
# localemmdd = localedate[3:]
# else:
if self.get_attr(CONF_DATE_FORMAT) == "dd/mm":
dateformat = "%d/%m"
else:
localemmdd = "%m/%d"
dateformat = "%m/%d"
mmddstring = (
datetime.fromisoformat(self.get_attr(ATTR_LAST_CHANGED))
.strftime(f"{localemmdd}")
.strftime(f"{dateformat}")
.replace(" ", "")[:5]
)
self.set_attr(
Expand Down
2 changes: 2 additions & 0 deletions custom_components/places/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"language": "Language (optional)",
"extended_attr": "Enable Extended Attributes",
"show_time": "Show last updated time at end of state '(since xx:yy)'",
"date_format": "Date format to show at end of state when not changed >24h",
"use_gps_accuracy": "Use GPS Accuracy"
},
"description": "Create a new sensor\nSee [Configuration Options]({component_config_url}) on GitHub for details"
Expand All @@ -35,6 +36,7 @@
"language": "Language (optional)",
"extended_attr": "Enable Extended Attributes",
"show_time": "Show last updated time at end of state '(since xx:yy)'",
"date_format": "Date format to show at end of state when not changed >24h",
"use_gps_accuracy": "Use GPS Accuracy"
},
"description": "**Updating sensor: {sensor_name}**\nSee [Configuration Options]({component_config_url}) on GitHub for details"
Expand Down

0 comments on commit a31f041

Please sign in to comment.