Created
October 15, 2017 09:33
-
-
Save nlamirault/5ed4859572373d043e67b9950ddb733a to your computer and use it in GitHub Desktop.
Home Assistant Teleinfo platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Support for Teleinfo sensors. | |
Teleinfo is a French specific protocol used in electricity smart meters. | |
It provides real time information on power consumption, rates and current on | |
a user accessible serial port. | |
For more details about this platform, please refer to the documentation at | |
https://home-assistant.io/components/sensor.teleinfo/ | |
""" | |
import asyncio | |
import logging | |
import voluptuous as vol | |
import homeassistant.helpers.config_validation as cv | |
from homeassistant.components.sensor import PLATFORM_SCHEMA | |
from homeassistant.const import CONF_NAME | |
from homeassistant.helpers.discovery import async_load_platform | |
from homeassistant.const import EVENT_HOMEASSISTANT_STOP | |
REQUIREMENTS = ["kylin==1.0.0"] | |
_LOGGER = logging.getLogger(__name__) | |
DOMAIN = 'teleinfo' | |
DEFAULT_NAME = 'Teleinfo sensor' | |
CONF_DEVICE = 'device' | |
DEFAULT_DEVICE = '/dev/ttyUSB0' | |
DEFAULT_NAME = 'teleinfo' | |
DATA_VELUX = "data_teleinfo" | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
DOMAIN: vol.Schema({ | |
vol.Required(CONF_DEVICE, default=DEFAULT_DEVICE): cv.string, | |
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | |
}), | |
}) | |
@asyncio.coroutine | |
def async_setup_platform(hass, config, async_add_devices, | |
discovery_info=None): | |
"""Set up for the Teleinfo device.""" | |
from kylin import exceptions | |
conf = config.get(DOMAIN) | |
try: | |
hass.data[DATA_TELEINFO] = TeleinfoModule(hass, conf) | |
yield from hass.data[DATA_TELEINFO].start() | |
hass.loop.create_task( | |
hass.data[DATA_TELEINFO].read_data()) | |
except exceptions.KylinSerialError as err: | |
_LOGGER.error("Can't connect to teleinfo device: %s", str(err)) | |
return False | |
except Exception as err: | |
_LOGGER.error("An error occured: %s", str(err)) | |
return False | |
#sensors = [] | |
#hass.async_add_job( | |
# discovery.async_load_platform( | |
# hass, 'sensor', DOMAIN, conf, config)) | |
yield from async_add_devices([hass.data[DATA_TELEINFO]) | |
return True | |
class TeleinfoModule(object): | |
"""Representation of the Teleinfo object.""" | |
def __init__(self, hass, config): | |
self.hass = hass | |
self.config = config | |
self._name = config.get(CONF_NAME) | |
self._state = None | |
self.initialized = False | |
self.init_teleinfo() | |
@property | |
def name(self): | |
return self._name | |
@property | |
def state(self): | |
return self._state | |
def init_teleinfo(self): | |
import kylin | |
device = self.config.get(CONF_DEVICE) | |
_LOGGER.info("Initialize Teleinfo device using: %s", device) | |
self.teleinfo = kylin.Kylin(port=device, timeout=2) | |
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) | |
self.initialized = True | |
@asyncio.coroutine | |
def async_stop(self, event): | |
"""Stop Teleinfo object. Disconnect from device.""" | |
yield from self.teleinfo.stop() | |
@asyncio.coroutine | |
def async_update(self): | |
"""Read frame from the device.""" | |
try: | |
_LOGGER.info("Read Teleinfo frames") | |
yield from self.teleinfo.open() | |
self._state = yield from self.teleinfo.readframe() | |
except exceptions.KylinSerialError as err: | |
_LOGGER.error("Teleinfo device error: %s", str(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment