Skip to content

Commit

Permalink
🔧 add support for python-miio v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Jul 11, 2024
1 parent 136de06 commit 181522b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 81 deletions.
11 changes: 6 additions & 5 deletions custom_components/xiaomi_miot/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@
RemoteEntity,
)

from miio.chuangmi_ir import (
ChuangmiIr,
DeviceException,
)

from . import (
DOMAIN,
CONF_MODEL,
XIAOMI_CONFIG_SCHEMA as PLATFORM_SCHEMA, # noqa: F401
MiotEntity,
DeviceException,
async_setup_config_entry,
bind_services_to_entries,
)
Expand All @@ -35,6 +31,11 @@
MiCloudException,
)

try:
from miio import ChuangmiIr
except (ModuleNotFoundError, ImportError):
from miio.integrations.chuangmi.remote import ChuangmiIr

_LOGGER = logging.getLogger(__name__)
DATA_KEY = f'{ENTITY_DOMAIN}.{DOMAIN}'

Expand Down
76 changes: 0 additions & 76 deletions custom_components/xiaomi_miot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
)
from homeassistant.helpers.restore_state import RestoreEntity, RestoredExtraData
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from miio.waterpurifier_yunmi import WaterPurifierYunmi

from . import (
DOMAIN,
Expand Down Expand Up @@ -534,81 +533,6 @@ def native_value(self):
return val


class WaterPurifierYunmiEntity(MiioEntity, Entity):
def __init__(self, config):
name = config[CONF_NAME]
host = config[CONF_HOST]
token = config[CONF_TOKEN]
_LOGGER.info('%s: Initializing with host %s (token %s...)', name, host, token[:5])

self._device = WaterPurifierYunmi(host, token)
super().__init__(name, self._device, config=config, logger=_LOGGER)
self._subs = {
'tds_in': {'keys': ['tds_warn_thd'], 'unit': CONCENTRATION_PARTS_PER_MILLION, 'icon': 'mdi:water'},
'tds_out': {'keys': ['tds_warn_thd'], 'unit': CONCENTRATION_PARTS_PER_MILLION, 'icon': 'mdi:water-check'},
'temperature': {'class': SensorDeviceClass.TEMPERATURE, 'unit': UnitOfTemperature.CELSIUS},
}
for i in [1, 2, 3]:
self._subs.update({
f'f{i}_remaining': {
'keys': [f'f{i}_totalflow', f'f{i}_usedflow'],
'unit': PERCENTAGE,
'icon': 'mdi:water-percent',
},
f'f{i}_remain_days': {
'keys': [f'f{i}_totaltime', f'f{i}_usedtime'],
'unit': UnitOfTime.DAYS,
'icon': 'mdi:clock',
},
})

@property
def state(self):
return self._state

@property
def icon(self):
return 'mdi:water-pump'

@property
def unit_of_measurement(self):
return CONCENTRATION_PARTS_PER_MILLION

async def async_update(self):
try:
status = await self.hass.async_add_executor_job(partial(self._device.status))
except DeviceException as ex:
if self._available:
self._available = False
_LOGGER.error('Got exception while fetching the state for %s: %s', self.entity_id, ex)
return
attrs = status.data or {}
_LOGGER.debug('Got new state from %s: %s', self.entity_id, attrs)
self._available = True
self._state = int(attrs.get('tds_out', 0))
self._state_attrs.update(attrs)
for i in [1, 2, 3]:
self._state_attrs.update({
f'f{i}_remaining': round(100 - 100 * attrs[f'f{i}_usedtime'] / attrs[f'f{i}_totaltime']),
f'f{i}_remain_days': round((attrs[f'f{i}_totaltime'] - attrs[f'f{i}_usedtime']) / 24),
})
self._state_attrs.update({
'errors': '|'.join(status.operation_status.errors),
})
add_entities = self._add_entities.get('sensor')
for k, v in self._subs.items():
if 'entity' in v:
v['entity'].update_from_parent()
elif add_entities:
v['entity'] = WaterPurifierYunmiSubEntity(self, k, v)
add_entities([v['entity']], update_before_add=True)


class WaterPurifierYunmiSubEntity(BaseSubEntity):
def __init__(self, parent: WaterPurifierYunmiEntity, attr, option=None):
super().__init__(parent, attr, option)


class MihomeMessageSensor(MiCoordinatorEntity, SensorEntity, RestoreEntity):
_filter_homes = None
_exclude_types = None
Expand Down

0 comments on commit 181522b

Please sign in to comment.