2019-04-03 15:40:03 +00:00
|
|
|
"""Demo platform that has two fake switches."""
|
2015-06-13 21:56:20 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
from homeassistant.const import DEVICE_DEFAULT_NAME
|
2019-11-13 15:37:31 +00:00
|
|
|
from . import DOMAIN
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the demo switches."""
|
2019-11-13 15:37:31 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2019-11-13 15:37:31 +00:00
|
|
|
DemoSwitch("swith1", "Decorative Lights", True, None, True),
|
|
|
|
DemoSwitch("swith2", "AC", False, "mdi:air-conditioner", False),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
class DemoSwitch(SwitchDevice):
|
2016-07-12 14:46:29 +00:00
|
|
|
"""Representation of a demo switch."""
|
2016-03-08 12:35:39 +00:00
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
def __init__(self, unique_id, name, state, icon, assumed, device_class=None):
|
2016-07-09 07:35:55 +00:00
|
|
|
"""Initialize the Demo switch."""
|
2019-11-13 15:37:31 +00:00
|
|
|
self._unique_id = unique_id
|
2015-02-28 15:31:39 +00:00
|
|
|
self._name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._state = state
|
2015-11-03 08:20:48 +00:00
|
|
|
self._icon = icon
|
2016-02-14 07:42:11 +00:00
|
|
|
self._assumed = assumed
|
2019-04-17 00:07:14 +00:00
|
|
|
self._device_class = device_class
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {
|
|
|
|
# Serial numbers are unique identifiers within a specific domain
|
|
|
|
(DOMAIN, self.unique_id)
|
|
|
|
},
|
|
|
|
"name": self.name,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2015-03-17 05:20:31 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""No polling needed for a demo switch."""
|
2015-03-17 05:20:31 +00:00
|
|
|
return False
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the name of the device if any."""
|
2015-02-28 15:31:39 +00:00
|
|
|
return self._name
|
|
|
|
|
2015-11-03 08:20:48 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the icon to use for device if any."""
|
2015-11-03 08:20:48 +00:00
|
|
|
return self._icon
|
|
|
|
|
2016-02-14 07:42:11 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return if the state is based on assumptions."""
|
|
|
|
return self._assumed
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power usage in W."""
|
2015-06-13 21:56:20 +00:00
|
|
|
if self._state:
|
|
|
|
return 100
|
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def today_energy_kwh(self):
|
|
|
|
"""Return the today total energy usage in kWh."""
|
|
|
|
return 15
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return true if switch is on."""
|
2015-06-13 21:56:20 +00:00
|
|
|
return self._state
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2019-04-17 00:07:14 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return device of entity."""
|
|
|
|
return self._device_class
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Turn the switch on."""
|
2015-06-13 21:56:20 +00:00
|
|
|
self._state = True
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Turn the device off."""
|
2015-06-13 21:56:20 +00:00
|
|
|
self._state = False
|
2016-11-16 16:26:29 +00:00
|
|
|
self.schedule_update_ha_state()
|