Skip to content

Commit

Permalink
Small refactoring of MQTT light (#19009)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored and balloob committed Dec 5, 2018
1 parent 12f222b commit 69fd3aa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 101 deletions.
95 changes: 48 additions & 47 deletions homeassistant/components/light/mqtt/schema_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ def __init__(self, config, discovery_hash):
self._white_value = None
self._supported_features = 0

self._name = None
self._effect_list = None
self._topic = None
self._qos = None
self._retain = None
self._payload = None
self._templates = None
self._optimistic = False
Expand All @@ -146,9 +142,6 @@ def __init__(self, config, discovery_hash):
self._optimistic_hs = False
self._optimistic_white_value = False
self._optimistic_xy = False
self._brightness_scale = None
self._white_value_scale = None
self._on_command_type = None
self._unique_id = config.get(CONF_UNIQUE_ID)

# Load config
Expand All @@ -157,8 +150,9 @@ def __init__(self, config, discovery_hash):
availability_topic = config.get(CONF_AVAILABILITY_TOPIC)
payload_available = config.get(CONF_PAYLOAD_AVAILABLE)
payload_not_available = config.get(CONF_PAYLOAD_NOT_AVAILABLE)
qos = config.get(CONF_QOS)

MqttAvailability.__init__(self, availability_topic, self._qos,
MqttAvailability.__init__(self, availability_topic, qos,
payload_available, payload_not_available)
MqttDiscoveryUpdate.__init__(self, discovery_hash,
self.discovery_update)
Expand All @@ -178,8 +172,8 @@ async def discovery_update(self, discovery_payload):

def _setup_from_config(self, config):
"""(Re)Setup the entity."""
self._name = config.get(CONF_NAME)
self._effect_list = config.get(CONF_EFFECT_LIST)
self._config = config

topic = {
key: config.get(key) for key in (
CONF_BRIGHTNESS_COMMAND_TOPIC,
Expand All @@ -201,8 +195,6 @@ def _setup_from_config(self, config):
)
}
self._topic = topic
self._qos = config.get(CONF_QOS)
self._retain = config.get(CONF_RETAIN)
self._payload = {
'on': config.get(CONF_PAYLOAD_ON),
'off': config.get(CONF_PAYLOAD_OFF),
Expand Down Expand Up @@ -240,10 +232,6 @@ def _setup_from_config(self, config):
self._optimistic_xy = \
optimistic or topic[CONF_XY_STATE_TOPIC] is None

self._brightness_scale = config.get(CONF_BRIGHTNESS_SCALE)
self._white_value_scale = config.get(CONF_WHITE_VALUE_SCALE)
self._on_command_type = config.get(CONF_ON_COMMAND_TYPE)

self._supported_features = 0
self._supported_features |= (
topic[CONF_RGB_COMMAND_TOPIC] is not None and
Expand Down Expand Up @@ -296,7 +284,7 @@ def state_received(topic, payload, qos):
topics[CONF_STATE_TOPIC] = {
'topic': self._topic[CONF_STATE_TOPIC],
'msg_callback': state_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
elif self._optimistic and last_state:
self._state = last_state.state == STATE_ON

Expand All @@ -310,15 +298,16 @@ def brightness_received(topic, payload, qos):
return

device_value = float(payload)
percent_bright = device_value / self._brightness_scale
percent_bright = \
device_value / self._config.get(CONF_BRIGHTNESS_SCALE)
self._brightness = int(percent_bright * 255)
self.async_schedule_update_ha_state()

if self._topic[CONF_BRIGHTNESS_STATE_TOPIC] is not None:
topics[CONF_BRIGHTNESS_STATE_TOPIC] = {
'topic': self._topic[CONF_BRIGHTNESS_STATE_TOPIC],
'msg_callback': brightness_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._brightness = 255
elif self._optimistic_brightness and last_state\
and last_state.attributes.get(ATTR_BRIGHTNESS):
Expand Down Expand Up @@ -348,7 +337,7 @@ def rgb_received(topic, payload, qos):
topics[CONF_RGB_STATE_TOPIC] = {
'topic': self._topic[CONF_RGB_STATE_TOPIC],
'msg_callback': rgb_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._hs = (0, 0)
if self._optimistic_rgb and last_state\
and last_state.attributes.get(ATTR_HS_COLOR):
Expand All @@ -372,7 +361,7 @@ def color_temp_received(topic, payload, qos):
topics[CONF_COLOR_TEMP_STATE_TOPIC] = {
'topic': self._topic[CONF_COLOR_TEMP_STATE_TOPIC],
'msg_callback': color_temp_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._color_temp = 150
if self._optimistic_color_temp and last_state\
and last_state.attributes.get(ATTR_COLOR_TEMP):
Expand All @@ -397,7 +386,7 @@ def effect_received(topic, payload, qos):
topics[CONF_EFFECT_STATE_TOPIC] = {
'topic': self._topic[CONF_EFFECT_STATE_TOPIC],
'msg_callback': effect_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._effect = 'none'
if self._optimistic_effect and last_state\
and last_state.attributes.get(ATTR_EFFECT):
Expand Down Expand Up @@ -427,7 +416,7 @@ def hs_received(topic, payload, qos):
topics[CONF_HS_STATE_TOPIC] = {
'topic': self._topic[CONF_HS_STATE_TOPIC],
'msg_callback': hs_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._hs = (0, 0)
if self._optimistic_hs and last_state\
and last_state.attributes.get(ATTR_HS_COLOR):
Expand All @@ -445,15 +434,16 @@ def white_value_received(topic, payload, qos):
return

device_value = float(payload)
percent_white = device_value / self._white_value_scale
percent_white = \
device_value / self._config.get(CONF_WHITE_VALUE_SCALE)
self._white_value = int(percent_white * 255)
self.async_schedule_update_ha_state()

if self._topic[CONF_WHITE_VALUE_STATE_TOPIC] is not None:
topics[CONF_WHITE_VALUE_STATE_TOPIC] = {
'topic': self._topic[CONF_WHITE_VALUE_STATE_TOPIC],
'msg_callback': white_value_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._white_value = 255
elif self._optimistic_white_value and last_state\
and last_state.attributes.get(ATTR_WHITE_VALUE):
Expand All @@ -480,7 +470,7 @@ def xy_received(topic, payload, qos):
topics[CONF_XY_STATE_TOPIC] = {
'topic': self._topic[CONF_XY_STATE_TOPIC],
'msg_callback': xy_received,
'qos': self._qos}
'qos': self._config.get(CONF_QOS)}
self._hs = (0, 0)
if self._optimistic_xy and last_state\
and last_state.attributes.get(ATTR_HS_COLOR):
Expand Down Expand Up @@ -525,7 +515,7 @@ def should_poll(self):
@property
def name(self):
"""Return the name of the device if any."""
return self._name
return self._config.get(CONF_NAME)

@property
def unique_id(self):
Expand All @@ -545,7 +535,7 @@ def assumed_state(self):
@property
def effect_list(self):
"""Return the list of supported effects."""
return self._effect_list
return self._config.get(CONF_EFFECT_LIST)

@property
def effect(self):
Expand All @@ -563,17 +553,19 @@ async def async_turn_on(self, **kwargs):
This method is a coroutine.
"""
should_update = False
on_command_type = self._config.get(CONF_ON_COMMAND_TYPE)

if self._on_command_type == 'first':
if on_command_type == 'first':
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC],
self._payload['on'], self._qos, self._retain)
self._payload['on'], self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))
should_update = True

# If brightness is being used instead of an on command, make sure
# there is a brightness input. Either set the brightness to our
# saved value or the maximum value if this is the first call
elif self._on_command_type == 'brightness':
elif on_command_type == 'brightness':
if ATTR_BRIGHTNESS not in kwargs:
kwargs[ATTR_BRIGHTNESS] = self._brightness if \
self._brightness else 255
Expand Down Expand Up @@ -605,7 +597,8 @@ async def async_turn_on(self, **kwargs):

mqtt.async_publish(
self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
rgb_color_str, self._qos, self._retain)
rgb_color_str, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_rgb:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -617,8 +610,8 @@ async def async_turn_on(self, **kwargs):
hs_color = kwargs[ATTR_HS_COLOR]
mqtt.async_publish(
self.hass, self._topic[CONF_HS_COMMAND_TOPIC],
'{},{}'.format(*hs_color), self._qos,
self._retain)
'{},{}'.format(*hs_color), self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_hs:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -630,8 +623,8 @@ async def async_turn_on(self, **kwargs):
xy_color = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
mqtt.async_publish(
self.hass, self._topic[CONF_XY_COMMAND_TOPIC],
'{},{}'.format(*xy_color), self._qos,
self._retain)
'{},{}'.format(*xy_color), self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_xy:
self._hs = kwargs[ATTR_HS_COLOR]
Expand All @@ -640,10 +633,12 @@ async def async_turn_on(self, **kwargs):
if ATTR_BRIGHTNESS in kwargs and \
self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC] is not None:
percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
device_brightness = int(percent_bright * self._brightness_scale)
device_brightness = \
int(percent_bright * self._config.get(CONF_BRIGHTNESS_SCALE))
mqtt.async_publish(
self.hass, self._topic[CONF_BRIGHTNESS_COMMAND_TOPIC],
device_brightness, self._qos, self._retain)
device_brightness, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_brightness:
self._brightness = kwargs[ATTR_BRIGHTNESS]
Expand All @@ -664,7 +659,8 @@ async def async_turn_on(self, **kwargs):

mqtt.async_publish(
self.hass, self._topic[CONF_RGB_COMMAND_TOPIC],
rgb_color_str, self._qos, self._retain)
rgb_color_str, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_brightness:
self._brightness = kwargs[ATTR_BRIGHTNESS]
Expand All @@ -675,7 +671,8 @@ async def async_turn_on(self, **kwargs):
color_temp = int(kwargs[ATTR_COLOR_TEMP])
mqtt.async_publish(
self.hass, self._topic[CONF_COLOR_TEMP_COMMAND_TOPIC],
color_temp, self._qos, self._retain)
color_temp, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_color_temp:
self._color_temp = kwargs[ATTR_COLOR_TEMP]
Expand All @@ -684,10 +681,11 @@ async def async_turn_on(self, **kwargs):
if ATTR_EFFECT in kwargs and \
self._topic[CONF_EFFECT_COMMAND_TOPIC] is not None:
effect = kwargs[ATTR_EFFECT]
if effect in self._effect_list:
if effect in self._config.get(CONF_EFFECT_LIST):
mqtt.async_publish(
self.hass, self._topic[CONF_EFFECT_COMMAND_TOPIC],
effect, self._qos, self._retain)
effect, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_effect:
self._effect = kwargs[ATTR_EFFECT]
Expand All @@ -696,18 +694,21 @@ async def async_turn_on(self, **kwargs):
if ATTR_WHITE_VALUE in kwargs and \
self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None:
percent_white = float(kwargs[ATTR_WHITE_VALUE]) / 255
device_white_value = int(percent_white * self._white_value_scale)
device_white_value = \
int(percent_white * self._config.get(CONF_WHITE_VALUE_SCALE))
mqtt.async_publish(
self.hass, self._topic[CONF_WHITE_VALUE_COMMAND_TOPIC],
device_white_value, self._qos, self._retain)
device_white_value, self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))

if self._optimistic_white_value:
self._white_value = kwargs[ATTR_WHITE_VALUE]
should_update = True

if self._on_command_type == 'last':
if on_command_type == 'last':
mqtt.async_publish(self.hass, self._topic[CONF_COMMAND_TOPIC],
self._payload['on'], self._qos, self._retain)
self._payload['on'], self._config.get(CONF_QOS),
self._config.get(CONF_RETAIN))
should_update = True

if self._optimistic:
Expand All @@ -725,7 +726,7 @@ async def async_turn_off(self, **kwargs):
"""
mqtt.async_publish(
self.hass, self._topic[CONF_COMMAND_TOPIC], self._payload['off'],
self._qos, self._retain)
self._config.get(CONF_QOS), self._config.get(CONF_RETAIN))

if self._optimistic:
# Optimistically assume that the light has changed state.
Expand Down
Loading

0 comments on commit 69fd3aa

Please sign in to comment.