Created
January 11, 2019 07:50
-
-
Save kennedyshead/d1ea86a13910ad896cfe39fb374866f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import asyncio | |
import json | |
import logging | |
import voluptuous as vol | |
from datetime import datetime | |
from homeassistant.components.sensor import PLATFORM_SCHEMA | |
from homeassistant.const import CONF_USERNAME | |
from homeassistant.helpers.entity import Entity | |
import homeassistant.helpers.config_validation as cv | |
CONF_PATH = 'path' | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Required(CONF_PATH): cv.string, | |
vol.Required(CONF_USERNAME): cv.string | |
}) | |
_LOGGER = logging.getLogger(__name__) | |
DOMAIN = 'signalmessenger' | |
class SignalMessenger: | |
def __init__(self, cmd, username): | |
self.data = [] | |
self._cmd = cmd + ' -u %s receive --json' % username | |
self.is_polling = False | |
async def async_update(self): | |
if not self.is_polling: | |
self.is_polling = True | |
self.data = await run(self._cmd) | |
self.is_polling = False | |
async def run(cmd): | |
proc = await asyncio.create_subprocess_shell( | |
cmd, | |
stdout=asyncio.subprocess.PIPE, | |
stderr=asyncio.subprocess.PIPE) | |
stdout, stderr = await proc.communicate() | |
if stdout: | |
return stdout.decode() | |
else: | |
_LOGGER.debug("{} {} ()".format( | |
proc.returncode, stderr.decode(), stdout.decode())) | |
async def async_setup_platform( | |
hass, config, async_add_entities, discovery_info=None): | |
"""Set up the platform.""" | |
data = SignalMessenger(config[CONF_PATH], config[CONF_USERNAME]) | |
await data.async_update() | |
entities = [ | |
Sensor(data) | |
] | |
async_add_entities(entities) | |
class Sensor(Entity): | |
"""Representation of a sensor.""" | |
def __init__(self, data=None): | |
"""Init the sensor.""" | |
self._type = 'message' | |
self.data = data | |
self._state = None | |
self._name = 'Signalmessage' | |
self._attributes = None | |
@property | |
def unit_of_measurement(self): | |
"""Return the unit of measurement.""" | |
return "message" | |
@property | |
def state(self): | |
"""Property for the state attributes.""" | |
return self._state | |
@property | |
def name(self): | |
"""Name property for sensor.""" | |
return self._name | |
@property | |
def state_attributes(self): | |
return self._attributes | |
def update_attributes(self, data): | |
self._attributes = { | |
"from": data['envelope']['source'], | |
"time": datetime.utcfromtimestamp( | |
int(str(data['envelope']['timestamp'])[:-3]) | |
).strftime("%Y-%m-%d %T") | |
} | |
async def async_update(self): | |
"""Fetch new state data for the sensor.""" | |
await self.data.async_update() | |
if self.data.data: | |
for line in self.data.data.split('\n'): | |
data = json.loads(line) | |
if data['envelope']['dataMessage'] is not None: | |
self._state = data['envelope']['dataMessage']['message'] | |
self.update_attributes(data) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment