Use voluptuous for file (#3049)

This commit is contained in:
Pascal Vizeli 2016-08-31 18:12:34 +02:00 committed by GitHub
parent dfee443312
commit 705b3571f4
2 changed files with 19 additions and 14 deletions

View file

@ -7,24 +7,28 @@ https://home-assistant.io/components/notify.file/
import logging
import os
import voluptuous as vol
import homeassistant.util.dt as dt_util
from homeassistant.components.notify import (
ATTR_TITLE, DOMAIN, BaseNotificationService)
from homeassistant.helpers import validate_config
ATTR_TITLE, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_FILENAME
import homeassistant.helpers.config_validation as cv
CONF_TIMESTAMP = 'timestamp'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_FILENAME): cv.string,
vol.Optional(CONF_TIMESTAMP, default=False): cv.boolean,
})
_LOGGER = logging.getLogger(__name__)
def get_service(hass, config):
"""Get the file notification service."""
if not validate_config({DOMAIN: config},
{DOMAIN: ['filename',
'timestamp']},
_LOGGER):
return None
filename = config['filename']
timestamp = config['timestamp']
filename = config[CONF_FILENAME]
timestamp = config[CONF_TIMESTAMP]
return FileNotificationService(hass, filename, timestamp)
@ -48,7 +52,7 @@ class FileNotificationService(BaseNotificationService):
'-' * 80)
file.write(title)
if self.add_timestamp == 1:
if self.add_timestamp:
text = '{} {}\n'.format(dt_util.utcnow().isoformat(), message)
file.write(text)
else:

View file

@ -8,6 +8,7 @@ import homeassistant.components.notify as notify
from homeassistant.components.notify import (
ATTR_TITLE_DEFAULT)
import homeassistant.util.dt as dt_util
from homeassistant.bootstrap import _setup_component
from tests.common import get_test_home_assistant
@ -25,11 +26,11 @@ class TestNotifyFile(unittest.TestCase):
def test_bad_config(self):
"""Test set up the platform with bad/missing config."""
self.assertFalse(notify.setup(self.hass, {
self.assertFalse(_setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
}
},
}))
@patch('homeassistant.util.dt.utcnow')
@ -45,7 +46,7 @@ class TestNotifyFile(unittest.TestCase):
'name': 'test',
'platform': 'file',
'filename': filename,
'timestamp': 0
'timestamp': False,
}
}))
title = '{} notifications (Log started: {})\n{}\n'.format(