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

Fixed deprecated entities and DoHomeDevice.__init__() in light component #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
except ImportError:
from custom_components.dohome import (DOHOME_GATEWAY, DoHomeDevice)

from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.binary_sensor import BinarySensorEntity


NO_CLOSE = 'no_close'
Expand All @@ -27,6 +27,7 @@

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Perform the setup for Xiaomi devices."""
sensor_devices = []
Expand All @@ -36,12 +37,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.info(device)
if(device['type'] == '_MOTION' or device['type'] == '_THIMR'):
sensor_devices.append(MotionSensor(hass, device))

if(len(sensor_devices) > 0):
add_devices(sensor_devices)


class MotionSensor(DoHomeDevice, BinarySensorDevice):
class MotionSensor(DoHomeDevice, BinarySensorEntity):

def __init__(self, hass, device):
self._device = device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
PLATFORM_SCHEMA,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
Light,
LightEntity,
)

try:
Expand All @@ -22,6 +22,7 @@

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_devices, discovery_info=None):
light_devices = []
devices = DOHOME_GATEWAY.devices
Expand All @@ -30,12 +31,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.info(device)
if(device['type'] == '_DT-WYRGB'):
light_devices.append(DoHomeLight(hass, device))

if(len(light_devices) > 0):
add_devices(light_devices)


class DoHomeLight(DoHomeDevice, Light):
class DoHomeLight(DoHomeDevice, LightEntity):

def __init__(self, hass, device):
self._device = device
Expand All @@ -44,7 +45,7 @@ def __init__(self, hass, device):
self._brightness = 100
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

DoHomeDevice.__init__(self, device)
DoHomeDevice.__init__(self, device['name'], device)

@property
def brightness(self):
Expand Down Expand Up @@ -76,27 +77,29 @@ def turn_on(self, **kwargs):

self._state = True
data = {
"cmd":6,
"r":int(50 * self._rgb[0] / 255)*self._brightness,
"g":int(50 * self._rgb[1] / 255)*self._brightness,
"b":int(50 * self._rgb[2] / 255)*self._brightness,
"w":0,
"m":0}
"cmd": 6,
"r": int(50 * self._rgb[0] / 255)*self._brightness,
"g": int(50 * self._rgb[1] / 255)*self._brightness,
"b": int(50 * self._rgb[2] / 255)*self._brightness,
"w": 0,
"m": 0}
op = json.dumps(data)
self._send_cmd(self._device,'cmd=ctrl&devices={[' + self._device["sid"] + ']}&op=' + op + '}', 6)
self._send_cmd(
self._device, 'cmd=ctrl&devices={[' + self._device["sid"] + ']}&op=' + op + '}', 6)

def turn_off(self, **kwargs):
"""Turn the light off."""
self._state = False
data = {
"cmd":6,
"r":0,
"g":0,
"b":0,
"w":0,
"m":0}
"cmd": 6,
"r": 0,
"g": 0,
"b": 0,
"w": 0,
"m": 0}
op = json.dumps(data)
self._send_cmd(self._device,'cmd=ctrl&devices={[' + self._device["sid"] + ']}&op=' + op + '}', 6)
self._send_cmd(
self._device, 'cmd=ctrl&devices={[' + self._device["sid"] + ']}&op=' + op + '}', 6)

def _send_cmd(self, device, cmd, rtn_cmd):

Expand All @@ -110,14 +113,17 @@ def _send_cmd(self, device, cmd, rtn_cmd):
if data is None:
return None
_LOGGER.debug("result :%s", data.decode("utf-8"))
dic = {i.split("=")[0]:i.split("=")[1] for i in data.decode("utf-8").split("&")}
dic = {i.split("=")[0]: i.split("=")[1]
for i in data.decode("utf-8").split("&")}
resp = []
if(dic["dev"][8:12] == device["sid"]):
resp = json.loads(dic["op"])
if resp['cmd'] != rtn_cmd:
_LOGGER.debug("Non matching response. Expecting %s, but got %s", rtn_cmd, resp['cmd'])
_LOGGER.debug(
"Non matching response. Expecting %s, but got %s", rtn_cmd, resp['cmd'])
return None
return resp
else:
_LOGGER.debug("Non matching response. device %s, but got %s", device["sid"], dic["dev"][8:12])
return None
_LOGGER.debug("Non matching response. device %s, but got %s",
device["sid"], dic["dev"][8:12])
return None
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import timedelta
from homeassistant.helpers.event import track_time_interval

from homeassistant.components.switch import SwitchDevice
from homeassistant.components.switch import SwitchEntity
try:
from homeassistant.components.dohome import (DOHOME_GATEWAY, DoHomeDevice)
except ImportError:
Expand Down Expand Up @@ -35,7 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices(switch_devices)


class DoHomeSwitch(DoHomeDevice, SwitchDevice):
class DoHomeSwitch(DoHomeDevice, SwitchEntity):

def __init__(self, hass, name, data_key, device):
self._device = device
Expand Down