diff --git a/CODEOWNERS b/CODEOWNERS index 78867fec0ee..51791886e6d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -57,5 +57,7 @@ homeassistant/components/switch/tplink.py @rytilahti homeassistant/components/*/broadlink.py @danielhiversen homeassistant/components/*/rfxtrx.py @danielhiversen +homeassistant/components/tesla.py @zabuldon +homeassistant/components/*/tesla.py @zabuldon homeassistant/components/*/xiaomi_aqara.py @danielhiversen homeassistant/components/*/xiaomi_miio.py @rytilahti diff --git a/homeassistant/components/binary_sensor/tesla.py b/homeassistant/components/binary_sensor/tesla.py index af7e394b50e..a7cda90b3f6 100644 --- a/homeassistant/components/binary_sensor/tesla.py +++ b/homeassistant/components/binary_sensor/tesla.py @@ -30,7 +30,6 @@ class TeslaBinarySensor(TeslaDevice, BinarySensorDevice): def __init__(self, tesla_device, controller, sensor_type): """Initialisation of binary sensor.""" super().__init__(tesla_device, controller) - self._name = self.tesla_device.name self._state = False self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) self._sensor_type = sensor_type diff --git a/homeassistant/components/climate/tesla.py b/homeassistant/components/climate/tesla.py index 39d002e72d9..684d131d960 100644 --- a/homeassistant/components/climate/tesla.py +++ b/homeassistant/components/climate/tesla.py @@ -35,7 +35,6 @@ class TeslaThermostat(TeslaDevice, ClimateDevice): self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) self._target_temperature = None self._temperature = None - self._name = self.tesla_device.name @property def current_operation(self): diff --git a/homeassistant/components/lock/tesla.py b/homeassistant/components/lock/tesla.py index 3e93e4787a0..80a35adb5fb 100644 --- a/homeassistant/components/lock/tesla.py +++ b/homeassistant/components/lock/tesla.py @@ -29,20 +29,17 @@ class TeslaLock(TeslaDevice, LockDevice): """Initialisation of the lock.""" self._state = None super().__init__(tesla_device, controller) - self._name = self.tesla_device.name self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) def lock(self, **kwargs): """Send the lock command.""" _LOGGER.debug("Locking doors for: %s", self._name) self.tesla_device.lock() - self._state = STATE_LOCKED def unlock(self, **kwargs): """Send the unlock command.""" _LOGGER.debug("Unlocking doors for: %s", self._name) self.tesla_device.unlock() - self._state = STATE_UNLOCKED @property def is_locked(self): diff --git a/homeassistant/components/sensor/tesla.py b/homeassistant/components/sensor/tesla.py index fc31a5543e2..824fec41580 100644 --- a/homeassistant/components/sensor/tesla.py +++ b/homeassistant/components/sensor/tesla.py @@ -49,7 +49,6 @@ class TeslaSensor(TeslaDevice, Entity): self.entity_id = ENTITY_ID_FORMAT.format( '{}_{}'.format(self.tesla_id, self.type)) else: - self._name = self.tesla_device.name self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) @property diff --git a/homeassistant/components/switch/tesla.py b/homeassistant/components/switch/tesla.py new file mode 100644 index 00000000000..7de0c417d56 --- /dev/null +++ b/homeassistant/components/switch/tesla.py @@ -0,0 +1,53 @@ +""" +Support for Tesla charger switch. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/switch.tesla/ +""" +import logging + +from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice +from homeassistant.components.tesla import DOMAIN as TESLA_DOMAIN, TeslaDevice +from homeassistant.const import STATE_ON, STATE_OFF + +_LOGGER = logging.getLogger(__name__) +DEPENDENCIES = ['tesla'] + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the Tesla switch platform.""" + devices = [ChargerSwitch(device, hass.data[TESLA_DOMAIN]['controller']) + for device in hass.data[TESLA_DOMAIN]['devices']['switch']] + add_devices(devices, True) + + +class ChargerSwitch(TeslaDevice, SwitchDevice): + """Representation of a Tesla charger switch.""" + + def __init__(self, tesla_device, controller): + """Initialisation of the switch.""" + self._state = None + super().__init__(tesla_device, controller) + self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) + + def turn_on(self, **kwargs): + """Send the on command.""" + _LOGGER.debug("Enable charging: %s", self._name) + self.tesla_device.start_charge() + + def turn_off(self, **kwargs): + """Send the off command.""" + _LOGGER.debug("Disable charging for: %s", self._name) + self.tesla_device.stop_charge() + + @property + def is_on(self): + """Get whether the switch is in on state.""" + return self._state == STATE_ON + + def update(self): + """Updating state of the switch.""" + _LOGGER.debug("Updating state for: %s", self._name) + self.tesla_device.update() + self._state = STATE_ON if self.tesla_device.is_charging() \ + else STATE_OFF diff --git a/homeassistant/components/tesla.py b/homeassistant/components/tesla.py index 08006310dc7..915ebb6d4c3 100644 --- a/homeassistant/components/tesla.py +++ b/homeassistant/components/tesla.py @@ -17,7 +17,7 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.util import slugify -REQUIREMENTS = ['teslajsonpy==0.0.11'] +REQUIREMENTS = ['teslajsonpy==0.0.17'] DOMAIN = 'tesla' @@ -39,7 +39,7 @@ NOTIFICATION_ID = 'tesla_integration_notification' NOTIFICATION_TITLE = 'Tesla integration setup' TESLA_COMPONENTS = [ - 'sensor', 'lock', 'climate', 'binary_sensor', 'device_tracker' + 'sensor', 'lock', 'climate', 'binary_sensor', 'device_tracker', 'switch' ] @@ -55,7 +55,8 @@ def setup(hass, base_config): if hass.data.get(DOMAIN) is None: try: hass.data[DOMAIN] = { - 'controller': teslaApi(email, password, update_interval), + 'controller': teslaApi( + email, password, update_interval), 'devices': defaultdict(list) } _LOGGER.debug("Connected to the Tesla API.") diff --git a/requirements_all.txt b/requirements_all.txt index 67d4e762c3e..c2bc4befda6 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -993,7 +993,7 @@ tellduslive==0.3.4 temperusb==1.5.3 # homeassistant.components.tesla -teslajsonpy==0.0.11 +teslajsonpy==0.0.17 # homeassistant.components.thingspeak thingspeak==0.4.1