Skip to content

Commit

Permalink
enable python3 isort and fix with error
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwentao committed May 21, 2024
1 parent f92780d commit efac6cd
Show file tree
Hide file tree
Showing 94 changed files with 4,664 additions and 3,285 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ jobs:
VALIDATE_JSCPD: false
VALIDATE_PYTHON_PYLINT: false
VALIDATE_PYTHON_MYPY: false
VALIDATE_PYTHON_BLACK: false
VALIDATE_PYTHON_ISORT: false
# VALIDATE_PYTHON_BLACK: false
# VALIDATE_PYTHON_ISORT: false
90 changes: 43 additions & 47 deletions custom_components/midea_ac_lan/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from .const import (
DOMAIN,
CONF_ACCOUNT,
CONF_KEY,
CONF_MODEL,
CONF_SUBTYPE,
CONF_REFRESH_INTERVAL,
DEVICES,
EXTRA_SWITCH,
ALL_PLATFORM,
)
from .midea_devices import MIDEA_DEVICES

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.const import (CONF_CUSTOMIZE, CONF_DEVICE_ID,
CONF_IP_ADDRESS, CONF_NAME, CONF_PORT,
CONF_PROTOCOL, CONF_TOKEN, CONF_TYPE)
from homeassistant.core import HomeAssistant
from homeassistant.const import (
CONF_NAME,
CONF_TOKEN,
CONF_IP_ADDRESS,
CONF_PORT,
CONF_PROTOCOL,
CONF_DEVICE_ID,
CONF_TYPE,
CONF_CUSTOMIZE,
)

from .const import (ALL_PLATFORM, CONF_ACCOUNT, CONF_KEY, CONF_MODEL,
CONF_REFRESH_INTERVAL, CONF_SUBTYPE, DEVICES, DOMAIN,
EXTRA_SWITCH)
from .midea.devices import async_device_selector
from .midea_devices import MIDEA_DEVICES

_LOGGER = logging.getLogger(__name__)

Expand All @@ -34,18 +20,13 @@ async def update_listener(hass, config_entry):
for platform in ALL_PLATFORM:
await hass.config_entries.async_forward_entry_unload(config_entry, platform)
for platform in ALL_PLATFORM:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
config_entry, platform))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
device_id = config_entry.data.get(CONF_DEVICE_ID)
customize = config_entry.options.get(
CONF_CUSTOMIZE, ""
)
ip_address = config_entry.options.get(
CONF_IP_ADDRESS, None
)
refresh_interval = config_entry.options.get(
CONF_REFRESH_INTERVAL, None
)
customize = config_entry.options.get(CONF_CUSTOMIZE, "")
ip_address = config_entry.options.get(CONF_IP_ADDRESS, None)
refresh_interval = config_entry.options.get(CONF_REFRESH_INTERVAL, None)
dev = hass.data[DOMAIN][DEVICES].get(device_id)
if dev:
dev.set_customize(customize)
Expand All @@ -60,7 +41,10 @@ async def async_setup(hass: HomeAssistant, hass_config: dict):
attributes = []
for device_entities in MIDEA_DEVICES.values():
for attribute_name, attribute in device_entities.get("entities").items():
if attribute.get("type") in EXTRA_SWITCH and attribute_name.value not in attributes:
if (
attribute.get("type") in EXTRA_SWITCH
and attribute_name.value not in attributes
):
attributes.append(attribute_name.value)

def service_set_attribute(service):
Expand All @@ -72,11 +56,20 @@ def service_set_attribute(service):
if attr == "fan_speed" and value == "auto":
value = 102
item = MIDEA_DEVICES.get(dev.device_type).get("entities").get(attr)
if (item and (item.get("type") in EXTRA_SWITCH) or
(dev.device_type == 0xAC and attr == "fan_speed" and value in range(0, 103))):
if (
item
and (item.get("type") in EXTRA_SWITCH)
or (
dev.device_type == 0xAC
and attr == "fan_speed"
and value in range(0, 103)
)
):
dev.set_attribute(attr=attr, value=value)
else:
_LOGGER.error(f"Appliance [{device_id}] has no attribute {attr} or value is invalid")
_LOGGER.error(
f"Appliance [{device_id}] has no attribute {attr} or value is invalid"
)

def service_send_command(service):
device_id = service.data.get("device_id")
Expand All @@ -85,7 +78,9 @@ def service_send_command(service):
try:
cmd_body = bytearray.fromhex(cmd_body)
except ValueError:
_LOGGER.error(f"Appliance [{device_id}] invalid cmd_body, a hexadecimal string required")
_LOGGER.error(
f"Appliance [{device_id}] invalid cmd_body, a hexadecimal string required"
)
return
dev = hass.data[DOMAIN][DEVICES].get(device_id)
if dev:
Expand All @@ -99,9 +94,9 @@ def service_send_command(service):
{
vol.Required("device_id"): vol.Coerce(int),
vol.Required("attribute"): vol.In(attributes),
vol.Required("value"): vol.Any(int, cv.boolean, str)
vol.Required("value"): vol.Any(int, cv.boolean, str),
}
)
),
)

hass.services.async_register(
Expand All @@ -112,9 +107,9 @@ def service_send_command(service):
{
vol.Required("device_id"): vol.Coerce(int),
vol.Required("cmd_type"): vol.In([2, 3]),
vol.Required("cmd_body"): str
vol.Required("cmd_body"): str,
}
)
),
)
return True

Expand All @@ -128,7 +123,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry):
if name is None:
name = f"{device_id}"
if device_type is None:
device_type = 0xac
device_type = 0xAC
token = config_entry.data.get(CONF_TOKEN)
key = config_entry.data.get(CONF_KEY)
ip_address = config_entry.options.get(CONF_IP_ADDRESS, None)
Expand Down Expand Up @@ -167,8 +162,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry):
hass.data[DOMAIN][DEVICES] = {}
hass.data[DOMAIN][DEVICES][device_id] = device
for platform in ALL_PLATFORM:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
config_entry, platform))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, platform)
)
config_entry.add_update_listener(update_listener)
return True
return False
Expand Down
14 changes: 5 additions & 9 deletions custom_components/midea_ac_lan/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import Platform, CONF_DEVICE_ID, CONF_SENSORS
from .const import (
DOMAIN,
DEVICES
)
from .midea_entity import MideaEntity
from homeassistant.const import CONF_DEVICE_ID, CONF_SENSORS, Platform

from .const import DEVICES, DOMAIN
from .midea_devices import MIDEA_DEVICES
from .midea_entity import MideaEntity


async def async_setup_entry(hass, config_entry, async_add_entities):
device_id = config_entry.data.get(CONF_DEVICE_ID)
device = hass.data[DOMAIN][DEVICES].get(device_id)
extra_sensors = config_entry.options.get(
CONF_SENSORS, []
)
extra_sensors = config_entry.options.get(CONF_SENSORS, [])
binary_sensors = []
for entity_key, config in MIDEA_DEVICES[device.device_type]["entities"].items():
if config["type"] == Platform.BINARY_SENSOR and entity_key in extra_sensors:
Expand Down
Loading

0 comments on commit efac6cd

Please sign in to comment.