Skip to content

Commit

Permalink
Add Xiaomi Miio subdevice lightbulb support (home-assistant#46660)
Browse files Browse the repository at this point in the history
* Xiaomi Miio: add subdevice lightbulb support

* fix tests

* process revieuw comments

* bump python-miio to 0.5.5

* bump python-miio to 0.5.5

* fix imports
  • Loading branch information
starkillerOG authored Mar 14, 2021
1 parent 60838cf commit 50b5fc4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/xiaomi_miio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta
import logging

from miio.gateway import GatewayException
from miio.gateway.gateway import GatewayException

from homeassistant import config_entries, core
from homeassistant.const import CONF_HOST, CONF_TOKEN
Expand Down
66 changes: 65 additions & 1 deletion homeassistant/components/xiaomi_miio/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from math import ceil

from miio import Ceil, DeviceException, PhilipsBulb, PhilipsEyecare, PhilipsMoonlight
from miio.gateway import (
from miio.gateway.gateway import (
GATEWAY_MODEL_AC_V1,
GATEWAY_MODEL_AC_V2,
GATEWAY_MODEL_AC_V3,
Expand Down Expand Up @@ -36,6 +36,7 @@
CONF_GATEWAY,
CONF_MODEL,
DOMAIN,
KEY_COORDINATOR,
MODELS_LIGHT,
MODELS_LIGHT_BULB,
MODELS_LIGHT_CEILING,
Expand All @@ -52,6 +53,7 @@
SERVICE_SET_SCENE,
)
from .device import XiaomiMiioEntity
from .gateway import XiaomiGatewayDevice

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -148,6 +150,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entities.append(
XiaomiGatewayLight(gateway, config_entry.title, config_entry.unique_id)
)
# Gateway sub devices
sub_devices = gateway.devices
coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
for sub_device in sub_devices.values():
if sub_device.device_type == "LightBulb":
entities.append(
XiaomiGatewayBulb(coordinator, sub_device, config_entry)
)

if config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE:
if DATA_KEY not in hass.data:
Expand Down Expand Up @@ -1042,3 +1052,57 @@ async def async_update(self):
self._brightness_pct = state_dict["brightness"]
self._rgb = state_dict["rgb"]
self._hs = color.color_RGB_to_hs(*self._rgb)


class XiaomiGatewayBulb(XiaomiGatewayDevice, LightEntity):
"""Representation of Xiaomi Gateway Bulb."""

@property
def brightness(self):
"""Return the brightness of the light."""
return round((self._sub_device.status["brightness"] * 255) / 100)

@property
def color_temp(self):
"""Return current color temperature."""
return self._sub_device.status["color_temp"]

@property
def is_on(self):
"""Return true if light is on."""
return self._sub_device.status["status"] == "on"

@property
def min_mireds(self):
"""Return min cct."""
return self._sub_device.status["cct_min"]

@property
def max_mireds(self):
"""Return max cct."""
return self._sub_device.status["cct_max"]

@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP

async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on."""
await self.hass.async_add_executor_job(self._sub_device.on)

if ATTR_COLOR_TEMP in kwargs:
color_temp = kwargs[ATTR_COLOR_TEMP]
await self.hass.async_add_executor_job(
self._sub_device.set_color_temp, color_temp
)

if ATTR_BRIGHTNESS in kwargs:
brightness = round((kwargs[ATTR_BRIGHTNESS] * 100) / 255)
await self.hass.async_add_executor_job(
self._sub_device.set_brightness, brightness
)

async def async_turn_off(self, **kwargsf):
"""Instruct the light to turn off."""
await self.hass.async_add_executor_job(self._sub_device.off)
2 changes: 1 addition & 1 deletion homeassistant/components/xiaomi_miio/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Xiaomi Miio",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/xiaomi_miio",
"requirements": ["construct==2.10.56", "python-miio==0.5.4"],
"requirements": ["construct==2.10.56", "python-miio==0.5.5"],
"codeowners": ["@rytilahti", "@syssi", "@starkillerOG"],
"zeroconf": ["_miio._udp.local."]
}
2 changes: 1 addition & 1 deletion homeassistant/components/xiaomi_miio/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

from miio import AirQualityMonitor, DeviceException
from miio.gateway import (
from miio.gateway.gateway import (
GATEWAY_MODEL_AC_V1,
GATEWAY_MODEL_AC_V2,
GATEWAY_MODEL_AC_V3,
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@ python-juicenet==1.0.1
# python-lirc==1.2.3

# homeassistant.components.xiaomi_miio
python-miio==0.5.4
python-miio==0.5.5

# homeassistant.components.mpd
python-mpd2==3.0.4
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ python-izone==1.1.4
python-juicenet==1.0.1

# homeassistant.components.xiaomi_miio
python-miio==0.5.4
python-miio==0.5.5

# homeassistant.components.nest
python-nest==4.1.0
Expand Down

0 comments on commit 50b5fc4

Please sign in to comment.