hass-core/homeassistant/components/starline/switch.py
Nikolay Vasilchuk a37260faa9 StarLine integration (#27197)
* Device Tracker works

* Device Tracker works

* Binary Sensor

* Sensor

* Lock

* Switch and service

* New switches

* Update interval options

* WIP

* Translation errors

* Check online state

* WIP

* Move to aiohttp

* Some checks

* CI

* CI

* .coveragerc

* Black

* icon_for_signal_level test

* update_interval renamed to scan_interval

* async logic

* Fix cookie read

* Requirement starline

* Reformat

* Requirements updated

* ConfigEntryNotReady

* Requirement starline

* Lint fix

* Requirement starline

* available status

* Translations

* Expiration to config

* CI

* Linter fix

* Small renaming

* Update slnet token

* Starline version bump

* Fix updates

* Black

* Small fix

* Removed unused fields

* CI

* set_scan_interval service

* deps updated

* Horn switch

* Starline lib updated

* Starline lib updated

* Black

* Support multiple integrations

* Review

* async_will_remove_from_hass

* Deps updated

* Test config flow

* Requirements

* CI

* Review

* Review

* Review

* Review

* Review

* CI

* pylint fix

* Review

* Support "mayak" devices

* Icons removed

* Removed options_flow

* Removed options_flow test

* Removed options_flow test
2019-11-26 11:17:11 -08:00

86 lines
2.7 KiB
Python

"""Support for StarLine switch."""
from homeassistant.components.switch import SwitchDevice
from .account import StarlineAccount, StarlineDevice
from .const import DOMAIN
from .entity import StarlineEntity
SWITCH_TYPES = {
"ign": ["Engine", "mdi:engine-outline", "mdi:engine-off-outline"],
"webasto": ["Webasto", "mdi:radiator", "mdi:radiator-off"],
"out": [
"Additional Channel",
"mdi:access-point-network",
"mdi:access-point-network-off",
],
"poke": ["Horn", "mdi:bullhorn-outline", "mdi:bullhorn-outline"],
}
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the StarLine switch."""
account: StarlineAccount = hass.data[DOMAIN][entry.entry_id]
entities = []
for device in account.api.devices.values():
if device.support_state:
for key, value in SWITCH_TYPES.items():
switch = StarlineSwitch(account, device, key, *value)
if switch.is_on is not None:
entities.append(switch)
async_add_entities(entities)
class StarlineSwitch(StarlineEntity, SwitchDevice):
"""Representation of a StarLine switch."""
def __init__(
self,
account: StarlineAccount,
device: StarlineDevice,
key: str,
name: str,
icon_on: str,
icon_off: str,
):
"""Initialize the switch."""
super().__init__(account, device, key, name)
self._icon_on = icon_on
self._icon_off = icon_off
@property
def available(self):
"""Return True if entity is available."""
return super().available and self._device.online
@property
def device_state_attributes(self):
"""Return the state attributes of the switch."""
if self._key == "ign":
return self._account.engine_attrs(self._device)
return None
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon_on if self.is_on else self._icon_off
@property
def assumed_state(self):
"""Return True if unable to access real state of the entity."""
return True
@property
def is_on(self):
"""Return True if entity is on."""
if self._key == "poke":
return False
return self._device.car_state.get(self._key)
def turn_on(self, **kwargs):
"""Turn the entity on."""
self._account.api.set_car_state(self._device.device_id, self._key, True)
def turn_off(self, **kwargs) -> None:
"""Turn the entity off."""
if self._key == "poke":
return
self._account.api.set_car_state(self._device.device_id, self._key, False)