Skip to content

Commit

Permalink
Merge pull request #194 from Snuffy2/Add-zone-as-an-Advanced-Display-…
Browse files Browse the repository at this point in the history
…Option

Add zone as an Advanced Display Option
  • Loading branch information
Snuffy2 authored Aug 6, 2023
2 parents fc90918 + 9bbd8a8 commit 3d5d9aa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ These can be nested.
```
name_no_dupe, category(-, place), type(-, yes), neighborhood, house_number, street
```
* To recreate `formatted_place` _(as close as possible)_:
* To recreate `formatted_place`:
```
driving, name_no_dupe[type(-, unclassified, category(-, highway))[category(-, highway)], house_number, route_number(type(+, motorway, trunk))[street[route_number]], neighborhood(type(house))], city_clean[county], state_abbr
zone_name[driving, name_no_dupe[type(-, unclassified, category(-, highway))[category(-, highway)], house_number, route_number(type(+, motorway, trunk))[street[route_number]], neighborhood(type(house))], city_clean[county], state_abbr]
```

### Fields
Expand Down
90 changes: 48 additions & 42 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,15 +1659,15 @@ def parse_parens(self, curr_options):
if attr_item == "+":
continue
# _LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [parse_parens] attr: {paren_attr} / item: {attr_item}")
paren_attr_list.append(attr_item.strip())
paren_attr_list.append(str(attr_item).strip().lower())
if paren_attr_incl:
incl_attr.update({paren_attr: paren_attr_list})
else:
excl_attr.update({paren_attr: paren_attr_list})
elif paren_incl:
incl.append(item)
incl.append(str(item).strip().lower())
else:
excl.append(item)
excl.append(str(item).strip().lower())

elif not empty_paren:
_LOGGER.error(
Expand Down Expand Up @@ -1727,18 +1727,24 @@ def get_option_state(
incl_attr = {} if incl_attr is None else incl_attr
excl_attr = {} if excl_attr is None else excl_attr
if opt is not None and opt:
opt = opt.lower().strip()
opt = str(opt).lower().strip()
_LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] Option: {opt}")
out = self.get_attr(DISPLAY_OPTIONS_MAP.get(opt))
if (
DISPLAY_OPTIONS_MAP.get(opt)
in [ATTR_DEVICETRACKER_ZONE, ATTR_DEVICETRACKER_ZONE_NAME]
and not self.in_zone()
):
out = None
_LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] State: {out}")
# _LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] incl list: {incl}")
# _LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] excl list: {excl}")
# _LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] incl_attr dict: {incl_attr}")
# _LOGGER.debug(f"({self.get_attr(CONF_NAME)}) [get_option_state] excl_attr dict: {excl_attr}")
if out is not None and out:
if incl and out not in incl:
if incl and str(out).strip().lower() not in incl:
out = None
elif excl and out in excl:
elif excl and str(out).strip().lower() in excl:
out = None
if incl_attr:
for attr, states in incl_attr.items():
Expand Down Expand Up @@ -1822,14 +1828,14 @@ def build_state_from_display_options(self):

if (
"zone_name" in display_options
and "do_not_show_not_home" not in display_options
and not self.is_attr_blank(ATTR_DEVICETRACKER_ZONE_NAME)
and (self.in_zone() or "do_not_show_not_home" not in display_options)
):
user_display.append(self.get_attr(ATTR_DEVICETRACKER_ZONE_NAME))
elif (
"zone" in display_options
and "do_not_show_not_home" not in display_options
and not self.is_attr_blank(ATTR_DEVICETRACKER_ZONE)
and (self.in_zone() or "do_not_show_not_home" not in display_options)
):
user_display.append(self.get_attr(ATTR_DEVICETRACKER_ZONE))

Expand Down Expand Up @@ -2251,7 +2257,7 @@ def do_update(self, reason):
# 0: False. 1: True. 2: False, but set direction of travel to stationary
proceed_with_update = self.determine_if_update_needed()

if proceed_with_update == 1 and not self.is_attr_blank(ATTR_DEVICETRACKER_ZONE):
if proceed_with_update == 1:
# 0: False. 1: True. 2: False, but set direction of travel to stationary
_LOGGER.info(
f"({self.get_attr(CONF_NAME)}) Meets criteria, proceeding with OpenStreetMap query"
Expand Down Expand Up @@ -2299,40 +2305,40 @@ def do_update(self, reason):
f"({self.get_attr(CONF_NAME)}) New State using formatted_place: "
+ f"{self.get_attr(ATTR_NATIVE_VALUE)}"
)
elif not self.in_zone():
if any(
ext in self.get_attr(ATTR_DISPLAY_OPTIONS)
for ext in ["(", ")", "[", "]"]
):
# Replace place option with expanded definition
# temp_opt = self.get_attr(ATTR_DISPLAY_OPTIONS)
# re.sub(
# r"place(?=[\[\(\]\)\,\s])",
# "place_name,place_category(-,place),place_type(-,yes),neighborhood,street_number,street",
# temp_opt,
# )
# self.set_attr(ATTR_DISPLAY_OPTIONS, temp_opt)
self.clear_attr(ATTR_DISPLAY_OPTIONS_LIST)
display_options = None
self.adv_options_state_list = []
self.street_num_i = -1
self.street_i = -1
self.temp_i = 0
_LOGGER.debug(
f"({self.get_attr(CONF_NAME)}) Initial Advanced Display Options: "
+ f"{self.get_attr(ATTR_DISPLAY_OPTIONS)}"
)

self.build_from_advanced_options(
self.get_attr(ATTR_DISPLAY_OPTIONS)
)
_LOGGER.debug(
f"({self.get_attr(CONF_NAME)}) Back from initial advanced build: "
+ f"{self.adv_options_state_list}"
)
self.compile_state_from_advanced_options()
else:
self.build_state_from_display_options()
elif any(
ext in self.get_attr(ATTR_DISPLAY_OPTIONS)
for ext in ["(", ")", "[", "]"]
):
# Replace place option with expanded definition
# temp_opt = self.get_attr(ATTR_DISPLAY_OPTIONS)
# re.sub(
# r"place(?=[\[\(\]\)\,\s])",
# "place_name,place_category(-,place),place_type(-,yes),neighborhood,street_number,street",
# temp_opt,
# )
# self.set_attr(ATTR_DISPLAY_OPTIONS, temp_opt)
self.clear_attr(ATTR_DISPLAY_OPTIONS_LIST)
display_options = None
self.adv_options_state_list = []
self.street_num_i = -1
self.street_i = -1
self.temp_i = 0
_LOGGER.debug(
f"({self.get_attr(CONF_NAME)}) Initial Advanced Display Options: "
+ f"{self.get_attr(ATTR_DISPLAY_OPTIONS)}"
)

self.build_from_advanced_options(
self.get_attr(ATTR_DISPLAY_OPTIONS)
)
_LOGGER.debug(
f"({self.get_attr(CONF_NAME)}) Back from initial advanced build: "
+ f"{self.adv_options_state_list}"
)
self.compile_state_from_advanced_options()
elif not self.in_zone():
self.build_state_from_display_options()
elif (
"zone" in display_options
and not self.is_attr_blank(ATTR_DEVICETRACKER_ZONE)
Expand Down

0 comments on commit 3d5d9aa

Please sign in to comment.