diff --git a/homeassistant/components/sensor/version.py b/homeassistant/components/sensor/version.py new file mode 100644 index 00000000000..c19d2743563 --- /dev/null +++ b/homeassistant/components/sensor/version.py @@ -0,0 +1,55 @@ +""" +Support for displaying the current version of Home Assistant. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.version/ +""" +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 __version__, CONF_NAME +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_NAME = "Current Version" + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + 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 the Version sensor platform.""" + name = config.get(CONF_NAME) + + async_add_devices([VersionSensor(name)]) + + +class VersionSensor(Entity): + """Representation of a Home Assistant version sensor.""" + + def __init__(self, name): + """Initialize the Version sensor.""" + self._name = name + self._state = __version__ + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def should_poll(self): + """No polling needed.""" + return False + + @property + def state(self): + """Return the state of the sensor.""" + return self._state diff --git a/tests/components/sensor/test_version.py b/tests/components/sensor/test_version.py new file mode 100644 index 00000000000..270cfd1709d --- /dev/null +++ b/tests/components/sensor/test_version.py @@ -0,0 +1,50 @@ +"""The test for the version sensor platform.""" +import asyncio +import unittest +from unittest.mock import patch + +from homeassistant.setup import setup_component + +from tests.common import get_test_home_assistant + +MOCK_VERSION = '10.0' + + +class TestVersionSensor(unittest.TestCase): + """Test the Version sensor.""" + + def setup_method(self, method): + """Set up things to be run when tests are started.""" + self.hass = get_test_home_assistant() + + def teardown_method(self, method): + """Stop everything that was started.""" + self.hass.stop() + + def test_version_sensor(self): + """Test the Version sensor.""" + config = { + 'sensor': { + 'platform': 'version', + } + } + + assert setup_component(self.hass, 'sensor', config) + + @asyncio.coroutine + def test_version(self): + """Test the Version sensor.""" + config = { + 'sensor': { + 'platform': 'version', + 'name': 'test', + } + } + + with patch('homeassistant.const.__version__', MOCK_VERSION): + assert setup_component(self.hass, 'sensor', config) + self.hass.block_till_done() + + state = self.hass.states.get('sensor.test') + + self.assertEqual(state.state, '10.0')