From a2f4366a749036eb7b4e0c14fac1a9d34d23098d Mon Sep 17 00:00:00 2001 From: Chen-IL <18098431+Chen-IL@users.noreply.github.com> Date: Sun, 6 Jun 2021 15:37:32 +0300 Subject: [PATCH] New Feature: language key new display option: 'place_name' --- README.md | 7 ++++++- custom_components/places/sensor.py | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f4538b5f..2ccbe521 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ sensor places_sharon: options: zone, place map_provider: apple map_zoom: 18 + language: de home_zone: zone.sharon_home api_key: !secret email_sharon @@ -60,6 +61,7 @@ sensor places_aidan: options: place map_provider: google map_zoom: 17 + language: jp,en home_zone: zone.aidan_home api_key: !secret email_aidan ``` @@ -75,7 +77,8 @@ Key | Type | Required | Description | Default | `api_key` | `string` | `False` | OpenStreetMap API key (your email address). | `no key` `map_provider` | `string` | `False` | `google` or `apple` | `apple` `map_zoom` | `number` | `False` | Level of zoom for the generated map link <1-20> | `18` -`options` | `string` | `False` | Display options: `zone, place, street_number, street, city, county, state, postal_code, country, formatted_address, do_not_show_not_home` | `zone, place` +`language` | `string` | `False` | Requested* language(s) for state and attributes. Two-Letter language code(s). | *Refer to Notes +`options` | `string` | `False` | Display options: `zone, place, place_name, street_number, street, city, county, state, postal_code, country, formatted_address, do_not_show_not_home` | `zone, place` Sample attributes that can be used in notifications, alerts, automations, etc: ```json @@ -166,6 +169,8 @@ Sample generic automations.yaml snippet to send an iOS notify on any device stat * The OpenStreetMap API requests that you include your valid e-mail address in each API call if you are making a large numbers of requests. They say that this information will be kept confidential and only used to contact you in the event of a problem, see their Usage Policy for more details. * The map link that gets generated for Google maps has a push pin marking the users location. * The map link for Apple maps is centered on the users location - but without any marker. +* When no `language` value is given, default language will be location's local language or English. When a comma separated list of languages is provided - the component will attempt to fill each address field in desired languages by order. +* Translations are partial in OpenStreetMap database. For each field, if a translation is missing in first requested language it will be resolved with a language following in the provided list, defaulting to local language if no matching translations were found for the list. * To enable detailed logging for this component, add the following to your configuration.yaml file ```yaml logger: diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index ed7e949a..4164b8ba 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -3,7 +3,7 @@ Original Author: Jim Thompson -Current Version: 1.2 20180510 - Jim Thompson +Current Version: 1.8 20210125 - iantrich 20180330 - Initial Release - Event driven and timed updates @@ -244,6 +244,7 @@ CONF_OPTIONS = 'options' CONF_MAP_PROVIDER = 'map_provider' CONF_MAP_ZOOM = 'map_zoom' +CONF_LANGUAGE = 'language' ATTR_OPTIONS = 'options' ATTR_STREET_NUMBER = 'street_number' @@ -283,6 +284,7 @@ DEFAULT_KEY = "no key" DEFAULT_MAP_PROVIDER = 'apple' DEFAULT_MAP_ZOOM = '18' +DEFAULT_LANGUAGE = 'default' SCAN_INTERVAL = timedelta(seconds=30) THROTTLE_INTERVAL = timedelta(seconds=600) @@ -295,6 +297,7 @@ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_MAP_PROVIDER, default=DEFAULT_MAP_PROVIDER): cv.string, vol.Optional(CONF_MAP_ZOOM, default=DEFAULT_MAP_ZOOM): cv.string, + vol.Optional(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): cv.string, vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period, }) @@ -309,14 +312,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None): home_zone = config.get(CONF_HOME_ZONE) map_provider = config.get(CONF_MAP_PROVIDER) map_zoom = config.get(CONF_MAP_ZOOM) + language = config.get(CONF_LANGUAGE) - add_devices([Places(hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom)]) + add_devices([Places(hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom, language)]) class Places(Entity): """Representation of a Places Sensor.""" - def __init__(self, hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom): + def __init__(self, hass, devicetracker_id, name, api_key, options, home_zone, map_provider, map_zoom, language): """Initialize the sensor.""" self._hass = hass self._name = name @@ -326,6 +330,8 @@ def __init__(self, hass, devicetracker_id, name, api_key, options, home_zone, ma self._home_zone = home_zone.lower() self._map_provider = map_provider.lower() self._map_zoom = map_zoom.lower() + self._language = language.lower() + self._language.replace(" ", "") self._state = "Initializing... (since 99:99)" home_latitude = str(hass.states.get(home_zone).attributes.get('latitude')) @@ -551,10 +557,7 @@ def do_update(self, reason): _LOGGER.debug( "(" + self._name + ") Map Link generated: " + self._map_link ) - if self._api_key == 'no key': - osm_url = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=" + self._latitude + "&lon=" + self._longitude + "&addressdetails=1&namedetails=1&zoom=18&limit=1" - else: - osm_url = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=" + self._latitude + "&lon=" + self._longitude + "&addressdetails=1&namedetails=1&zoom=18&limit=1&email=" + self._api_key + osm_url = "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=" + self._latitude + "&lon=" + self._longitude + ("&accept-language=" + self._language if self._language != DEFAULT_LANGUAGE else "") + "&addressdetails=1&namedetails=1&zoom=18&limit=1" + ("&email=" + self._api_key if self._api_key != DEFAULT_KEY else "") osm_decoded = {} _LOGGER.info( "(" + self._name + ") OpenStreetMap request sent with lat=" + self._latitude + " and lon=" + self._longitude) @@ -593,6 +596,10 @@ def do_update(self, reason): place_name = osm_decoded["address"][place_category] if "name" in osm_decoded["namedetails"]: place_name = osm_decoded["namedetails"]["name"] + for language in self._language.split(','): + if "name:" + language in osm_decoded["namedetails"]: + place_name = osm_decoded["namedetails"]["name:" + language] + break if "neighbourhood" in osm_decoded["address"]: place_neighbourhood = osm_decoded["address"]["neighbourhood"] if self._devicetracker_zone == 'not_home' and place_name != 'house': @@ -662,6 +669,9 @@ def do_update(self, reason): if "zone" in display_options and ("do_not_show_not_home" not in display_options and self._devicetracker_zone != "not_home"): zone = self._devicetracker_zone user_display.append(zone) + if "place_name" in display_options: + if place_name != "-": + user_display.append(place_name) if "place" in display_options: if place_name != "-": user_display.append(place_name)