2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Tesla charger switches."""
|
2017-10-09 11:38:00 +00:00
|
|
|
import logging
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2017-10-09 11:38:00 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
|
|
|
|
|
2017-10-09 11:38:00 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Tesla binary_sensors by config_entry."""
|
|
|
|
controller = hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"]
|
|
|
|
entities = []
|
|
|
|
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["switch"]:
|
2020-01-01 11:23:29 +00:00
|
|
|
if device.type == "charger switch":
|
2019-12-23 20:54:25 +00:00
|
|
|
entities.append(ChargerSwitch(device, controller, config_entry))
|
|
|
|
entities.append(UpdateSwitch(device, controller, config_entry))
|
2020-01-01 11:23:29 +00:00
|
|
|
elif device.type == "maxrange switch":
|
2019-12-23 20:54:25 +00:00
|
|
|
entities.append(RangeSwitch(device, controller, config_entry))
|
2020-03-23 09:52:59 +00:00
|
|
|
elif device.type == "sentry mode switch":
|
|
|
|
entities.append(SentryModeSwitch(device, controller, config_entry))
|
2019-12-23 20:54:25 +00:00
|
|
|
async_add_entities(entities, True)
|
2017-10-09 11:38:00 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class ChargerSwitch(TeslaDevice, SwitchEntity):
|
2017-10-09 11:38:00 +00:00
|
|
|
"""Representation of a Tesla charger switch."""
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
def __init__(self, tesla_device, controller, config_entry):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialise of the switch."""
|
2017-10-09 11:38:00 +00:00
|
|
|
self._state = None
|
2019-12-23 20:54:25 +00:00
|
|
|
super().__init__(tesla_device, controller, config_entry)
|
2017-10-09 11:38:00 +00:00
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-10-09 11:38:00 +00:00
|
|
|
"""Send the on command."""
|
|
|
|
_LOGGER.debug("Enable charging: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await self.tesla_device.start_charge()
|
2017-10-09 11:38:00 +00:00
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-10-09 11:38:00 +00:00
|
|
|
"""Send the off command."""
|
2018-03-04 10:35:38 +00:00
|
|
|
_LOGGER.debug("Disable charging for: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await self.tesla_device.stop_charge()
|
2017-10-09 11:38:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get whether the switch is in on state."""
|
|
|
|
return self._state == STATE_ON
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_update(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Update the state of the switch."""
|
2017-10-09 11:38:00 +00:00
|
|
|
_LOGGER.debug("Updating state for: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await super().async_update()
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = STATE_ON if self.tesla_device.is_charging() else STATE_OFF
|
2018-03-04 10:35:38 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class RangeSwitch(TeslaDevice, SwitchEntity):
|
2018-03-04 10:35:38 +00:00
|
|
|
"""Representation of a Tesla max range charging switch."""
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
def __init__(self, tesla_device, controller, config_entry):
|
2019-11-15 04:15:58 +00:00
|
|
|
"""Initialise the switch."""
|
2018-03-04 10:35:38 +00:00
|
|
|
self._state = None
|
2019-12-23 20:54:25 +00:00
|
|
|
super().__init__(tesla_device, controller, config_entry)
|
2018-03-04 10:35:38 +00:00
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-03-04 10:35:38 +00:00
|
|
|
"""Send the on command."""
|
|
|
|
_LOGGER.debug("Enable max range charging: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await self.tesla_device.set_max()
|
2018-03-04 10:35:38 +00:00
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-03-04 10:35:38 +00:00
|
|
|
"""Send the off command."""
|
|
|
|
_LOGGER.debug("Disable max range charging: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await self.tesla_device.set_standard()
|
2018-03-04 10:35:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get whether the switch is in on state."""
|
2019-10-21 08:59:58 +00:00
|
|
|
return self._state
|
2018-03-04 10:35:38 +00:00
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_update(self):
|
2018-03-04 10:35:38 +00:00
|
|
|
"""Update the state of the switch."""
|
|
|
|
_LOGGER.debug("Updating state for: %s", self._name)
|
2019-11-15 04:15:58 +00:00
|
|
|
await super().async_update()
|
2019-10-21 08:59:58 +00:00
|
|
|
self._state = bool(self.tesla_device.is_maxrange())
|
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class UpdateSwitch(TeslaDevice, SwitchEntity):
|
2019-10-21 08:59:58 +00:00
|
|
|
"""Representation of a Tesla update switch."""
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
def __init__(self, tesla_device, controller, config_entry):
|
2019-11-15 04:15:58 +00:00
|
|
|
"""Initialise the switch."""
|
2019-10-21 08:59:58 +00:00
|
|
|
self._state = None
|
2019-11-15 04:15:58 +00:00
|
|
|
tesla_device.type = "update switch"
|
2019-12-23 20:54:25 +00:00
|
|
|
super().__init__(tesla_device, controller, config_entry)
|
2019-10-21 08:59:58 +00:00
|
|
|
self._name = self._name.replace("charger", "update")
|
|
|
|
self.tesla_id = self.tesla_id.replace("charger", "update")
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2019-10-21 08:59:58 +00:00
|
|
|
"""Send the on command."""
|
|
|
|
_LOGGER.debug("Enable updates: %s %s", self._name, self.tesla_device.id())
|
|
|
|
self.controller.set_updates(self.tesla_device.id(), True)
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2019-10-21 08:59:58 +00:00
|
|
|
"""Send the off command."""
|
|
|
|
_LOGGER.debug("Disable updates: %s %s", self._name, self.tesla_device.id())
|
|
|
|
self.controller.set_updates(self.tesla_device.id(), False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get whether the switch is in on state."""
|
|
|
|
return self._state
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_update(self):
|
2019-10-21 08:59:58 +00:00
|
|
|
"""Update the state of the switch."""
|
|
|
|
car_id = self.tesla_device.id()
|
|
|
|
_LOGGER.debug("Updating state for: %s %s", self._name, car_id)
|
2019-11-15 04:15:58 +00:00
|
|
|
await super().async_update()
|
2019-10-21 08:59:58 +00:00
|
|
|
self._state = bool(self.controller.get_updates(car_id))
|
2020-03-23 09:52:59 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class SentryModeSwitch(TeslaDevice, SwitchEntity):
|
2020-03-23 09:52:59 +00:00
|
|
|
"""Representation of a Tesla sentry mode switch."""
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Send the on command."""
|
|
|
|
_LOGGER.debug("Enable sentry mode: %s", self._name)
|
|
|
|
await self.tesla_device.enable_sentry_mode()
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Send the off command."""
|
|
|
|
_LOGGER.debug("Disable sentry mode: %s", self._name)
|
|
|
|
await self.tesla_device.disable_sentry_mode()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Get whether the switch is in on state."""
|
|
|
|
return self.tesla_device.is_on()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Indicate if Home Assistant is able to read the state and control the underlying device."""
|
|
|
|
return self.tesla_device.available()
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update the state of the switch."""
|
|
|
|
_LOGGER.debug("Updating state for: %s", self._name)
|
|
|
|
await super().async_update()
|