Skip to content

Commit

Permalink
Merge pull request #127 from Snuffy2/Advanced-Display-Options
Browse files Browse the repository at this point in the history
Advanced display options v3
  • Loading branch information
Snuffy2 authored Nov 20, 2022
2 parents 8d5c71d + b2d18c8 commit 70d1b1d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,24 @@ name_no_dupe, category(-, place), type(-, yes), neighborhood, house_number, stre
```
* To recreate `formatted_place` _(as close as possible)_:
```
driving, name_no_dupe[type(-, unclassified, category(-, highway))[category(-, highway)], house_number, route_number(type(+, motorway, trunk))[street[route_number]], neighborhood(type(house))], city[county], state_abbr
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

* `driving`
* `name` (Synonym: `place_name`)
* `name_no_dupe` (Synonym: `place_name_no_dupe` - _Will be blank if the name is the same as one of the other attributes_)
* `name_no_dupe` (Synonym: `place_name_no_dupe`)
* _Will be blank if the name is the same as one of the other attributes_
* `type` (Synonym: `place_type`)
* `category` (Synonym: `place_category`)
* `street_number` (Synonym: `house_number`)
* `street`
* `route_number` (Synonym: `street_ref`)
* `neighborhood` (Synonyms: `neighbourhood`, `place_neighborhood`, `place_neighbourhood`)
* `city`
* `city_clean`
* _`city` but removes "Township" and moves "City" to the end it it stats with "City of"_
* `state` (Synonym: `region`)
* `state_abbr`
* `county`
Expand Down
4 changes: 4 additions & 0 deletions custom_components/places/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

