Skip to content

Commit

Permalink
Merge pull request #4 from gregoryduckworth/language
Browse files Browse the repository at this point in the history
 Update to use language and region
  • Loading branch information
gregoryduckworth authored Dec 19, 2019
2 parents 5aa921e + 25885c3 commit a1e0ac3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ image (Optional): A link to an image which if provided, will override `picture`

api_key (Optional): Your application’s API key (get one by following the instructions below). This key identifies your application for purposes of quota management. Most users will not need to use this unless multiple sensors are created.

language (Optional): The language with which you want to display the results from [Google Maps](https://developers.google.com/maps/documentation/javascript/localization#Language)

region (Optional): The region with which you want to display the results from [Google Maps](https://developers.google.com/maps/documentation/javascript/localization#Region)

scan_interval (Optional): The frequency with which scans occur in seconds, the default is 60.

You need to register for an API key to use Google Geocode. This can be done by following these instructions
Expand Down
39 changes: 25 additions & 14 deletions custom_components/google_geocode/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
CONF_ATTRIBUTION = "Data provided by maps.google.com"
CONF_GRAVATAR = 'gravatar'
CONF_IMAGE = 'image'
CONF_GOOGLE_LANGUAGE = 'language'
CONF_GOOGLE_REGION = 'region'

ATTR_STREET_NUMBER = 'Street Number'
ATTR_STREET = 'Street'
Expand All @@ -41,6 +43,8 @@

DEFAULT_NAME = 'Google Geocode'
DEFAULT_OPTION = 'street, city'
DEFAULT_LANGUAGE = 'en-GB'
DEFAULT_REGION = 'GB'
DEFAULT_DISPLAY_ZONE = 'display'
DEFAULT_KEY = 'no key'
current = '0,0'
Expand All @@ -51,12 +55,13 @@
vol.Required(CONF_ORIGIN): cv.string,
vol.Optional(CONF_API_KEY, default=DEFAULT_KEY): cv.string,
vol.Optional(CONF_OPTIONS, default=DEFAULT_OPTION): cv.string,
vol.Optional(CONF_GOOGLE_LANGUAGE, default=DEFAULT_LANGUAGE): cv.string,
vol.Optional(CONF_GOOGLE_REGION, default=DEFAULT_REGION): cv.string,
vol.Optional(CONF_DISPLAY_ZONE, default=DEFAULT_DISPLAY_ZONE): cv.string,
vol.Optional(CONF_GRAVATAR, default=None): vol.Any(None, cv.string),
vol.Optional(CONF_IMAGE, default=None): vol.Any(None, cv.string),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL):
cv.time_period,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
})

TRACKABLE_DOMAINS = ['device_tracker', 'sensor', 'person']
Expand All @@ -67,22 +72,27 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
api_key = config.get(CONF_API_KEY)
origin = config.get(CONF_ORIGIN)
options = config.get(CONF_OPTIONS)
google_language = config.get(CONF_GOOGLE_LANGUAGE)
google_region = config.get(CONF_GOOGLE_REGION)
display_zone = config.get(CONF_DISPLAY_ZONE)
gravatar = config.get(CONF_GRAVATAR)
image = config.get(CONF_IMAGE)

add_devices([GoogleGeocode(hass, origin, name, api_key, options, display_zone, gravatar, image)])

add_devices([GoogleGeocode(hass, origin, name, api_key, options, google_language, google_region, display_zone, gravatar, image)])

class GoogleGeocode(Entity):
"""Representation of a Google Geocode Sensor."""

def __init__(self, hass, origin, name, api_key, options, display_zone, gravatar, image):
def __init__(self, hass, origin, name, api_key, options, google_language, google_region, display_zone, gravatar, image):
_LOGGER.debug("Language - first: " + google_language)
_LOGGER.debug("Region - first: " + google_region)
"""Initialize the sensor."""
self._hass = hass
self._name = name
self._api_key = api_key
self._options = options.lower()
self._google_language = google_language.lower()
self._google_region = google_region.lower()
self._display_zone = display_zone.lower()
self._state = "Awaiting Update"
self._gravatar = gravatar
Expand Down Expand Up @@ -127,7 +137,7 @@ def state(self):
def entity_picture(self):
"""Return the picture of the device."""
return self._picture

@property
def device_state_attributes(self):
"""Return the state attributes."""
Expand Down Expand Up @@ -174,15 +184,18 @@ def update(self):
pass
else:
_LOGGER.info("google request sent")
_LOGGER.debug("Language:" + self._google_language)
_LOGGER.debug("Region:" + self._google_region)
self._zone_check_current = self.hass.states.get(self._origin_entity_id).state
zone_check_count = 2
lat = self._origin
current = lat
self._reset_attributes()
if self._api_key == 'no key':
url = "https://maps.google.com/maps/api/geocode/json?latlng=" + lat
url = "https://maps.google.com/maps/api/geocode/json?language=" + self._google_language + "&region=" + self._google_region + "&latlng=" + lat
else:
url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "&key=" + self._api_key
url = "https://maps.googleapis.com/maps/api/geocode/json?language=" + self._google_language + "&region=" + self._google_region + "&latlng=" + lat + "&key=" + self._api_key
_LOGGER.debug(url)
response = get(url)
json_input = response.text
decoded = json.loads(json_input)
Expand All @@ -196,8 +209,6 @@ def update(self):
county = ''
country = ''



for result in decoded["results"]:
for component in result["address_components"]:
if 'street_number' in component["types"]:
Expand Down Expand Up @@ -267,9 +278,9 @@ def update(self):
self._append_to_user_display(country)
if "formatted_address" in display_options:
self._append_to_user_display(formatted_address)

user_display = ', '.join( x for x in user_display )

if user_display == '':
user_display = street
self._state = user_display
Expand Down Expand Up @@ -315,15 +326,15 @@ def _get_location_from_attributes(entity):
"""Get the lat/long string from an entities attributes."""
attr = entity.attributes
return "%s,%s" % (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE))

def _get_gravatar_for_email(self, email: str):
"""Return an 80px Gravatar for the given email address.
Async friendly.
"""
import hashlib
url = 'https://www.gravatar.com/avatar/{}.jpg?s=80&d=wavatar'
return url.format(hashlib.md5(email.encode('utf-8').lower()).hexdigest())

def _get_image_from_url(self, url: str):
"""Return an image from a given url.
Async friendly.
Expand Down

0 comments on commit a1e0ac3

Please sign in to comment.