From 1e832dc9ec57c2523441f3f45756f39006571d70 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Mon, 21 Oct 2019 01:59:58 -0700 Subject: [PATCH] Bump teslajsonpy and add update switch (#27957) * bump teslajsonpy to 0.0.26 breaking change * add update switch to tesla * bump requirements_all.txt for teslajsonpy * address requested style changes * fix bug where update switch not loaded --- homeassistant/components/tesla/manifest.json | 8 +--- homeassistant/components/tesla/switch.py | 39 ++++++++++++++++++-- requirements_all.txt | 2 +- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/tesla/manifest.json b/homeassistant/components/tesla/manifest.json index 4071178c7c3..87d76c16f05 100644 --- a/homeassistant/components/tesla/manifest.json +++ b/homeassistant/components/tesla/manifest.json @@ -2,11 +2,7 @@ "domain": "tesla", "name": "Tesla", "documentation": "https://www.home-assistant.io/integrations/tesla", - "requirements": [ - "teslajsonpy==0.0.25" - ], + "requirements": ["teslajsonpy==0.0.26"], "dependencies": [], - "codeowners": [ - "@zabuldon" - ] + "codeowners": ["@zabuldon"] } diff --git a/homeassistant/components/tesla/switch.py b/homeassistant/components/tesla/switch.py index 19ac76018ff..985194f87b2 100644 --- a/homeassistant/components/tesla/switch.py +++ b/homeassistant/components/tesla/switch.py @@ -11,11 +11,12 @@ _LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Tesla switch platform.""" - controller = hass.data[TESLA_DOMAIN]["devices"]["controller"] + controller = hass.data[TESLA_DOMAIN]["controller"] devices = [] for device in hass.data[TESLA_DOMAIN]["devices"]["switch"]: if device.bin_type == 0x8: devices.append(ChargerSwitch(device, controller)) + devices.append(UpdateSwitch(device, controller)) elif device.bin_type == 0x9: devices.append(RangeSwitch(device, controller)) add_entities(devices, True) @@ -72,10 +73,42 @@ class RangeSwitch(TeslaDevice, SwitchDevice): @property def is_on(self): """Get whether the switch is in on state.""" - return self._state == STATE_ON + return self._state def update(self): """Update the 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_maxrange() else STATE_OFF + self._state = bool(self.tesla_device.is_maxrange()) + + +class UpdateSwitch(TeslaDevice, SwitchDevice): + """Representation of a Tesla update switch.""" + + def __init__(self, tesla_device, controller): + """Initialise of the switch.""" + self._state = None + super().__init__(tesla_device, controller) + self._name = self._name.replace("charger", "update") + self.tesla_id = self.tesla_id.replace("charger", "update") + + def turn_on(self, **kwargs): + """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) + + def turn_off(self, **kwargs): + """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 + + def update(self): + """Update the state of the switch.""" + car_id = self.tesla_device.id() + _LOGGER.debug("Updating state for: %s %s", self._name, car_id) + self._state = bool(self.controller.get_updates(car_id)) diff --git a/requirements_all.txt b/requirements_all.txt index 3c649e91756..81e31c81817 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1876,7 +1876,7 @@ temperusb==1.5.3 # tensorflow==1.13.2 # homeassistant.components.tesla -teslajsonpy==0.0.25 +teslajsonpy==0.0.26 # homeassistant.components.thermoworks_smoke thermoworks_smoke==0.1.8