core/homeassistant/components/mopar/switch.py

53 lines
1.5 KiB
Python
Raw Normal View History

"""Support for the Mopar vehicle switch."""
import logging
from homeassistant.components.mopar import DOMAIN as MOPAR_DOMAIN
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import STATE_ON, STATE_OFF
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Mopar Switch platform."""
data = hass.data[MOPAR_DOMAIN]
2019-07-31 19:25:30 +00:00
add_entities(
[MoparSwitch(data, index) for index, _ in enumerate(data.vehicles)], True
)
class MoparSwitch(SwitchDevice):
"""Representation of a Mopar switch."""
def __init__(self, data, index):
"""Initialize the Switch."""
self._index = index
2019-07-31 19:25:30 +00:00
self._name = "{} Switch".format(data.get_vehicle_name(self._index))
self._actuate = data.actuate
self._state = None
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def is_on(self):
"""Return True if the entity is on."""
return self._state == STATE_ON
@property
def should_poll(self):
"""Return the polling requirement for this switch."""
return False
def turn_on(self, **kwargs):
"""Turn on the Mopar Vehicle."""
2019-07-31 19:25:30 +00:00
if self._actuate("engine_on", self._index):
self._state = STATE_ON
def turn_off(self, **kwargs):
"""Turn off the Mopar Vehicle."""
2019-07-31 19:25:30 +00:00
if self._actuate("engine_off", self._index):
self._state = STATE_OFF