2019-02-13 20:21:14 +00:00
|
|
|
"""Support for EnOcean devices."""
|
2016-08-25 04:35:09 +00:00
|
|
|
import logging
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import CONF_DEVICE
|
2019-04-24 22:30:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-08-25 04:35:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'enocean'
|
2019-04-24 22:30:46 +00:00
|
|
|
DATA_ENOCEAN = 'enocean'
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_DEVICE): cv.string,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
SIGNAL_RECEIVE_MESSAGE = 'enocean.receive_message'
|
|
|
|
SIGNAL_SEND_MESSAGE = 'enocean.send_message'
|
|
|
|
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the EnOcean component."""
|
2016-08-25 04:35:09 +00:00
|
|
|
serial_dev = config[DOMAIN].get(CONF_DEVICE)
|
2019-04-24 22:30:46 +00:00
|
|
|
dongle = EnOceanDongle(hass, serial_dev)
|
|
|
|
hass.data[DATA_ENOCEAN] = dongle
|
2016-08-25 04:35:09 +00:00
|
|
|
|
2016-05-29 21:28:03 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class EnOceanDongle:
|
|
|
|
"""Representation of an EnOcean dongle."""
|
|
|
|
|
|
|
|
def __init__(self, hass, ser):
|
|
|
|
"""Initialize the EnOcean dongle."""
|
|
|
|
from enocean.communicators.serialcommunicator import SerialCommunicator
|
2017-04-30 05:04:49 +00:00
|
|
|
self.__communicator = SerialCommunicator(
|
|
|
|
port=ser, callback=self.callback)
|
2016-05-29 21:28:03 +00:00
|
|
|
self.__communicator.start()
|
2019-04-24 22:30:46 +00:00
|
|
|
self.hass = hass
|
|
|
|
self.hass.helpers.dispatcher.dispatcher_connect(
|
|
|
|
SIGNAL_SEND_MESSAGE, self._send_message_callback)
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def _send_message_callback(self, command):
|
|
|
|
"""Send a command through the EnOcean dongle."""
|
2016-05-29 21:28:03 +00:00
|
|
|
self.__communicator.send(command)
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def callback(self, packet):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle EnOcean device's callback.
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
This is the callback function called by python-enocan whenever there
|
|
|
|
is an incoming packet.
|
2016-05-29 21:28:03 +00:00
|
|
|
"""
|
|
|
|
from enocean.protocol.packet import RadioPacket
|
2019-04-24 22:30:46 +00:00
|
|
|
if isinstance(packet, RadioPacket):
|
|
|
|
_LOGGER.debug("Received radio packet: %s", packet)
|
|
|
|
self.hass.helpers.dispatcher.dispatcher_send(
|
|
|
|
SIGNAL_RECEIVE_MESSAGE, packet)
|
|
|
|
|
|
|
|
|
|
|
|
class EnOceanDevice(Entity):
|
2016-05-29 21:28:03 +00:00
|
|
|
"""Parent class for all devices associated with the EnOcean component."""
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def __init__(self, dev_id, dev_name="EnOcean device"):
|
2016-05-29 21:28:03 +00:00
|
|
|
"""Initialize the device."""
|
2019-04-24 22:30:46 +00:00
|
|
|
self.dev_id = dev_id
|
|
|
|
self.dev_name = dev_name
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
|
|
|
SIGNAL_RECEIVE_MESSAGE, self._message_received_callback)
|
|
|
|
|
|
|
|
def _message_received_callback(self, packet):
|
|
|
|
"""Handle incoming packets."""
|
|
|
|
from enocean.utils import combine_hex
|
|
|
|
if packet.sender_int == combine_hex(self.dev_id):
|
|
|
|
self.value_changed(packet)
|
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the device when a packet arrives."""
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
def send_command(self, data, optional, packet_type):
|
|
|
|
"""Send a command via the EnOcean dongle."""
|
|
|
|
from enocean.protocol.packet import Packet
|
|
|
|
packet = Packet(packet_type, data=data, optional=optional)
|
2019-04-24 22:30:46 +00:00
|
|
|
self.hass.helpers.dispatcher.dispatcher_send(
|
|
|
|
SIGNAL_SEND_MESSAGE, packet)
|