Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Nov 4, 2018
1 parent ac19a0b commit b357dce
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions homeassistant/components/binary_sensor/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
https://home-assistant.io/components/binary_sensor.mqtt/
"""
import logging
from typing import Optional

import voluptuous as vol

Expand Down Expand Up @@ -79,11 +78,6 @@ async def _async_setup_entity(hass, config, async_add_entities,
value_template.hass = hass

async_add_entities([MqttBinarySensor(
config.get(CONF_AVAILABILITY_TOPIC),
config.get(CONF_QOS),
config.get(CONF_PAYLOAD_AVAILABLE),
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
config.get(CONF_DEVICE),
config,
discovery_payload
)])
Expand All @@ -93,22 +87,14 @@ class MqttBinarySensor(MqttAvailability, MqttDiscoveryUpdate,
MqttEntityDeviceInfo, BinarySensorDevice):
"""Representation a binary sensor that is updated by MQTT."""

def __init__(self, availability_topic, qos, payload_available,
payload_not_available, device_config: Optional[ConfigType],
config, discovery_payload):
def __init__(self, config, discovery_payload):
"""Initialize the MQTT binary sensor."""
MqttAvailability.__init__(self, availability_topic, qos,
payload_available, payload_not_available)
MqttDiscoveryUpdate.__init__(self, None, discovery_payload,
self.discovery_callback)
MqttEntityDeviceInfo.__init__(self, device_config)
self._config = config
self._state = None
self._state_unsub = None
self._delay_listener = None

# Config
self._name = config.get(CONF_NAME)
self._name = None
self._state_topic = None
self._device_class = None
self._payload_on = None
Expand All @@ -117,24 +103,40 @@ def __init__(self, availability_topic, qos, payload_available,
self._force_update = None
self._off_delay = None
self._template = None
self._unique_id = config.get(CONF_UNIQUE_ID)
self._unique_id = None

# Load config
self._setup_from_config(config)

availability_topic = config.get(CONF_AVAILABILITY_TOPIC)
payload_available = config.get(CONF_PAYLOAD_AVAILABLE)
payload_not_available = config.get(CONF_PAYLOAD_NOT_AVAILABLE)
device_config = config.get(CONF_DEVICE)

MqttAvailability.__init__(self, availability_topic, self._qos,
payload_available, payload_not_available)
MqttDiscoveryUpdate.__init__(self, None, discovery_payload,
self.discovery_callback)
MqttEntityDeviceInfo.__init__(self, device_config)

async def async_added_to_hass(self):
"""Subscribe mqtt events."""
await MqttAvailability.async_added_to_hass(self)
await MqttDiscoveryUpdate.async_added_to_hass(self)
await self._setup_from_config(self._config)
await self._subscribe_topics(None)

async def discovery_callback(self, discovery_payload):
"""Handle updated discovery message."""
config = PLATFORM_SCHEMA(discovery_payload)
await self._setup_from_config(config)
self._setup_from_config(config)
old_state_topic = self._state_topic
await self._subscribe_topics(config.get(CONF_STATE_TOPIC))
self.async_schedule_update_ha_state()

async def _setup_from_config(self, config):
def _setup_from_config(self, config):
"""(Re)Setup the entity."""
self._name = config.get(CONF_NAME)
state_topic = config.get(CONF_STATE_TOPIC)
self._state_topic = config.get(CONF_STATE_TOPIC)
self._device_class = config.get(CONF_DEVICE_CLASS)
self._qos = config.get(CONF_QOS)
self._force_update = config.get(CONF_FORCE_UPDATE)
Expand All @@ -157,6 +159,8 @@ async def _setup_from_config(self, config):
# TODO: Handle changed device?
# config.get(CONF_DEVICE),

async def _subscribe_topics(self, old_state_topic):
"""(Re)Subscribe to topics."""
@callback
def off_delay_listener(now):
"""Switch device off after a delay."""
Expand Down Expand Up @@ -189,13 +193,16 @@ def state_message_received(_topic, payload, _qos):

self.async_schedule_update_ha_state()

if self._state_topic != state_topic:
self._state_topic = state_topic
_LOGGER.warning("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
if self._state_topic != old_state_topic:
_LOGGER.warning("------------------------------------")
if self._state_unsub is not None:
self._state_unsub()
_LOGGER.warning("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
self._state_unsub = await mqtt.async_subscribe(
self.hass, self._state_topic, state_message_received,
self._qos)
_LOGGER.warning("!!!!!!----------!!!!!!!!!!")

async def async_will_remove_from_hass(self):
"""Unsubscribe when removed."""
Expand Down

0 comments on commit b357dce

Please sign in to comment.