Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switchbot #16396

Merged
merged 7 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ omit =
homeassistant/components/switch/rest.py
homeassistant/components/switch/rpi_rf.py
homeassistant/components/switch/snmp.py
homeassistant/components/switch/switchbot.py
homeassistant/components/switch/switchmate.py
homeassistant/components/switch/telnet.py
homeassistant/components/switch/tplink.py
Expand Down
73 changes: 73 additions & 0 deletions homeassistant/components/switch/switchbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Support for Switchbot.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.switchbot
"""
import logging

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_MAC

REQUIREMENTS = ['PySwitchbot==0.3']

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Switchbot'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Perform the setup for Switchbot devices."""
name = config.get(CONF_NAME)
mac_addr = config[CONF_MAC]
add_entities([SwitchBot(mac_addr, name)])


class SwitchBot(SwitchDevice):
"""Representation of a Switchbot."""

def __init__(self, mac, name) -> None:
"""Initialize the Switchbot."""
import switchbot
self._state = False
self._name = name
self._mac = mac
self._device = switchbot.Switchbot(mac=mac)

def turn_on(self, **kwargs) -> None:
"""Turn device on."""
if self._device.turn_on():
self._state = True

def turn_off(self, **kwargs) -> None:
"""Turn device off."""
if self._device.turn_off():
self._state = False

@property
def assumed_state(self) -> bool:
"""Return true if unable to access real state of entity."""
return True

@property
def is_on(self) -> bool:
"""Return true if device is on."""
return self._state

@property
def unique_id(self) -> str:
"""Return a unique, HASS-friendly identifier for this entity."""
return self._mac.replace(':', '')

@property
def name(self) -> str:
"""Return the name of the switch."""
return self._name
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ PyQRCode==1.2.1
# homeassistant.components.sensor.rmvtransport
PyRMVtransport==0.0.7

# homeassistant.components.switch.switchbot
PySwitchbot==0.3

# homeassistant.components.xiaomi_aqara
PyXiaomiGateway==0.9.5

Expand Down