Support adding different server locations for Microsoft face component (#7532)

* Support adding different server locations

* Rename variables and move CONF_ const into component as requested in review

* Fix unittests

* Forgot to add tests for microsoft_face_identify
This commit is contained in:
Tsvi Mostovicz 2017-05-12 11:53:25 +03:00 committed by Pascal Vizeli
parent 8da10f670b
commit 452c3a1b25
4 changed files with 49 additions and 38 deletions

View file

@ -28,10 +28,12 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = 'microsoft_face'
DEPENDENCIES = ['camera']
FACE_API_URL = "https://westus.api.cognitive.microsoft.com/face/v1.0/{0}"
FACE_API_URL = "api.cognitive.microsoft.com/face/v1.0/{0}"
DATA_MICROSOFT_FACE = 'microsoft_face'
CONF_AZURE_REGION = 'azure_region'
SERVICE_CREATE_GROUP = 'create_group'
SERVICE_DELETE_GROUP = 'delete_group'
SERVICE_TRAIN_GROUP = 'train_group'
@ -49,6 +51,7 @@ DEFAULT_TIMEOUT = 10
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_AZURE_REGION, default="westus"): cv.string,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
}),
}, extra=vol.ALLOW_EXTRA)
@ -115,6 +118,7 @@ def async_setup(hass, config):
entities = {}
face = MicrosoftFace(
hass,
config[DOMAIN].get(CONF_AZURE_REGION),
config[DOMAIN].get(CONF_API_KEY),
config[DOMAIN].get(CONF_TIMEOUT),
entities
@ -304,12 +308,13 @@ class MicrosoftFaceGroupEntity(Entity):
class MicrosoftFace(object):
"""Microsoft Face api for HomeAssistant."""
def __init__(self, hass, api_key, timeout, entities):
def __init__(self, hass, server_loc, api_key, timeout, entities):
"""Initialize Microsoft Face api."""
self.hass = hass
self.websession = async_get_clientsession(hass)
self.timeout = timeout
self._api_key = api_key
self._server_url = "https://{0}.{1}".format(server_loc, FACE_API_URL)
self._store = {}
self._entities = entities
@ -346,7 +351,7 @@ class MicrosoftFace(object):
params=None):
"""Make a api call."""
headers = {"Ocp-Apim-Subscription-Key": self._api_key}
url = FACE_API_URL.format(function)
url = self._server_url.format(function)
payload = None
if binary: