Skip to content

Commit

Permalink
Maintenance
Browse files Browse the repository at this point in the history
- Range device clean-up and minor review
- Added "network type" check
- Implemented device factory
  • Loading branch information
ollo69 committed May 22, 2021
1 parent 41be11b commit 9523dfd
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 106 deletions.
32 changes: 7 additions & 25 deletions custom_components/smartthinq_sensors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
from .wideq.core import Client
from .wideq.core_v2 import ClientV2, CoreV2HttpAdapter
from .wideq.device import UNIT_TEMP_CELSIUS, UNIT_TEMP_FAHRENHEIT, DeviceType
from .wideq.ac import AirConditionerDevice
from .wideq.dishwasher import DishWasherDevice
from .wideq.dryer import DryerDevice
from .wideq.range import RangeDevice
from .wideq.refrigerator import RefrigeratorDevice
from .wideq.styler import StylerDevice
from .wideq.washer import WasherDevice
from .wideq.factory import get_lge_device

from .wideq.core_exceptions import (
InvalidCredentialError,
Expand Down Expand Up @@ -563,34 +557,22 @@ async def lge_devices_setup(hass, client) -> dict:
device_id = device.id
device_name = device.name
device_type = device.type
network_type = device.network_type
model_name = device.model_name
device_count += 1

dev = None
if device_type in [DeviceType.WASHER, DeviceType.TOWER_WASHER]:
dev = LGEDevice(WasherDevice(client, device), hass)
elif device_type in [DeviceType.DRYER, DeviceType.TOWER_DRYER]:
dev = LGEDevice(DryerDevice(client, device), hass)
elif device_type == DeviceType.STYLER:
dev = LGEDevice(StylerDevice(client, device), hass)
elif device_type == DeviceType.DISHWASHER:
dev = LGEDevice(DishWasherDevice(client, device), hass)
elif device_type == DeviceType.REFRIGERATOR:
dev = LGEDevice(RefrigeratorDevice(client, device), hass)
elif device_type == DeviceType.AC:
dev = LGEDevice(AirConditionerDevice(client, device, temp_unit), hass)
elif device_type == DeviceType.RANGE:
dev = LGEDevice(RangeDevice(client, device), hass)

if not dev:
lge_dev = get_lge_device(client, device, temp_unit)
if not lge_dev:
_LOGGER.info(
"Found unsupported LGE Device. Name: %s - Type: %s - InfoUrl: %s",
"Found unsupported LGE Device. Name: %s - Type: %s - NetworkType: %s - InfoUrl: %s",
device_name,
device_type.name,
network_type.name,
device.model_info_url,
)
continue

dev = LGEDevice(lge_dev, hass)
if not await dev.init_device():
_LOGGER.error(
"Error initializing LGE Device. Name: %s - Type: %s - InfoUrl: %s",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartthinq_sensors/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Support to interface with LGE ThinQ Devices.
"""

__version__ = "0.7.2"
__version__ = "0.8.0"
PROJECT_URL = "https://github.com/ollo69/ha-smartthinq-sensors/"
ISSUE_URL = "{}issues".format(PROJECT_URL)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartthinq_sensors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"requirements": ["pycountry>=20.7.3"],
"config_flow": true,
"iot_class": "cloud_polling",
"version": "0.7.2"
"version": "0.8.0"
}
35 changes: 17 additions & 18 deletions custom_components/smartthinq_sensors/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
FEAT_TUBCLEAN_COUNT,
FEAT_TEMPCONTROL,
FEAT_WATERTEMP,
FEAT_COOKTOP_STATE,
FEAT_COOKTOP_LEFT_FRONT_STATE,
FEAT_COOKTOP_LEFT_REAR_STATE,
FEAT_COOKTOP_CENTER_STATE,
FEAT_COOKTOP_RIGHT_FRONT_STATE,
FEAT_COOKTOP_RIGHT_REAR_STATE,
FEAT_OVEN_STATE,
FEAT_OVEN_LOWER_STATE,
FEAT_OVEN_UPPER_STATE,
)
Expand All @@ -45,7 +43,6 @@
DEVICE_CLASS_LOCK,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_PROBLEM,
DEVICE_CLASS_POWER,
)

from homeassistant.const import (
Expand Down Expand Up @@ -89,9 +86,11 @@
# ac sensor attributes
ATTR_ROOM_TEMP = "room_temperature"

# oven sensor attributes
# range sensor attributes
ATTR_COOKTOP_STATE = "cooktop_state"
ATTR_OVEN_LOWER_TARGET_TEMP = "oven_lower_target_temp"
ATTR_OVEN_UPPER_TARGET_TEMP = "oven_upper_target_temp"
ATTR_OVEN_STATE = "oven_state"
ATTR_OVEN_TEMP_UNIT = "oven_temp_unit"

STATE_LOOKUP = {
Expand Down Expand Up @@ -304,15 +303,6 @@
},
}

WASH_DEVICE_TYPES = [
DeviceType.DISHWASHER,
DeviceType.DRYER,
DeviceType.STYLER,
DeviceType.TOWER_DRYER,
DeviceType.TOWER_WASHER,
DeviceType.WASHER,
]

RANGE_SENSORS = {
DEFAULT_SENSOR: {
ATTR_MEASUREMENT_NAME: "Default",
Expand Down Expand Up @@ -373,17 +363,26 @@
},
}
RANGE_BINARY_SENSORS = {
FEAT_COOKTOP_STATE: {
ATTR_COOKTOP_STATE: {
ATTR_MEASUREMENT_NAME: "Cooktop",
ATTR_VALUE_FN: lambda x: x._cooktop_state,
ATTR_ENABLED: True,
},
FEAT_OVEN_STATE: {
ATTR_OVEN_STATE: {
ATTR_MEASUREMENT_NAME: "Oven",
ATTR_VALUE_FN: lambda x: x._oven_state,
},
}

WASH_DEVICE_TYPES = [
DeviceType.DISHWASHER,
DeviceType.DRYER,
DeviceType.STYLER,
DeviceType.TOWER_DRYER,
DeviceType.TOWER_WASHER,
DeviceType.WASHER,
]


def _sensor_exist(lge_device, sensor_def):
"""Check if a sensor exist for device."""
Expand Down Expand Up @@ -840,6 +839,6 @@ def _oven_upper_target_temp(self):
@property
def _oven_temp_unit(self):
if self._api.state:
return self._api.state.oven_temp_unit
return None

unit = self._api.state.oven_temp_unit
return TEMP_UNIT_LOOKUP.get(unit, TEMP_CELSIUS)
return TEMP_CELSIUS
5 changes: 2 additions & 3 deletions custom_components/smartthinq_sensors/wideq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@
# FEAT_SMARTSAVING_STATE = "smart_saving_state"
FEAT_WATERFILTERUSED_MONTH = "water_filter_used_month"

# oven device features
FEAT_COOKTOP_STATE = "cooktop_state"
# range device features
FEAT_COOKTOP_LEFT_FRONT_STATE = "cooktop_left_front_state"
FEAT_COOKTOP_LEFT_REAR_STATE = "cooktop_left_rear_state"
FEAT_COOKTOP_CENTER_STATE = "cooktop_center_state"
FEAT_COOKTOP_RIGHT_FRONT_STATE = "cooktop_right_front_state"
FEAT_COOKTOP_RIGHT_REAR_STATE = "cooktop_right_rear_state"
FEAT_OVEN_STATE = "oven_state"
FEAT_OVEN_LOWER_STATE = "oven_lower_state"
FEAT_OVEN_UPPER_STATE = "oven_upper_state"

# request ciphers settings
CIPHERS = ":HIGH:!DH:!aNULL"


Expand Down
47 changes: 39 additions & 8 deletions custom_components/smartthinq_sensors/wideq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ class PlatformType(enum.Enum):
UNKNOWN = STATE_OPTIONITEM_UNKNOWN


class NetworkType(enum.Enum):
"""The type of network."""

WIFI = "02"
NFC3 = "03"
NFC4 = "04"
UNKNOWN = STATE_OPTIONITEM_UNKNOWN


_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -239,19 +248,38 @@ def type(self) -> DeviceType:
try:
ret_val = DeviceType(device_type)
except ValueError:
_LOGGER.warning("Unknown device type with id %s", device_type)
_LOGGER.warning("Device %s: unknown device type with id %s", self.id, device_type)
ret_val = DeviceType.UNKNOWN
return ret_val

@property
def platform_type(self) -> PlatformType:
"""The kind of device, as a `DeviceType` value."""
ptype = self._data.get("platformType")
if not ptype:
return (
PlatformType.THINQ1
) # for the moment, probably not available in APIv1
return PlatformType(ptype)
"""The kind of platform, as a `PlatformType` value."""
plat_type = self._data.get("platformType")
if plat_type is None:
# for the moment, probably not available in APIv1
return PlatformType.THINQ1
try:
ret_val = PlatformType(plat_type)
except ValueError:
_LOGGER.warning("Device %s: unknown platform type with id %s", self.id, plat_type)
ret_val = PlatformType.UNKNOWN
return ret_val

@property
def network_type(self) -> NetworkType:
"""The kind of network, as a `NetworkType` value."""
net_type = self._data.get("networkType")
if net_type is None:
# for the moment we set WIFI if not available
return NetworkType.WIFI
try:
ret_val = NetworkType(net_type)
except ValueError:
_LOGGER.warning("Device %s: unknown network type with id %s", self.id, net_type)
# for the moment we set WIFI if unknown
ret_val = NetworkType.WIFI
return ret_val

@property
def snapshot(self) -> Optional[Dict[str, Any]]:
Expand Down Expand Up @@ -847,6 +875,9 @@ def _get_control(self, key):
return value

def init_device_info(self):
if self._device_info.platform_type == PlatformType.UNKNOWN:
return False

if self._model_info is None:
if self._model_data is None:
self._model_data = self._client.model_url_info(
Expand Down
37 changes: 37 additions & 0 deletions custom_components/smartthinq_sensors/wideq/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

from .ac import AirConditionerDevice
from .dishwasher import DishWasherDevice
from .dryer import DryerDevice
from .range import RangeDevice
from .refrigerator import RefrigeratorDevice
from .styler import StylerDevice
from .washer import WasherDevice

from .device import UNIT_TEMP_CELSIUS, DeviceInfo, DeviceType, NetworkType


def get_lge_device(client, device: DeviceInfo, temp_unit=UNIT_TEMP_CELSIUS):
"""Return a device based on the device type."""

device_type = device.type
network_type = device.network_type

if network_type != NetworkType.WIFI:
return None

if device_type == DeviceType.AC:
return AirConditionerDevice(client, device, temp_unit)
if device_type == DeviceType.DISHWASHER:
return DishWasherDevice(client, device)
if device_type in [DeviceType.DRYER, DeviceType.TOWER_DRYER]:
return DryerDevice(client, device)
if device_type == DeviceType.RANGE:
return RangeDevice(client, device)
if device_type == DeviceType.REFRIGERATOR:
return RefrigeratorDevice(client, device)
if device_type == DeviceType.STYLER:
return StylerDevice(client, device)
if device_type in [DeviceType.WASHER, DeviceType.TOWER_WASHER]:
return WasherDevice(client, device)

return None
Loading

0 comments on commit 9523dfd

Please sign in to comment.