From daa11ee5504e742800e5539d02785bd929ffac0e Mon Sep 17 00:00:00 2001 From: Adam Mills Date: Wed, 8 Feb 2017 12:30:58 -0500 Subject: [PATCH] Update example platforms for async conventions (#1998) --- source/developers/platform_example_light.markdown | 11 ++++++++--- source/developers/platform_example_sensor.markdown | 13 ++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/source/developers/platform_example_light.markdown b/source/developers/platform_example_light.markdown index 9e530f65221..a5092db30ca 100644 --- a/source/developers/platform_example_light.markdown +++ b/source/developers/platform_example_light.markdown @@ -76,11 +76,14 @@ class AwesomeLight(Light): def __init__(self, light): """Initialize an AwesomeLight.""" self._light = light + self._name = light.name + self._state = None + self._brightness = None @property def name(self): """Return the display name of this light.""" - return self._light.name + return self._name @property def brightness(self): @@ -89,12 +92,12 @@ class AwesomeLight(Light): This method is optional. Removing it indicates to Home Assistant that brightness is not supported for this light. """ - return self._light.brightness + return self._brightness @property def is_on(self): """Return true if light is on.""" - return self._light.is_on() + return self._state def turn_on(self, **kwargs): """Instruct the light to turn on. @@ -115,4 +118,6 @@ class AwesomeLight(Light): This is the only method that should fetch new data for Home Assistant. """ self._light.update() + self._state = self._light.is_on() + self._brightness = self._light.brightness ``` diff --git a/source/developers/platform_example_sensor.markdown b/source/developers/platform_example_sensor.markdown index 2fd29db8faa..94b84f0d374 100644 --- a/source/developers/platform_example_sensor.markdown +++ b/source/developers/platform_example_sensor.markdown @@ -38,6 +38,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class ExampleSensor(Entity): """Representation of a Sensor.""" + def __init__(self): + """Initialize the sensor.""" + self._state = None + @property def name(self): """Return the name of the sensor.""" @@ -46,10 +50,17 @@ class ExampleSensor(Entity): @property def state(self): """Return the state of the sensor.""" - return 23 + return self._state @property def unit_of_measurement(self): """Return the unit of measurement.""" return TEMP_CELSIUS + + def update(self): + """Fetch new state data for the sensor. + + This is the only method that should fetch new data for Home Assistant. + """ + self._state = 23 ```