# Attributes
ATTR_CITY = "city"
ATTR_CITY_CLEAN = "city_clean"
ATTR_COUNTRY = "country"
ATTR_COUNTY = "county"
ATTR_DEVICETRACKER_ID = "devicetracker_entityid"
Expand Down Expand Up @@ -123,6 +124,7 @@
]
RESET_ATTRIBUTE_LIST = [
ATTR_CITY,
ATTR_CITY_CLEAN,
ATTR_COUNTRY,
ATTR_COUNTY,
ATTR_DRIVING,
Expand Down Expand Up @@ -208,6 +210,7 @@
]
JSON_ATTRIBUTE_LIST = [
ATTR_CITY,
ATTR_CITY_CLEAN,
ATTR_COUNTRY,
ATTR_COUNTY,
ATTR_DEVICETRACKER_ZONE_NAME,
Expand Down Expand Up @@ -311,6 +314,7 @@
"place_neighborhood": ATTR_PLACE_NEIGHBOURHOOD,
"place_neighbourhood": ATTR_PLACE_NEIGHBOURHOOD,
"city": ATTR_CITY,
"city_clean": ATTR_CITY_CLEAN,
"region": ATTR_REGION,
"state": ATTR_REGION,
"state_abbr": ATTR_STATE_ABBR,
Expand Down
81 changes: 50 additions & 31 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

from .const import (
ATTR_CITY,
ATTR_CITY_CLEAN,
ATTR_COUNTRY,
ATTR_COUNTY,
ATTR_DEVICETRACKER_ID,
Expand Down Expand Up @@ -1265,15 +1266,6 @@ def parse_osm_dict(self):
+ ") Place Name: "
+ str(self.get_attr(ATTR_PLACE_NAME))
)
dupe_attributes_check = []
for attr in PLACE_NAME_DUPLICATE_LIST:
if not self.is_attr_blank(attr):
dupe_attributes_check.append(self.get_attr(attr))
if (
not self.is_attr_blank(ATTR_PLACE_NAME)
and self.get_attr(ATTR_PLACE_NAME) not in dupe_attributes_check
):
self.set_attr(ATTR_PLACE_NAME_NO_DUPE, self.get_attr(ATTR_PLACE_NAME))

if "neighbourhood" in self.get_attr(ATTR_OSM_DICT).get("address"):
self.set_attr(
Expand Down Expand Up @@ -1321,10 +1313,15 @@ def parse_osm_dict(self):
ATTR_CITY,
self.get_attr(ATTR_OSM_DICT).get("address").get("city_district"),
)
if not self.is_attr_blank(ATTR_CITY) and self.get_attr(ATTR_CITY).startswith(
"City of"
):
self.set_attr(ATTR_CITY, self.get_attr(ATTR_CITY)[8:] + " City")
if not self.is_attr_blank(ATTR_CITY):
self.set_attr(
ATTR_CITY_CLEAN,
self.get_attr(ATTR_CITY).replace(" Township", "").strip(),
)
if self.get_attr(ATTR_CITY_CLEAN).startswith("City of"):
self.set_attr(
ATTR_CITY_CLEAN, self.get_attr(ATTR_CITY_CLEAN)[8:] + " City"
)

if "city_district" in self.get_attr(ATTR_OSM_DICT).get("address"):
self.set_attr(
Expand Down Expand Up @@ -1405,6 +1402,16 @@ def parse_osm_dict(self):
+ " / Street Ref: "
+ str(self.get_attr(ATTR_STREET_REF))
)
dupe_attributes_check = []
for attr in PLACE_NAME_DUPLICATE_LIST:
if not self.is_attr_blank(attr):
dupe_attributes_check.append(self.get_attr(attr))
if (
not self.is_attr_blank(ATTR_PLACE_NAME)
and self.get_attr(ATTR_PLACE_NAME) not in dupe_attributes_check
):
self.set_attr(ATTR_PLACE_NAME_NO_DUPE, self.get_attr(ATTR_PLACE_NAME))

_LOGGER.debug(
"("
+ self.get_attr(CONF_NAME)
Expand Down Expand Up @@ -2058,14 +2065,25 @@ def get_option_state(self, opt, incl=[], excl=[], incl_attr={}, excl_attr={}):
+ str(out)
)
if out is not None and out:
if out == out.lower() and opt in [
ATTR_DEVICETRACKER_ZONE_NAME,
ATTR_PLACE_TYPE,
ATTR_PLACE_CATEGORY,
]:
if out == out.lower() and (
DISPLAY_OPTIONS_MAP.get(opt) == ATTR_DEVICETRACKER_ZONE_NAME
or DISPLAY_OPTIONS_MAP.get(opt) == ATTR_PLACE_TYPE
or DISPLAY_OPTIONS_MAP.get(opt) == ATTR_PLACE_CATEGORY
):
out = out.title()
out = out.strip()
if opt == "street_number":
if (
DISPLAY_OPTIONS_MAP.get(opt) == ATTR_STREET
or DISPLAY_OPTIONS_MAP.get(opt) == ATTR_STREET_REF
):
self.street_i = self.temp_i
_LOGGER.debug(
"("
+ self.get_attr(CONF_NAME)
+ ") [get_option_state] street_i: "
+ str(self.street_i)
)
if DISPLAY_OPTIONS_MAP.get(opt) == ATTR_STREET_NUMBER:
self.street_num_i = self.temp_i
_LOGGER.debug(
"("
Expand All @@ -2079,17 +2097,17 @@ def get_option_state(self, opt, incl=[], excl=[], incl_attr={}, excl_attr={}):
return None

def compile_state_from_advanced_options(self):
street_and_num = False
if re.search(
r"street_number[\s]*\,[\s]*street", self.get_attr(ATTR_DISPLAY_OPTIONS)
):
street_and_num = True
self.street_num_i += 1
_LOGGER.debug(
"("
+ self.get_attr(CONF_NAME)
+ ") [compile_adv] Num and Street is True"
)
# street_and_num = False
# if re.search(
# r"street_number[\s]*\,[\s]*street", self.get_attr(ATTR_DISPLAY_OPTIONS)
# ):
# street_and_num = True
self.street_num_i += 1
# _LOGGER.debug(
# "("
# + self.get_attr(CONF_NAME)
# + ") [compile_adv] Num and Street is True"
# )
first = True
for i, out in enumerate(self.adv_options_state_list):
if out is not None and out:
Expand All @@ -2098,7 +2116,7 @@ def compile_state_from_advanced_options(self):
self.set_attr(ATTR_NATIVE_VALUE, str(out))
first = False
else:
if street_and_num and i == self.street_num_i:
if i == self.street_i and i == self.street_num_i:
self.set_attr(
ATTR_NATIVE_VALUE, self.get_attr(ATTR_NATIVE_VALUE) + " "
)
Expand Down Expand Up @@ -2761,6 +2779,7 @@ def do_update(self, reason):
display_options = None
self.adv_options_state_list = []
self.street_num_i = -1
self.street_i = -1
self.temp_i = 0
_LOGGER.debug(
"("
Expand Down

0 comments on commit 70d1b1d

Please sign in to comment.