Skip to content

Commit

Permalink
Merge pull request #26 from Chen-IL/language-1
Browse files Browse the repository at this point in the history
New Feature: language key
  • Loading branch information
iantrich authored Aug 12, 2021
2 parents 7d6625d + a2f4366 commit 4c352c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 17 additions & 7 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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,
})

Expand All @@ -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
Expand All @@ -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'))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4c352c4

Please sign in to comment.