Skip to content

Commit

Permalink
automatically use bundled certificate it set to auto (home-assistant#…
Browse files Browse the repository at this point in the history
…6707)

* automatically use bundled certificate if certificate-parameter is set to 'auto' and seperate this from which port is specified

* Fix travis error and long lines

* Update __init__.py
  • Loading branch information
printzlau authored and balloob committed Mar 19, 2017
1 parent acf75b5 commit 1cb2a6a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def valid_discovery_topic(value):
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_CERTIFICATE): cv.isfile,
vol.Optional(CONF_CERTIFICATE): vol.Any('auto', cv.isfile),
vol.Inclusive(CONF_CLIENT_KEY, 'client_key_auth',
msg=CLIENT_KEY_AUTH_MSG): cv.isfile,
vol.Inclusive(CONF_CLIENT_CERT, 'client_key_auth',
Expand Down Expand Up @@ -317,8 +317,8 @@ def async_setup(hass, config):
certificate = os.path.join(os.path.dirname(__file__),
'addtrustexternalcaroot.crt')

# When the port indicates mqtts, use bundled certificates from requests
if certificate is None and port == 8883:
# When the certificate is set to auto, use bundled certs from requests
if certificate == 'auto':
certificate = requests.certs.where()

will_message = conf.get(CONF_WILL_MESSAGE)
Expand Down
15 changes: 7 additions & 8 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,33 +377,32 @@ def test_setup_fails_if_no_connect_broker(hass):


@asyncio.coroutine
def test_setup_uses_certificate_on_mqtts_port(hass):
"""Test setup uses bundled certificates when mqtts port is requested."""
def test_setup_uses_certificate_on_certificate_set_to_auto(hass):
"""Test setup uses bundled certs when certificate is set to auto."""
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker',
'port': 8883}}
'certificate': 'auto'}}

with mock.patch('homeassistant.components.mqtt.MQTT') as mock_MQTT:
yield from async_setup_component(hass, mqtt.DOMAIN, test_broker_cfg)

assert mock_MQTT.called
assert mock_MQTT.mock_calls[0][1][2] == 8883

import requests.certs
expectedCertificate = requests.certs.where()
assert mock_MQTT.mock_calls[0][1][7] == expectedCertificate


@asyncio.coroutine
def test_setup_uses_certificate_not_on_mqtts_port(hass):
"""Test setup doesn't use bundled certificates when not mqtts port."""
def test_setup_does_not_use_certificate_on_mqtts_port(hass):
"""Test setup doesn't use bundled certs when certificate is not set."""
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker',
'port': 1883}}
'port': 8883}}

with mock.patch('homeassistant.components.mqtt.MQTT') as mock_MQTT:
yield from async_setup_component(hass, mqtt.DOMAIN, test_broker_cfg)

assert mock_MQTT.called
assert mock_MQTT.mock_calls[0][1][2] == 1883
assert mock_MQTT.mock_calls[0][1][2] == 8883

import requests.certs
mqttsCertificateBundle = requests.certs.where()
Expand Down

0 comments on commit 1cb2a6a

Please sign in to comment.