diff --git a/custom_components/places/config_flow.py b/custom_components/places/config_flow.py index efe58e04..40239877 100644 --- a/custom_components/places/config_flow.py +++ b/custom_components/places/config_flow.py @@ -16,6 +16,7 @@ from homeassistant.helpers import selector from .const import ( + CONF_DATE_FORMAT, CONF_DEVICETRACKER_ID, CONF_DISPLAY_OPTIONS, CONF_EXTENDED_ATTR, @@ -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, @@ -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 = ( @@ -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 @@ -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}") @@ -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()), @@ -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=( diff --git a/custom_components/places/const.py b/custom_components/places/const.py index 595faca9..79c61f52 100644 --- a/custom_components/places/const.py +++ b/custom_components/places/const.py @@ -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 @@ -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 @@ -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" @@ -126,6 +127,7 @@ CONF_NAME, CONF_DISPLAY_OPTIONS, CONF_SHOW_TIME, + CONF_DATE_FORMAT, CONF_USE_GPS, CONF_UNIQUE_ID, ] diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index 038e3ae1..607e9a3c 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -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, @@ -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, @@ -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, @@ -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( diff --git a/custom_components/places/translations/en.json b/custom_components/places/translations/en.json index e4195267..13b8a718 100644 --- a/custom_components/places/translations/en.json +++ b/custom_components/places/translations/en.json @@ -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" @@ -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"