Konnected component follow up (#14491)
* make device_discovered synchronous * small fixes from code review * use dispatcher to update sensor state * update switch state based on response from the device * interpolate entity_id into dispatcher signal * cleanup lint * change coroutine to callback
This commit is contained in:
1 parent
144524fbbb
commit
ed3efc8712
3 files changed
+57
-38
No files matched your search
@@ -5,11 +5,11 @@ For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.konnected/
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from homeassistant.components.konnected import (
|
||||
DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION, STATE_LOW, STATE_HIGH)
|
||||
DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION,
|
||||
STATE_LOW, STATE_HIGH)
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.const import (CONF_DEVICES, CONF_SWITCHES, ATTR_STATE)
|
||||
|
||||
@@ -24,7 +24,7 @@ async def async_setup_platform(hass, config, async_add_devices,
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
data = hass.data[DOMAIN]
|
||||
data = hass.data[KONNECTED_DOMAIN]
|
||||
device_id = discovery_info['device_id']
|
||||
client = data[CONF_DEVICES][device_id]['client']
|
||||
switches = [KonnectedSwitch(device_id, pin_num, pin_data, client)
|
||||
@@ -41,12 +41,11 @@ class KonnectedSwitch(ToggleEntity):
|
||||
self._data = data
|
||||
self._device_id = device_id
|
||||
self._pin_num = pin_num
|
||||
self._state = self._data.get(ATTR_STATE)
|
||||
self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH)
|
||||
self._state = self._boolean_state(self._data.get(ATTR_STATE))
|
||||
self._name = self._data.get(
|
||||
'name', 'Konnected {} Actuator {}'.format(
|
||||
device_id, PIN_TO_ZONE[pin_num]))
|
||||
self._data['entity'] = self
|
||||
self._client = client
|
||||
_LOGGER.debug('Created new switch: %s', self._name)
|
||||
|
||||
@@ -62,26 +61,34 @@ class KonnectedSwitch(ToggleEntity):
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Send a command to turn on the switch."""
|
||||
self._client.put_device(self._pin_num,
|
||||
int(self._activation == STATE_HIGH))
|
||||
self._set_state(True)
|
||||
resp = self._client.put_device(
|
||||
self._pin_num, int(self._activation == STATE_HIGH))
|
||||
|
||||
if resp.get(ATTR_STATE) is not None:
|
||||
self._set_state(self._boolean_state(resp.get(ATTR_STATE)))
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Send a command to turn off the switch."""
|
||||
self._client.put_device(self._pin_num,
|
||||
int(self._activation == STATE_LOW))
|
||||
self._set_state(False)
|
||||
resp = self._client.put_device(
|
||||
self._pin_num, int(self._activation == STATE_LOW))
|
||||
|
||||
if resp.get(ATTR_STATE) is not None:
|
||||
self._set_state(self._boolean_state(resp.get(ATTR_STATE)))
|
||||
|
||||
def _boolean_state(self, int_state):
|
||||
if int_state is None:
|
||||
return False
|
||||
if int_state == 0:
|
||||
return self._activation == STATE_LOW
|
||||
if int_state == 1:
|
||||
return self._activation == STATE_HIGH
|
||||
|
||||
def _set_state(self, state):
|
||||
self._state = state
|
||||
self._data[ATTR_STATE] = state
|
||||
self.schedule_update_ha_state()
|
||||
_LOGGER.debug('Setting status of %s actuator pin %s to %s',
|
||||
self._device_id, self.name, state)
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_set_state(self, state):
|
||||
"""Update the switch's state."""
|
||||
self._state = state
|
||||
self._data[ATTR_STATE] = state
|
||||
self.async_schedule_update_ha_state()
|
||||
async def async_added_to_hass(self):
|
||||
"""Store entity_id."""
|
||||
self._data['entity_id'] = self.entity_id
|
||||
Reference in new issue
Block a user