mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Support all Xiaomi Miio gateway switches (#46657)
* Support all gateway switches * fix checks * process revieuw * Update homeassistant/components/xiaomi_miio/switch.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * generilize variable matching * fix styling Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
99d1e3e71d
commit
b2efcb3c22
@ -26,7 +26,7 @@ from .gateway import ConnectXiaomiGateway
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
GATEWAY_PLATFORMS = ["alarm_control_panel", "sensor", "switch", "light"]
|
GATEWAY_PLATFORMS = ["alarm_control_panel", "light", "sensor", "switch"]
|
||||||
SWITCH_PLATFORMS = ["switch"]
|
SWITCH_PLATFORMS = ["switch"]
|
||||||
FAN_PLATFORMS = ["fan"]
|
FAN_PLATFORMS = ["fan"]
|
||||||
LIGHT_PLATFORMS = ["light"]
|
LIGHT_PLATFORMS = ["light"]
|
||||||
|
@ -21,9 +21,11 @@ from homeassistant.const import (
|
|||||||
CONF_TOKEN,
|
CONF_TOKEN,
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
DEVICE_CLASS_POWER,
|
||||||
DEVICE_CLASS_PRESSURE,
|
DEVICE_CLASS_PRESSURE,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
|
POWER_WATT,
|
||||||
PRESSURE_HPA,
|
PRESSURE_HPA,
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
)
|
)
|
||||||
@ -77,6 +79,9 @@ GATEWAY_SENSOR_TYPES = {
|
|||||||
"pressure": SensorType(
|
"pressure": SensorType(
|
||||||
unit=PRESSURE_HPA, icon=None, device_class=DEVICE_CLASS_PRESSURE
|
unit=PRESSURE_HPA, icon=None, device_class=DEVICE_CLASS_PRESSURE
|
||||||
),
|
),
|
||||||
|
"load_power": SensorType(
|
||||||
|
unit=POWER_WATT, icon=None, device_class=DEVICE_CLASS_POWER
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,11 @@ from miio import AirConditioningCompanionV3, ChuangmiPlug, DeviceException, Powe
|
|||||||
from miio.powerstrip import PowerMode
|
from miio.powerstrip import PowerMode
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
from homeassistant.components.switch import (
|
||||||
|
DEVICE_CLASS_SWITCH,
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
SwitchEntity,
|
||||||
|
)
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
from homeassistant.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
@ -25,12 +29,14 @@ from .const import (
|
|||||||
CONF_GATEWAY,
|
CONF_GATEWAY,
|
||||||
CONF_MODEL,
|
CONF_MODEL,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
KEY_COORDINATOR,
|
||||||
SERVICE_SET_POWER_MODE,
|
SERVICE_SET_POWER_MODE,
|
||||||
SERVICE_SET_POWER_PRICE,
|
SERVICE_SET_POWER_PRICE,
|
||||||
SERVICE_SET_WIFI_LED_OFF,
|
SERVICE_SET_WIFI_LED_OFF,
|
||||||
SERVICE_SET_WIFI_LED_ON,
|
SERVICE_SET_WIFI_LED_ON,
|
||||||
)
|
)
|
||||||
from .device import XiaomiMiioEntity
|
from .device import XiaomiMiioEntity
|
||||||
|
from .gateway import XiaomiGatewayDevice
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -40,6 +46,13 @@ DATA_KEY = "switch.xiaomi_miio"
|
|||||||
MODEL_POWER_STRIP_V2 = "zimi.powerstrip.v2"
|
MODEL_POWER_STRIP_V2 = "zimi.powerstrip.v2"
|
||||||
MODEL_PLUG_V3 = "chuangmi.plug.v3"
|
MODEL_PLUG_V3 = "chuangmi.plug.v3"
|
||||||
|
|
||||||
|
KEY_CHANNEL = "channel"
|
||||||
|
GATEWAY_SWITCH_VARS = {
|
||||||
|
"status_ch0": {KEY_CHANNEL: 0},
|
||||||
|
"status_ch1": {KEY_CHANNEL: 1},
|
||||||
|
"status_ch2": {KEY_CHANNEL: 2},
|
||||||
|
}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
@ -135,6 +148,25 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
model = config_entry.data[CONF_MODEL]
|
model = config_entry.data[CONF_MODEL]
|
||||||
unique_id = config_entry.unique_id
|
unique_id = config_entry.unique_id
|
||||||
|
|
||||||
|
if config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY:
|
||||||
|
gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY]
|
||||||
|
# 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 != "Switch":
|
||||||
|
continue
|
||||||
|
switch_variables = set(sub_device.status) & set(GATEWAY_SWITCH_VARS)
|
||||||
|
if switch_variables:
|
||||||
|
entities.extend(
|
||||||
|
[
|
||||||
|
XiaomiGatewaySwitch(
|
||||||
|
coordinator, sub_device, config_entry, variable
|
||||||
|
)
|
||||||
|
for variable in switch_variables
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
if config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE or (
|
if config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE or (
|
||||||
config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY
|
config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY
|
||||||
and model == "lumi.acpartner.v3"
|
and model == "lumi.acpartner.v3"
|
||||||
@ -227,6 +259,40 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_add_entities(entities, update_before_add=True)
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
|
|
||||||
|
class XiaomiGatewaySwitch(XiaomiGatewayDevice, SwitchEntity):
|
||||||
|
"""Representation of a XiaomiGatewaySwitch."""
|
||||||
|
|
||||||
|
def __init__(self, coordinator, sub_device, entry, variable):
|
||||||
|
"""Initialize the XiaomiSensor."""
|
||||||
|
super().__init__(coordinator, sub_device, entry)
|
||||||
|
self._channel = GATEWAY_SWITCH_VARS[variable][KEY_CHANNEL]
|
||||||
|
self._data_key = f"status_ch{self._channel}"
|
||||||
|
self._unique_id = f"{sub_device.sid}-ch{self._channel}"
|
||||||
|
self._name = f"{sub_device.name} ch{self._channel} ({sub_device.sid})"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self):
|
||||||
|
"""Return the device class of this entity."""
|
||||||
|
return DEVICE_CLASS_SWITCH
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Return true if switch is on."""
|
||||||
|
return self._sub_device.status[self._data_key] == "on"
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the switch on."""
|
||||||
|
await self.hass.async_add_executor_job(self._sub_device.on, self._channel)
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs):
|
||||||
|
"""Turn the switch off."""
|
||||||
|
await self.hass.async_add_executor_job(self._sub_device.off, self._channel)
|
||||||
|
|
||||||
|
async def async_toggle(self, **kwargs):
|
||||||
|
"""Toggle the switch."""
|
||||||
|
await self.hass.async_add_executor_job(self._sub_device.toggle, self._channel)
|
||||||
|
|
||||||
|
|
||||||
class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
|
class XiaomiPlugGenericSwitch(XiaomiMiioEntity, SwitchEntity):
|
||||||
"""Representation of a Xiaomi Plug Generic."""
|
"""Representation of a Xiaomi Plug Generic."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user