This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+43 -25
View File
@@ -8,8 +8,13 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType
from . import (
ATTR_AUTO_OFF_SET, ATTR_ELECTRIC_CURRENT, ATTR_REMAINING_TIME,
DATA_DEVICE, DOMAIN, SIGNAL_SWITCHER_DEVICE_UPDATE)
ATTR_AUTO_OFF_SET,
ATTR_ELECTRIC_CURRENT,
ATTR_REMAINING_TIME,
DATA_DEVICE,
DOMAIN,
SIGNAL_SWITCHER_DEVICE_UPDATE,
)
if TYPE_CHECKING:
from aioswitcher.devices import SwitcherV2Device
@@ -19,16 +24,19 @@ if TYPE_CHECKING:
_LOGGER = getLogger(__name__)
DEVICE_PROPERTIES_TO_HA_ATTRIBUTES = {
'power_consumption': ATTR_CURRENT_POWER_W,
'electric_current': ATTR_ELECTRIC_CURRENT,
'remaining_time': ATTR_REMAINING_TIME,
'auto_off_set': ATTR_AUTO_OFF_SET
"power_consumption": ATTR_CURRENT_POWER_W,
"electric_current": ATTR_ELECTRIC_CURRENT,
"remaining_time": ATTR_REMAINING_TIME,
"auto_off_set": ATTR_AUTO_OFF_SET,
}
async def async_setup_platform(hass: HomeAssistantType, config: Dict,
async_add_entities: Callable,
discovery_info: Dict) -> None:
async def async_setup_platform(
hass: HomeAssistantType,
config: Dict,
async_add_entities: Callable,
discovery_info: Dict,
) -> None:
"""Set up the switcher platform for the switch component."""
if discovery_info is None:
return
@@ -38,7 +46,7 @@ async def async_setup_platform(hass: HomeAssistantType, config: Dict,
class SwitcherControl(SwitchDevice):
"""Home Assistant switch entity."""
def __init__(self, device_data: 'SwitcherV2Device') -> None:
def __init__(self, device_data: "SwitcherV2Device") -> None:
"""Initialize the entity."""
self._self_initiated = False
self._device_data = device_data
@@ -57,13 +65,13 @@ class SwitcherControl(SwitchDevice):
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return "{}-{}".format(
self._device_data.device_id, self._device_data.mac_addr)
return "{}-{}".format(self._device_data.device_id, self._device_data.mac_addr)
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
from aioswitcher.consts import STATE_ON as SWITCHER_STATE_ON
return self._state == SWITCHER_STATE_ON
@property
@@ -88,16 +96,20 @@ class SwitcherControl(SwitchDevice):
@property
def available(self) -> bool:
"""Return True if entity is available."""
from aioswitcher.consts import (STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON)
from aioswitcher.consts import (
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON,
)
return self._state in [SWITCHER_STATE_ON, SWITCHER_STATE_OFF]
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
async_dispatcher_connect(
self.hass, SIGNAL_SWITCHER_DEVICE_UPDATE, self.async_update_data)
self.hass, SIGNAL_SWITCHER_DEVICE_UPDATE, self.async_update_data
)
async def async_update_data(self, device_data: 'SwitcherV2Device') -> None:
async def async_update_data(self, device_data: "SwitcherV2Device") -> None:
"""Update the entity data."""
if device_data:
if self._self_initiated:
@@ -124,20 +136,26 @@ class SwitcherControl(SwitchDevice):
async def _control_device(self, send_on: bool) -> None:
"""Turn the entity on or off."""
from aioswitcher.api import SwitcherV2Api
from aioswitcher.consts import (COMMAND_OFF, COMMAND_ON,
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON)
from aioswitcher.consts import (
COMMAND_OFF,
COMMAND_ON,
STATE_OFF as SWITCHER_STATE_OFF,
STATE_ON as SWITCHER_STATE_ON,
)
response = None # type: SwitcherV2ControlResponseMSG
async with SwitcherV2Api(
self.hass.loop, self._device_data.ip_addr,
self._device_data.phone_id, self._device_data.device_id,
self._device_data.device_password) as swapi:
self.hass.loop,
self._device_data.ip_addr,
self._device_data.phone_id,
self._device_data.device_id,
self._device_data.device_password,
) as swapi:
response = await swapi.control_device(
COMMAND_ON if send_on else COMMAND_OFF)
COMMAND_ON if send_on else COMMAND_OFF
)
if response and response.successful:
self._self_initiated = True
self._state = \
SWITCHER_STATE_ON if send_on else SWITCHER_STATE_OFF
self._state = SWITCHER_STATE_ON if send_on else SWITCHER_STATE_OFF
self.async_schedule_update_ha_state()