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 logging
import os import os
import voluptuous as vol
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.components.notify import ( from homeassistant.components.notify import (
ATTR_TITLE, DOMAIN, BaseNotificationService) ATTR_TITLE, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.helpers import validate_config 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__) _LOGGER = logging.getLogger(__name__)
def get_service(hass, config): def get_service(hass, config):
"""Get the file notification service.""" """Get the file notification service."""
if not validate_config({DOMAIN: config}, filename = config[CONF_FILENAME]
{DOMAIN: ['filename', timestamp = config[CONF_TIMESTAMP]
'timestamp']},
_LOGGER):
return None
filename = config['filename']
timestamp = config['timestamp']
return FileNotificationService(hass, filename, timestamp) return FileNotificationService(hass, filename, timestamp)
@ -48,7 +52,7 @@ class FileNotificationService(BaseNotificationService):
'-' * 80) '-' * 80)
file.write(title) file.write(title)
if self.add_timestamp == 1: if self.add_timestamp:
text = '{} {}\n'.format(dt_util.utcnow().isoformat(), message) text = '{} {}\n'.format(dt_util.utcnow().isoformat(), message)
file.write(text) file.write(text)
else: else:

View file

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