Merge pull request #935 from balloob/0.11.1

0.11.1
This commit is contained in:
Paulus Schoutsen 2016-01-19 12:20:26 -08:00
commit a4ee2bd8ef
19 changed files with 224 additions and 166 deletions

View File

@ -24,11 +24,6 @@ DOMAIN = "mqtt"
MQTT_CLIENT = None MQTT_CLIENT = None
DEFAULT_PORT = 1883
DEFAULT_KEEPALIVE = 60
DEFAULT_QOS = 0
DEFAULT_RETAIN = False
SERVICE_PUBLISH = 'publish' SERVICE_PUBLISH = 'publish'
EVENT_MQTT_MESSAGE_RECEIVED = 'mqtt_message_received' EVENT_MQTT_MESSAGE_RECEIVED = 'mqtt_message_received'
@ -41,6 +36,16 @@ CONF_KEEPALIVE = 'keepalive'
CONF_USERNAME = 'username' CONF_USERNAME = 'username'
CONF_PASSWORD = 'password' CONF_PASSWORD = 'password'
CONF_CERTIFICATE = 'certificate' CONF_CERTIFICATE = 'certificate'
CONF_PROTOCOL = 'protocol'
PROTOCOL_31 = '3.1'
PROTOCOL_311 = '3.1.1'
DEFAULT_PORT = 1883
DEFAULT_KEEPALIVE = 60
DEFAULT_QOS = 0
DEFAULT_RETAIN = False
DEFAULT_PROTOCOL = PROTOCOL_311
ATTR_TOPIC = 'topic' ATTR_TOPIC = 'topic'
ATTR_PAYLOAD = 'payload' ATTR_PAYLOAD = 'payload'
@ -51,7 +56,7 @@ MAX_RECONNECT_WAIT = 300 # seconds
def publish(hass, topic, payload, qos=None, retain=None): def publish(hass, topic, payload, qos=None, retain=None):
""" Send an MQTT message. """ """Publish message to an MQTT topic."""
data = { data = {
ATTR_TOPIC: topic, ATTR_TOPIC: topic,
ATTR_PAYLOAD: payload, ATTR_PAYLOAD: payload,
@ -66,7 +71,7 @@ def publish(hass, topic, payload, qos=None, retain=None):
def subscribe(hass, topic, callback, qos=DEFAULT_QOS): def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
""" Subscribe to a topic. """ """Subscribe to an MQTT topic."""
def mqtt_topic_subscriber(event): def mqtt_topic_subscriber(event):
"""Match subscribed MQTT topic.""" """Match subscribed MQTT topic."""
if _match_topic(topic, event.data[ATTR_TOPIC]): if _match_topic(topic, event.data[ATTR_TOPIC]):
@ -78,8 +83,7 @@ def subscribe(hass, topic, callback, qos=DEFAULT_QOS):
def setup(hass, config): def setup(hass, config):
""" Get the MQTT protocol service. """ """Start the MQTT protocol service."""
if not validate_config(config, {DOMAIN: ['broker']}, _LOGGER): if not validate_config(config, {DOMAIN: ['broker']}, _LOGGER):
return False return False
@ -92,6 +96,12 @@ def setup(hass, config):
username = util.convert(conf.get(CONF_USERNAME), str) username = util.convert(conf.get(CONF_USERNAME), str)
password = util.convert(conf.get(CONF_PASSWORD), str) password = util.convert(conf.get(CONF_PASSWORD), str)
certificate = util.convert(conf.get(CONF_CERTIFICATE), str) certificate = util.convert(conf.get(CONF_CERTIFICATE), str)
protocol = util.convert(conf.get(CONF_PROTOCOL), str, DEFAULT_PROTOCOL)
if protocol not in (PROTOCOL_31, PROTOCOL_311):
_LOGGER.error('Invalid protocol specified: %s. Allowed values: %s, %s',
protocol, PROTOCOL_31, PROTOCOL_311)
return False
# For cloudmqtt.com, secured connection, auto fill in certificate # For cloudmqtt.com, secured connection, auto fill in certificate
if certificate is None and 19999 < port < 30000 and \ if certificate is None and 19999 < port < 30000 and \
@ -102,7 +112,7 @@ def setup(hass, config):
global MQTT_CLIENT global MQTT_CLIENT
try: try:
MQTT_CLIENT = MQTT(hass, broker, port, client_id, keepalive, username, MQTT_CLIENT = MQTT(hass, broker, port, client_id, keepalive, username,
password, certificate) password, certificate, protocol)
except socket.error: except socket.error:
_LOGGER.exception("Can't connect to the broker. " _LOGGER.exception("Can't connect to the broker. "
"Please check your settings and the broker " "Please check your settings and the broker "
@ -137,34 +147,37 @@ def setup(hass, config):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
class MQTT(object): class MQTT(object):
""" Implements messaging service for MQTT. """ """Home Assistant MQTT client."""
def __init__(self, hass, broker, port, client_id, keepalive, username, def __init__(self, hass, broker, port, client_id, keepalive, username,
password, certificate): password, certificate, protocol):
"""Initialize Home Assistant MQTT client."""
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
self.userdata = { self.hass = hass
'hass': hass, self.topics = {}
'topics': {}, self.progress = {}
'progress': {},
} if protocol == PROTOCOL_31:
proto = mqtt.MQTTv31
else:
proto = mqtt.MQTTv311
if client_id is None: if client_id is None:
self._mqttc = mqtt.Client(protocol=mqtt.MQTTv311) self._mqttc = mqtt.Client(protocol=proto)
else: else:
self._mqttc = mqtt.Client(client_id, protocol=mqtt.MQTTv311) self._mqttc = mqtt.Client(client_id, protocol=proto)
self._mqttc.user_data_set(self.userdata)
if username is not None: if username is not None:
self._mqttc.username_pw_set(username, password) self._mqttc.username_pw_set(username, password)
if certificate is not None: if certificate is not None:
self._mqttc.tls_set(certificate) self._mqttc.tls_set(certificate)
self._mqttc.on_subscribe = _mqtt_on_subscribe self._mqttc.on_subscribe = self._mqtt_on_subscribe
self._mqttc.on_unsubscribe = _mqtt_on_unsubscribe self._mqttc.on_unsubscribe = self._mqtt_on_unsubscribe
self._mqttc.on_connect = _mqtt_on_connect self._mqttc.on_connect = self._mqtt_on_connect
self._mqttc.on_disconnect = _mqtt_on_disconnect self._mqttc.on_disconnect = self._mqtt_on_disconnect
self._mqttc.on_message = _mqtt_on_message self._mqttc.on_message = self._mqtt_on_message
self._mqttc.connect(broker, port, keepalive) self._mqttc.connect(broker, port, keepalive)
@ -178,35 +191,31 @@ class MQTT(object):
def stop(self): def stop(self):
"""Stop the MQTT client.""" """Stop the MQTT client."""
self._mqttc.disconnect()
self._mqttc.loop_stop() self._mqttc.loop_stop()
def subscribe(self, topic, qos): def subscribe(self, topic, qos):
"""Subscribe to a topic.""" """Subscribe to a topic."""
if topic in self.userdata['topics']: assert isinstance(topic, str)
if topic in self.topics:
return return
result, mid = self._mqttc.subscribe(topic, qos) result, mid = self._mqttc.subscribe(topic, qos)
_raise_on_error(result) _raise_on_error(result)
self.userdata['progress'][mid] = topic self.progress[mid] = topic
self.userdata['topics'][topic] = None self.topics[topic] = None
def unsubscribe(self, topic): def unsubscribe(self, topic):
"""Unsubscribe from topic.""" """Unsubscribe from topic."""
result, mid = self._mqttc.unsubscribe(topic) result, mid = self._mqttc.unsubscribe(topic)
_raise_on_error(result) _raise_on_error(result)
self.userdata['progress'][mid] = topic self.progress[mid] = topic
def _mqtt_on_connect(self, _mqttc, _userdata, _flags, result_code):
"""On connect callback.
def _mqtt_on_message(mqttc, userdata, msg): Resubscribe to all topics we were subscribed to.
""" Message callback """ """
userdata['hass'].bus.fire(EVENT_MQTT_MESSAGE_RECEIVED, {
ATTR_TOPIC: msg.topic,
ATTR_QOS: msg.qos,
ATTR_PAYLOAD: msg.payload.decode('utf-8'),
})
def _mqtt_on_connect(mqttc, userdata, flags, result_code):
""" On connect, resubscribe to all topics we were subscribed to. """
if result_code != 0: if result_code != 0:
_LOGGER.error('Unable to connect to the MQTT broker: %s', { _LOGGER.error('Unable to connect to the MQTT broker: %s', {
1: 'Incorrect protocol version', 1: 'Incorrect protocol version',
@ -215,38 +224,52 @@ def _mqtt_on_connect(mqttc, userdata, flags, result_code):
4: 'Bad username or password', 4: 'Bad username or password',
5: 'Not authorised' 5: 'Not authorised'
}.get(result_code, 'Unknown reason')) }.get(result_code, 'Unknown reason'))
mqttc.disconnect() self._mqttc.disconnect()
return return
old_topics = userdata['topics'] old_topics = self.topics
userdata['topics'] = {} self.topics = {key: value for key, value in self.topics.items()
userdata['progress'] = {} if value is None}
for topic, qos in old_topics.items(): for topic, qos in old_topics.items():
# qos is None if we were in process of subscribing # qos is None if we were in process of subscribing
if qos is not None: if qos is not None:
mqttc.subscribe(topic, qos) self.subscribe(topic, qos)
def _mqtt_on_subscribe(self, _mqttc, _userdata, mid, granted_qos):
def _mqtt_on_subscribe(mqttc, userdata, mid, granted_qos): """Subscribe successful callback."""
""" Called when subscribe successful. """ topic = self.progress.pop(mid, None)
topic = userdata['progress'].pop(mid, None)
if topic is None: if topic is None:
return return
userdata['topics'][topic] = granted_qos self.topics[topic] = granted_qos[0]
def _mqtt_on_message(self, _mqttc, _userdata, msg):
"""Message received callback."""
self.hass.bus.fire(EVENT_MQTT_MESSAGE_RECEIVED, {
ATTR_TOPIC: msg.topic,
ATTR_QOS: msg.qos,
ATTR_PAYLOAD: msg.payload.decode('utf-8'),
})
def _mqtt_on_unsubscribe(mqttc, userdata, mid, granted_qos): def _mqtt_on_unsubscribe(self, _mqttc, _userdata, mid, granted_qos):
""" Called when subscribe successful. """ """Unsubscribe successful callback."""
topic = userdata['progress'].pop(mid, None) topic = self.progress.pop(mid, None)
if topic is None: if topic is None:
return return
userdata['topics'].pop(topic, None) self.topics.pop(topic, None)
def _mqtt_on_disconnect(self, _mqttc, _userdata, result_code):
"""Disconnected callback."""
self.progress = {}
self.topics = {key: value for key, value in self.topics.items()
if value is not None}
# Remove None values from topic list
for key in list(self.topics):
if self.topics[key] is None:
self.topics.pop(key)
def _mqtt_on_disconnect(mqttc, userdata, result_code):
""" Called when being disconnected. """
# When disconnected because of calling disconnect() # When disconnected because of calling disconnect()
if result_code == 0: if result_code == 0:
return return
@ -256,7 +279,7 @@ def _mqtt_on_disconnect(mqttc, userdata, result_code):
while True: while True:
try: try:
if mqttc.reconnect() == 0: if self._mqttc.reconnect() == 0:
_LOGGER.info('Successfully reconnected to the MQTT server') _LOGGER.info('Successfully reconnected to the MQTT server')
break break
except socket.error: except socket.error:
@ -278,7 +301,7 @@ def _raise_on_error(result):
def _match_topic(subscription, topic): def _match_topic(subscription, topic):
""" Returns if topic matches subscription. """ """Test if topic matches subscription."""
if subscription.endswith('#'): if subscription.endswith('#'):
return (subscription[:-2] == topic or return (subscription[:-2] == topic or
topic.startswith(subscription[:-1])) topic.startswith(subscription[:-1]))

View File

@ -72,8 +72,6 @@ DISCOVERY_COMPONENTS = [
def setup(hass, config): def setup(hass, config):
"""Setup the MySensors component.""" """Setup the MySensors component."""
# pylint: disable=too-many-locals
if not validate_config(config, if not validate_config(config,
{DOMAIN: [CONF_GATEWAYS]}, {DOMAIN: [CONF_GATEWAYS]},
_LOGGER): _LOGGER):

View File

@ -17,24 +17,24 @@ REQUIREMENTS = ['blockchain==1.1.2']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
OPTION_TYPES = { OPTION_TYPES = {
'wallet': ['Wallet balance', 'BTC'], 'wallet': ['Wallet balance', 'BTC'],
'exchangerate': ['Exchange rate (1 BTC)', ''], 'exchangerate': ['Exchange rate (1 BTC)', None],
'trade_volume_btc': ['Trade volume', 'BTC'], 'trade_volume_btc': ['Trade volume', 'BTC'],
'miners_revenue_usd': ['Miners revenue', 'USD'], 'miners_revenue_usd': ['Miners revenue', 'USD'],
'btc_mined': ['Mined', 'BTC'], 'btc_mined': ['Mined', 'BTC'],
'trade_volume_usd': ['Trade volume', 'USD'], 'trade_volume_usd': ['Trade volume', 'USD'],
'difficulty': ['Difficulty', ''], 'difficulty': ['Difficulty', None],
'minutes_between_blocks': ['Time between Blocks', 'min'], 'minutes_between_blocks': ['Time between Blocks', 'min'],
'number_of_transactions': ['No. of Transactions', ''], 'number_of_transactions': ['No. of Transactions', None],
'hash_rate': ['Hash rate', 'PH/s'], 'hash_rate': ['Hash rate', 'PH/s'],
'timestamp': ['Timestamp', ''], 'timestamp': ['Timestamp', None],
'mined_blocks': ['Minded Blocks', ''], 'mined_blocks': ['Minded Blocks', None],
'blocks_size': ['Block size', ''], 'blocks_size': ['Block size', None],
'total_fees_btc': ['Total fees', 'BTC'], 'total_fees_btc': ['Total fees', 'BTC'],
'total_btc_sent': ['Total sent', 'BTC'], 'total_btc_sent': ['Total sent', 'BTC'],
'estimated_btc_sent': ['Estimated sent', 'BTC'], 'estimated_btc_sent': ['Estimated sent', 'BTC'],
'total_btc': ['Total', 'BTC'], 'total_btc': ['Total', 'BTC'],
'total_blocks': ['Total Blocks', ''], 'total_blocks': ['Total Blocks', None],
'next_retarget': ['Next retarget', ''], 'next_retarget': ['Next retarget', None],
'estimated_transaction_volume_usd': ['Est. Transaction volume', 'USD'], 'estimated_transaction_volume_usd': ['Est. Transaction volume', 'USD'],
'miners_revenue_btc': ['Miners revenue', 'BTC'], 'miners_revenue_btc': ['Miners revenue', 'BTC'],
'market_price_usd': ['Market price', 'USD'] 'market_price_usd': ['Market price', 'USD']

View File

@ -20,7 +20,7 @@ REQUIREMENTS = ['http://github.com/mala-zaba/Adafruit_Python_DHT/archive/'
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = { SENSOR_TYPES = {
'temperature': ['Temperature', ''], 'temperature': ['Temperature', None],
'humidity': ['Humidity', '%'] 'humidity': ['Humidity', '%']
} }
# Return cached results if last scan was less then this time ago # Return cached results if last scan was less then this time ago

View File

@ -36,7 +36,7 @@ DEPENDENCIES = ['ecobee']
SENSOR_TYPES = { SENSOR_TYPES = {
'temperature': ['Temperature', TEMP_FAHRENHEIT], 'temperature': ['Temperature', TEMP_FAHRENHEIT],
'humidity': ['Humidity', '%'], 'humidity': ['Humidity', '%'],
'occupancy': ['Occupancy', ''] 'occupancy': ['Occupancy', None]
} }
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -16,8 +16,8 @@ _LOGGER = logging.getLogger(__name__)
_RESOURCE = 'https://engage.efergy.com/mobile_proxy/' _RESOURCE = 'https://engage.efergy.com/mobile_proxy/'
SENSOR_TYPES = { SENSOR_TYPES = {
'instant_readings': ['Energy Usage', 'kW'], 'instant_readings': ['Energy Usage', 'kW'],
'budget': ['Energy Budget', ''], 'budget': ['Energy Budget', None],
'cost': ['Energy Cost', ''], 'cost': ['Energy Cost', None],
} }

View File

@ -19,13 +19,13 @@ _LOGGER = logging.getLogger(__name__)
# Sensor types are defined like so: # Sensor types are defined like so:
# Name, si unit, us unit, ca unit, uk unit, uk2 unit # Name, si unit, us unit, ca unit, uk unit, uk2 unit
SENSOR_TYPES = { SENSOR_TYPES = {
'summary': ['Summary', '', '', '', '', ''], 'summary': ['Summary', None, None, None, None, None],
'icon': ['Icon', '', '', '', '', ''], 'icon': ['Icon', None, None, None, None, None],
'nearest_storm_distance': ['Nearest Storm Distance', 'nearest_storm_distance': ['Nearest Storm Distance',
'km', 'm', 'km', 'km', 'm'], 'km', 'm', 'km', 'km', 'm'],
'nearest_storm_bearing': ['Nearest Storm Bearing', 'nearest_storm_bearing': ['Nearest Storm Bearing',
'°', '°', '°', '°', '°'], '°', '°', '°', '°', '°'],
'precip_type': ['Precip', '', '', '', '', ''], 'precip_type': ['Precip', None, None, None, None, None],
'precip_intensity': ['Precip Intensity', 'mm', 'in', 'mm', 'mm', 'mm'], 'precip_intensity': ['Precip Intensity', 'mm', 'in', 'mm', 'mm', 'mm'],
'precip_probability': ['Precip Probability', '%', '%', '%', '%', '%'], 'precip_probability': ['Precip Probability', '%', '%', '%', '%', '%'],
'temperature': ['Temperature', '°C', '°F', '°C', '°C', '°C'], 'temperature': ['Temperature', '°C', '°F', '°C', '°C', '°C'],

View File

@ -31,11 +31,11 @@ SENSOR_TYPES = {
'swap_use_percent': ['Swap Use', '%'], 'swap_use_percent': ['Swap Use', '%'],
'swap_use': ['Swap Use', 'GiB'], 'swap_use': ['Swap Use', 'GiB'],
'swap_free': ['Swap Free', 'GiB'], 'swap_free': ['Swap Free', 'GiB'],
'processor_load': ['CPU Load', ''], 'processor_load': ['CPU Load', None],
'process_running': ['Running', ''], 'process_running': ['Running', None],
'process_total': ['Total', ''], 'process_total': ['Total', None],
'process_thread': ['Thread', ''], 'process_thread': ['Thread', None],
'process_sleeping': ['Sleeping', ''] 'process_sleeping': ['Sleeping', None]
} }
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -33,6 +33,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
# Define the S_TYPES and V_TYPES that the platform should handle as # Define the S_TYPES and V_TYPES that the platform should handle as
# states. # states.
s_types = [ s_types = [
gateway.const.Presentation.S_DOOR,
gateway.const.Presentation.S_MOTION,
gateway.const.Presentation.S_SMOKE,
gateway.const.Presentation.S_TEMP, gateway.const.Presentation.S_TEMP,
gateway.const.Presentation.S_HUM, gateway.const.Presentation.S_HUM,
gateway.const.Presentation.S_BARO, gateway.const.Presentation.S_BARO,
@ -59,6 +62,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
s_types.extend([ s_types.extend([
gateway.const.Presentation.S_COLOR_SENSOR, gateway.const.Presentation.S_COLOR_SENSOR,
gateway.const.Presentation.S_MULTIMETER, gateway.const.Presentation.S_MULTIMETER,
gateway.const.Presentation.S_SPRINKLER,
gateway.const.Presentation.S_WATER_LEAK,
gateway.const.Presentation.S_SOUND,
gateway.const.Presentation.S_VIBRATION,
gateway.const.Presentation.S_MOISTURE,
]) ])
not_v_types.extend([gateway.const.SetReq.V_STATUS, ]) not_v_types.extend([gateway.const.SetReq.V_STATUS, ])
v_types = [member for member in gateway.const.SetReq v_types = [member for member in gateway.const.SetReq

View File

@ -16,8 +16,8 @@ from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['pyowm==2.3.0'] REQUIREMENTS = ['pyowm==2.3.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SENSOR_TYPES = { SENSOR_TYPES = {
'weather': ['Condition', ''], 'weather': ['Condition', None],
'temperature': ['Temperature', ''], 'temperature': ['Temperature', None],
'wind_speed': ['Wind speed', 'm/s'], 'wind_speed': ['Wind speed', 'm/s'],
'humidity': ['Humidity', '%'], 'humidity': ['Humidity', '%'],
'pressure': ['Pressure', 'mbar'], 'pressure': ['Pressure', 'mbar'],
@ -71,7 +71,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
pass pass
if forecast == 1: if forecast == 1:
SENSOR_TYPES['forecast'] = ['Forecast', ''] SENSOR_TYPES['forecast'] = ['Forecast', None]
dev.append(OpenWeatherMapSensor(data, 'forecast', unit)) dev.append(OpenWeatherMapSensor(data, 'forecast', unit))
add_devices(dev) add_devices(dev)

View File

@ -17,7 +17,7 @@ REQUIREMENTS = ['https://github.com/jamespcole/home-assistant-nzb-clients/'
'#python-sabnzbd==0.1'] '#python-sabnzbd==0.1']
SENSOR_TYPES = { SENSOR_TYPES = {
'current_status': ['Status', ''], 'current_status': ['Status', None],
'speed': ['Speed', 'MB/s'], 'speed': ['Speed', 'MB/s'],
'queue_size': ['Queue', 'MB'], 'queue_size': ['Queue', 'MB'],
'queue_remaining': ['Left', 'MB'], 'queue_remaining': ['Left', 'MB'],

View File

@ -18,7 +18,6 @@ from homeassistant.components import tellduslive
ATTR_LAST_UPDATED = "time_last_updated" ATTR_LAST_UPDATED = "time_last_updated"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['tellduslive']
SENSOR_TYPE_TEMP = "temp" SENSOR_TYPE_TEMP = "temp"
SENSOR_TYPE_HUMIDITY = "humidity" SENSOR_TYPE_HUMIDITY = "humidity"
@ -43,6 +42,8 @@ SENSOR_TYPES = {
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up Tellstick sensors. """ """ Sets up Tellstick sensors. """
if discovery_info is None:
return
sensors = tellduslive.NETWORK.get_sensors() sensors = tellduslive.NETWORK.get_sensors()
devices = [] devices = []

View File

@ -15,7 +15,7 @@ from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['transmissionrpc==0.11'] REQUIREMENTS = ['transmissionrpc==0.11']
SENSOR_TYPES = { SENSOR_TYPES = {
'current_status': ['Status', ''], 'current_status': ['Status', None],
'download_speed': ['Down Speed', 'MB/s'], 'download_speed': ['Down Speed', 'MB/s'],
'upload_speed': ['Up Speed', 'MB/s'] 'upload_speed': ['Up Speed', 'MB/s']
} }

View File

@ -67,7 +67,7 @@ class VerisureThermometer(Entity):
return TEMP_CELCIUS # can verisure report in fahrenheit? return TEMP_CELCIUS # can verisure report in fahrenheit?
def update(self): def update(self):
''' update sensor ''' """ update sensor """
verisure.update_climate() verisure.update_climate()
@ -96,5 +96,5 @@ class VerisureHygrometer(Entity):
return "%" return "%"
def update(self): def update(self):
''' update sensor ''' """ update sensor """
verisure.update_climate() verisure.update_climate()

View File

@ -21,7 +21,7 @@ REQUIREMENTS = ['xmltodict']
# Sensor types are defined like so: # Sensor types are defined like so:
SENSOR_TYPES = { SENSOR_TYPES = {
'symbol': ['Symbol', ''], 'symbol': ['Symbol', None],
'precipitation': ['Condition', 'mm'], 'precipitation': ['Condition', 'mm'],
'temperature': ['Temperature', '°C'], 'temperature': ['Temperature', '°C'],
'windSpeed': ['Wind speed', 'm/s'], 'windSpeed': ['Wind speed', 'm/s'],

View File

@ -15,11 +15,12 @@ from homeassistant.components import tellduslive
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['tellduslive']
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Find and return Tellstick switches. """ """ Find and return Tellstick switches. """
if discovery_info is None:
return
switches = tellduslive.NETWORK.get_switches() switches = tellduslive.NETWORK.get_switches()
add_devices([TelldusLiveSwitch(switch["name"], add_devices([TelldusLiveSwitch(switch["name"],
switch["id"]) switch["id"])

View File

@ -1,7 +1,7 @@
# coding: utf-8 # coding: utf-8
""" Constants used by Home Assistant components. """ """ Constants used by Home Assistant components. """
__version__ = "0.11.0" __version__ = "0.11.1"
# Can be used to specify a catch all when registering state or event listeners. # Can be used to specify a catch all when registering state or event listeners.
MATCH_ALL = '*' MATCH_ALL = '*'

View File

@ -101,17 +101,18 @@ class Entity(object):
state = str(self.state) state = str(self.state)
attr = self.state_attributes or {} attr = self.state_attributes or {}
if ATTR_FRIENDLY_NAME not in attr and self.name: if ATTR_FRIENDLY_NAME not in attr and self.name is not None:
attr[ATTR_FRIENDLY_NAME] = self.name attr[ATTR_FRIENDLY_NAME] = str(self.name)
if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement: if ATTR_UNIT_OF_MEASUREMENT not in attr and \
attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement self.unit_of_measurement is not None:
attr[ATTR_UNIT_OF_MEASUREMENT] = str(self.unit_of_measurement)
if ATTR_ICON not in attr and self.icon: if ATTR_ICON not in attr and self.icon is not None:
attr[ATTR_ICON] = self.icon attr[ATTR_ICON] = str(self.icon)
if self.hidden: if self.hidden:
attr[ATTR_HIDDEN] = self.hidden attr[ATTR_HIDDEN] = bool(self.hidden)
# overwrite properties that have been set in the config file # overwrite properties that have been set in the config file
attr.update(_OVERWRITE.get(self.entity_id, {})) attr.update(_OVERWRITE.get(self.entity_id, {}))

View File

@ -144,8 +144,15 @@ class TestMQTTCallbacks(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant(1) self.hass = get_test_home_assistant(1)
mock_mqtt_component(self.hass) # mock_mqtt_component(self.hass)
self.calls = []
with mock.patch('paho.mqtt.client.Client'):
mqtt.setup(self.hass, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'mock-broker',
}
})
self.hass.config.components.append(mqtt.DOMAIN)
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """ """ Stop down stuff we started. """
@ -162,7 +169,7 @@ class TestMQTTCallbacks(unittest.TestCase):
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload']) MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8')) message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8'))
mqtt._mqtt_on_message(None, {'hass': self.hass}, message) mqtt.MQTT_CLIENT._mqtt_on_message(None, {'hass': self.hass}, message)
self.hass.pool.block_till_done() self.hass.pool.block_till_done()
self.assertEqual(1, len(calls)) self.assertEqual(1, len(calls))
@ -173,36 +180,55 @@ class TestMQTTCallbacks(unittest.TestCase):
def test_mqtt_failed_connection_results_in_disconnect(self): def test_mqtt_failed_connection_results_in_disconnect(self):
for result_code in range(1, 6): for result_code in range(1, 6):
mqttc = mock.MagicMock() mqtt.MQTT_CLIENT._mqttc = mock.MagicMock()
mqtt._mqtt_on_connect(mqttc, {'topics': {}}, 0, result_code) mqtt.MQTT_CLIENT._mqtt_on_connect(None, {'topics': {}}, 0,
self.assertTrue(mqttc.disconnect.called) result_code)
self.assertTrue(mqtt.MQTT_CLIENT._mqttc.disconnect.called)
def test_mqtt_subscribes_topics_on_connect(self): def test_mqtt_subscribes_topics_on_connect(self):
prev_topics = { from collections import OrderedDict
'topic/test': 1, prev_topics = OrderedDict()
'home/sensor': 2, prev_topics['topic/test'] = 1,
'still/pending': None prev_topics['home/sensor'] = 2,
} prev_topics['still/pending'] = None
mqttc = mock.MagicMock()
mqtt._mqtt_on_connect(mqttc, {'topics': prev_topics}, 0, 0) mqtt.MQTT_CLIENT.topics = prev_topics
self.assertFalse(mqttc.disconnect.called) mqtt.MQTT_CLIENT.progress = {1: 'still/pending'}
# Return values for subscribe calls (rc, mid)
mqtt.MQTT_CLIENT._mqttc.subscribe.side_effect = ((0, 2), (0, 3))
mqtt.MQTT_CLIENT._mqtt_on_connect(None, None, 0, 0)
self.assertFalse(mqtt.MQTT_CLIENT._mqttc.disconnect.called)
expected = [(topic, qos) for topic, qos in prev_topics.items() expected = [(topic, qos) for topic, qos in prev_topics.items()
if qos is not None] if qos is not None]
self.assertEqual(expected, [call[1] for call self.assertEqual(
in mqttc.subscribe.mock_calls]) expected,
[call[1] for call in mqtt.MQTT_CLIENT._mqttc.subscribe.mock_calls])
self.assertEqual({
1: 'still/pending',
2: 'topic/test',
3: 'home/sensor',
}, mqtt.MQTT_CLIENT.progress)
def test_mqtt_disconnect_tries_no_reconnect_on_stop(self): def test_mqtt_disconnect_tries_no_reconnect_on_stop(self):
mqttc = mock.MagicMock() mqtt.MQTT_CLIENT._mqtt_on_disconnect(None, None, 0)
mqtt._mqtt_on_disconnect(mqttc, {}, 0) self.assertFalse(mqtt.MQTT_CLIENT._mqttc.reconnect.called)
self.assertFalse(mqttc.reconnect.called)
@mock.patch('homeassistant.components.mqtt.time.sleep') @mock.patch('homeassistant.components.mqtt.time.sleep')
def test_mqtt_disconnect_tries_reconnect(self, mock_sleep): def test_mqtt_disconnect_tries_reconnect(self, mock_sleep):
mqttc = mock.MagicMock() mqtt.MQTT_CLIENT.topics = {
mqttc.reconnect.side_effect = [1, 1, 1, 0] 'test/topic': 1,
mqtt._mqtt_on_disconnect(mqttc, {}, 1) 'test/progress': None
self.assertTrue(mqttc.reconnect.called) }
self.assertEqual(4, len(mqttc.reconnect.mock_calls)) mqtt.MQTT_CLIENT.progress = {
1: 'test/progress'
}
mqtt.MQTT_CLIENT._mqttc.reconnect.side_effect = [1, 1, 1, 0]
mqtt.MQTT_CLIENT._mqtt_on_disconnect(None, None, 1)
self.assertTrue(mqtt.MQTT_CLIENT._mqttc.reconnect.called)
self.assertEqual(4, len(mqtt.MQTT_CLIENT._mqttc.reconnect.mock_calls))
self.assertEqual([1, 2, 4], self.assertEqual([1, 2, 4],
[call[1][0] for call in mock_sleep.mock_calls]) [call[1][0] for call in mock_sleep.mock_calls])
self.assertEqual({'test/topic': 1}, mqtt.MQTT_CLIENT.topics)
self.assertEqual({}, mqtt.MQTT_CLIENT.progress)