hass-core/homeassistant/components/nissan_leaf/switch.py
Phil Cole caa3b123ae Nissanleaf (#21145)
* Remove unneeded returns from handle_update()

* Start __init__() params with hass.

* Remove excess logging and downgrade remaining logging.

* Remove period from end of comment

* Decorate callback with @callback

* Use more descriptive variables than key and value.

* Inherit from BinarySensorDevice and overwrite is_on rather than state.

* Removed uncheckedreturn values.

* Use super() rather than explicit object.

* Use add_entities instead of add_devices.

* Don't use listener when calling immediately.

* Remove some excess logging.

* Switch to sync since pycarwings2 is sync.

* Remove RuntimeError exception matching.

* Add temporary reviewer comments.

* Add UI help descriptions for update service.

* Fix hound errors.

* Replaced time.sleep() with await asyncio.sleep()

* Removed location_updateon_on attribute since on device_tracker.

* Use async_added_to_hass() and async_dispatcher_connect().

* Use dict[key] because schema key is required.

* Clarify variable names.

* Remove icon for charging switch.

* Convert LeafChargeSwitch into service and sensor.

* Use async_dispatcher_send().

* Add guard checks for discovery_info. Consistent logs.

* Use async_schedul_update_ha_state().

* Device tracker should return true.

* Remove icon for climate control.

* Really remove icon for climate control.

* Use register() instead of async_register().

* Add guard on device tracker if discovery_info is None.
2019-02-22 17:34:23 +01:00

60 lines
1.8 KiB
Python

"""Charge and Climate Control Support for the Nissan Leaf."""
import logging
from homeassistant.components.nissan_leaf import (
DATA_CLIMATE, DATA_LEAF, LeafEntity)
from homeassistant.helpers.entity import ToggleEntity
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['nissan_leaf']
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Nissan Leaf switch platform setup."""
if discovery_info is None:
return
devices = []
for vin, datastore in hass.data[DATA_LEAF].items():
_LOGGER.debug("Adding switch for vin=%s", vin)
devices.append(LeafClimateSwitch(datastore))
add_devices(devices, True)
class LeafClimateSwitch(LeafEntity, ToggleEntity):
"""Nissan Leaf Climate Control switch."""
@property
def name(self):
"""Switch name."""
return "{} {}".format(self.car.leaf.nickname, "Climate Control")
def log_registration(self):
"""Log registration."""
_LOGGER.debug(
"Registered LeafClimateSwitch component with HASS for VIN %s",
self.car.leaf.vin)
@property
def device_state_attributes(self):
"""Return climate control attributes."""
attrs = super().device_state_attributes
attrs["updated_on"] = self.car.last_climate_response
return attrs
@property
def is_on(self):
"""Return true if climate control is on."""
return self.car.data[DATA_CLIMATE]
async def async_turn_on(self, **kwargs):
"""Turn on climate control."""
if await self.car.async_set_climate(True):
self.car.data[DATA_CLIMATE] = True
async def async_turn_off(self, **kwargs):
"""Turn off climate control."""
if await self.car.async_set_climate(False):
self.car.data[DATA_CLIMATE] = False