Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use constants and update ordering (sensor.mqtt*) #3266

Merged
merged 1 commit into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions homeassistant/components/sensor/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@

import voluptuous as vol

import homeassistant.components.mqtt as mqtt
from homeassistant.components.mqtt import CONF_STATE_TOPIC, CONF_QOS
from homeassistant.const import (
CONF_NAME, CONF_VALUE_TEMPLATE, STATE_UNKNOWN, CONF_UNIT_OF_MEASUREMENT)
from homeassistant.components.mqtt import CONF_STATE_TOPIC, CONF_QOS
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers import template
from homeassistant.helpers.entity import Entity
import homeassistant.components.mqtt as mqtt
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'MQTT Sensor'
DEPENDENCIES = ['mqtt']

DEFAULT_NAME = "MQTT Sensor"

PLATFORM_SCHEMA = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
Expand All @@ -33,9 +32,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup MQTT Sensor."""
add_devices([MqttSensor(
hass,
config[CONF_NAME],
config[CONF_STATE_TOPIC],
config[CONF_QOS],
config.get(CONF_NAME),
config.get(CONF_STATE_TOPIC),
config.get(CONF_QOS),
config.get(CONF_UNIT_OF_MEASUREMENT),
config.get(CONF_VALUE_TEMPLATE),
)])
Expand Down
41 changes: 24 additions & 17 deletions homeassistant/components/sensor/mqtt_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import homeassistant.components.mqtt as mqtt
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, STATE_UNKNOWN
from homeassistant.const import (
CONF_NAME, STATE_UNKNOWN, CONF_TIMEOUT)
from homeassistant.components.mqtt import CONF_STATE_TOPIC
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
Expand All @@ -21,12 +22,18 @@

DEPENDENCIES = ['mqtt']

ATTR_DEVICE_ID = 'device_id'
ATTR_DISTANCE = 'distance'
ATTR_ID = 'id'
ATTR_ROOM = 'room'

CONF_DEVICE_ID = 'device_id'
CONF_TIMEOUT = 'timeout'
CONF_ROOM = 'room'

DEFAULT_TOPIC = 'room_presence'
DEFAULT_TIMEOUT = 5
DEFAULT_NAME = 'Room Sensor'
DEFAULT_TIMEOUT = 5
DEFAULT_TOPIC = 'room_presence'
DEPENDENCIES = ['mqtt']

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICE_ID): cv.string,
Expand All @@ -36,15 +43,15 @@
})

MQTT_PAYLOAD = vol.Schema(vol.All(json.loads, vol.Schema({
vol.Required('id'): cv.string,
vol.Required('distance'): vol.Coerce(float)
vol.Required(ATTR_ID): cv.string,
vol.Required(ATTR_DISTANCE): vol.Coerce(float),
}, extra=vol.ALLOW_EXTRA)))


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup MQTT Sensor."""
add_devices_callback([MQTTRoomSensor(
add_devices([MQTTRoomSensor(
hass,
config.get(CONF_NAME),
config.get(CONF_STATE_TOPIC),
Expand All @@ -62,7 +69,7 @@ def __init__(self, hass, name, state_topic, device_id, timeout):
self._state = STATE_UNKNOWN
self._hass = hass
self._name = name
self._state_topic = state_topic + '/+'
self._state_topic = '{}{}'.format(state_topic, '/+')
self._device_id = slugify(device_id).upper()
self._timeout = timeout
self._distance = None
Expand All @@ -86,7 +93,7 @@ def message_received(topic, payload, qos):
return

device = _parse_update_data(topic, data)
if device.get('device_id') == self._device_id:
if device.get(CONF_DEVICE_ID) == self._device_id:
if self._distance is None or self._updated is None:
update_state(**device)
else:
Expand All @@ -95,8 +102,8 @@ def message_received(topic, payload, qos):
# device is closer to another room OR
# last update from other room was too long ago
timediff = dt.utcnow() - self._updated
if device.get('room') == self._state \
or device.get('distance') < self._distance \
if device.get(ATTR_ROOM) == self._state \
or device.get(ATTR_DISTANCE) < self._distance \
or timediff.seconds >= self._timeout:
update_state(**device)

Expand All @@ -116,7 +123,7 @@ def name(self):
def device_state_attributes(self):
"""Return the state attributes."""
return {
'distance': self._distance
ATTR_DISTANCE: self._distance
}

@property
Expand All @@ -129,11 +136,11 @@ def _parse_update_data(topic, data):
"""Parse the room presence update."""
parts = topic.split('/')
room = parts[-1]
device_id = slugify(data.get('id')).upper()
device_id = slugify(data.get(ATTR_ID)).upper()
distance = data.get('distance')
parsed_data = {
'device_id': device_id,
'room': room,
'distance': distance
ATTR_DEVICE_ID: device_id,
ATTR_ROOM: room,
ATTR_DISTANCE: distance
}
return parsed_data
3 changes: 3 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
CONF_BEFORE = 'before'
CONF_BELOW = 'below'
CONF_BLACKLIST = 'blacklist'
CONF_BRIGHTNESS = 'brightness'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really have to do with this PR - accidentally committed?

CONF_CODE = 'code'
CONF_COMMAND = 'command'
CONF_COMMAND_CLOSE = 'command_close'
Expand Down Expand Up @@ -110,6 +111,7 @@
CONF_RECIPIENT = 'recipient'
CONF_RESOURCE = 'resource'
CONF_RESOURCES = 'resources'
CONF_RGB = 'rgb'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really have to do with this PR - accidentally committed?

CONF_SCAN_INTERVAL = 'scan_interval'
CONF_SENDER = 'sender'
CONF_SENSOR_CLASS = 'sensor_class'
Expand All @@ -120,6 +122,7 @@
CONF_SWITCHES = 'switches'
CONF_TEMPERATURE_UNIT = 'temperature_unit'
CONF_TIME_ZONE = 'time_zone'
CONF_TIMEOUT = 'timeout'
CONF_TOKEN = 'token'
CONF_TRIGGER_TIME = 'trigger_time'
CONF_UNIT_OF_MEASUREMENT = 'unit_of_measurement'
Expand Down