Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporto for A1 purifier #2

Closed
maury77 opened this issue Feb 28, 2021 · 4 comments
Closed

Supporto for A1 purifier #2

maury77 opened this issue Feb 28, 2021 · 4 comments

Comments

@maury77
Copy link

maury77 commented Feb 28, 2021

is there way to support model 1A (MR 432-D)?

I can also do tests if you need to debug

@maury77
Copy link
Author

maury77 commented Mar 7, 2021

for resolve my problem I modified the file in this mode

        tap_water=self._device.send('get_prop', ["tds_in"])
        data[TAP_WATER_QUALITY['key']] =int(tap_water[0]) 
        filtered_water=self._device.send('get_prop', ["tds_out"])
        data[FILTERED_WATER_QUALITY['key']] = int(filtered_water[0]) 
        temperature= self._device.send('get_prop', ["temperature"])
        data[TEMPERATURE['key']] = int(temperature[0])
        rinse= self._device.send('get_prop', ["rinse"])
        data[RINSE['key']] = rinse[0]
        run_status=self._device.send('get_prop', ["run_status"])
        data[RUN_STATUS['key']] = run_status[0]
        
        _LOGGER.info("Initializing Xiaomi water purifier with  (water quality %s...)", data[FILTERED_WATER_QUALITY['key']] )                                                        
                                                                
        f1_totaltime=self._device.send('get_prop', ["f1_totaltime"])
        f1_usedflow=self._device.send('get_prop', ["f1_usedflow"])
        f1_totalflow=self._device.send('get_prop', ["f1_totalflow"])
        f2_totalflow=self._device.send('get_prop', ["f2_totalflow"])
        f1_usedtime=self._device.send('get_prop', ["f1_usedtime"])    
        pfd = int((f1_totaltime[0] - f1_usedtime[0]) / 24)                
        _LOGGER.info("Initializing Xiaomi water purifier with  (water pfd %s...)", pfd )      
        data[COMPOSITE_REMAINING['days_key']] = pfd
        data[COMPOSITE_REMAINING['key']] = math.floor(pfd * 24 * 100 /f1_totaltime[0])
                                      
        f2_totaltime=self._device.send('get_prop', ["f2_totaltime"])
        f2_usedtime=self._device.send('get_prop', ["f2_usedtime"])   
        rfd = int((f2_totaltime[0] - f2_usedtime[0]) / 24)   
        data[RO_FILTER_REMAINING['days_key']] = rfd
        data[RO_FILTER_REMAINING['key']] = math.floor(rfd * 24 * 100 /f2_totaltime[0])

@Nagg35
Copy link

Nagg35 commented Oct 2, 2021

Hi maury77,

May you share with us your sensor.py file ?
I didn't manage to make my A1 purifier work with Home Asisstant.
Thanks

@maury77
Copy link
Author

maury77 commented Oct 3, 2021

The solution is not optimal because it makes a call for each attribute, but it works

this my sensor.py

"""Support for Xiaomi water purifier."""
import math
import logging

from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_TOKEN, )
from homeassistant.helpers.entity import Entity
from homeassistant.exceptions import PlatformNotReady
from miio import Device, DeviceException

_LOGGER = logging.getLogger(name)

REQUIREMENTS = ['python-miio>=0.3.1']

TAP_WATER_QUALITY = {'name': 'Tap water', 'key': 'ttds'}
FILTERED_WATER_QUALITY = {'name': 'Filtered water', 'key': 'ftds'}
PP_COTTON_FILTER_REMAINING = {'name': 'Composite filter', 'key': 'pfd', 'days_key': 'pfp'}
RO_FILTER_REMAINING = {'name': 'RO filter', 'key': 'rfd', 'days_key': 'rfp'}
TEMPERATURE = {'name': 'Temperature water', 'key': 'temperature'}
RINSE = {'name': 'Rinse', 'key': 'rinse'}
WATER_USED = {'name': 'Water used', 'key': 'water_used'}
WATER_PURIFIED = {'name': 'Water purified', 'key': 'water_purified'}
RUN_STATUS = {'name': 'Run status', 'key': 'run_status'}

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Perform the setup for Xiaomi water purifier."""

host = config.get(CONF_HOST)
name = config.get(CONF_NAME)
token = config.get(CONF_TOKEN)

_LOGGER.info("Initializing Xiaomi water purifier with host %s (token %s...)", host, token[:5])

devices = []
try:
    device = Device(host, token)
    waterPurifier = XiaomiWaterPurifier(device, name)
    devices.append(waterPurifier)
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, TAP_WATER_QUALITY))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, FILTERED_WATER_QUALITY))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, PP_COTTON_FILTER_REMAINING))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, TEMPERATURE))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, RO_FILTER_REMAINING))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, RINSE))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, WATER_USED))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, WATER_PURIFIED))
    devices.append(XiaomiWaterPurifierSensor(waterPurifier, RUN_STATUS))
except DeviceException:
    _LOGGER.exception('Fail to setup Xiaomi water purifier')
    raise PlatformNotReady

add_devices(devices)

class XiaomiWaterPurifierSensor(Entity):
"""Representation of a XiaomiWaterPurifierSensor."""

def __init__(self, waterPurifier, data_key):
    """Initialize the XiaomiWaterPurifierSensor."""
    self._state = None
    self._data = None
    self._waterPurifier = waterPurifier
    self._data_key = data_key
    self.parse_data()

@property
def name(self):
    """Return the name of the sensor."""
    return self._data_key['name']

@property
def icon(self):
    """Icon to use in the frontend, if any."""
    if self._data_key['key'] is TAP_WATER_QUALITY['key'] or \
       self._data_key['key'] is FILTERED_WATER_QUALITY['key']:
        return 'mdi:water'
    else:
        if self._data_key['key'] is TEMPERATURE['key']: return 'mdi:thermometer'
        if self._data_key['key'] is RINSE['key'] or \
        self._data_key['key'] is RUN_STATUS['key']: return 'mdi:replay'   
        return 'mdi:filter-outline'

@property
def state(self):
    """Return the state of the device."""
    return self._state

@property
def unit_of_measurement(self):
    """Return the unit of measurement of this entity, if any."""
    if self._data_key['key'] is TAP_WATER_QUALITY['key'] or \
       self._data_key['key'] is FILTERED_WATER_QUALITY['key']:
        return 'TDS'
    if self._data_key['key'] is TEMPERATURE['key']:
        return 'C°'    
    if self._data_key['key'] is RINSE['key'] or \
       self._data_key['key'] is RUN_STATUS['key']:
        return '' 
    if self._data_key['key'] is WATER_USED['key'] or \
       self._data_key['key'] is WATER_PURIFIED['key']:
        return 'L'
    return '%'

@property
def device_state_attributes(self):
    """Return the state attributes of the last update."""
    attrs = {}

    if self._data_key['key'] is PP_COTTON_FILTER_REMAINING['key'] or \
       self._data_key['key'] is RO_FILTER_REMAINING['key'] :
        attrs[self._data_key['name']] = '{} days remaining'.format(self._data[self._data_key['days_key']])

    return attrs

def parse_data(self):
    if self._waterPurifier._data:
        self._data = self._waterPurifier._data
        self._state = self._data[self._data_key['key']]

def update(self):
    """Get the latest data and updates the states."""
    self.parse_data()

class XiaomiWaterPurifier(Entity):
"""Representation of a XiaomiWaterPurifier."""

def __init__(self, device, name):
    """Initialize the XiaomiWaterPurifier."""
    self._state = None
    self._device = device
    self._name = name
    self.parse_data()

@property
def name(self):
    """Return the name of the device."""
    return self._name

@property
def icon(self):
    """Icon to use in the frontend, if any."""
    return 'mdi:water'

@property
def unit_of_measurement(self):
    """Return the unit of measurement of this entity, if any."""
    return 'TDS'

@property
def state(self):
    """Return the state of the device."""
    return self._state

@property
def hidden(self) -> bool:
    """Return True if the entity should be hidden from UIs."""
    return True

@property
def device_state_attributes(self):
    """Return the state attributes of the last update."""
    attrs = {}
    attrs[TAP_WATER_QUALITY['name']] = '{}TDS'.format(self._data[TAP_WATER_QUALITY['key']])
    attrs[PP_COTTON_FILTER_REMAINING['name']] = '{}%'.format(self._data[PP_COTTON_FILTER_REMAINING['key']])
    attrs[RO_FILTER_REMAINING['name']] = '{}%'.format(self._data[RO_FILTER_REMAINING['key']])
    attrs[TEMPERATURE['name']] = '{}C°'.format(self._data[TEMPERATURE['key']])
    attrs[RINSE['name']] = '{}'.format(self._data[RINSE['key']])
    attrs[WATER_USED['name']] = '{}'.format(self._data[WATER_USED['key']])
    attrs[WATER_PURIFIED['name']] = '{}'.format(self._data[WATER_PURIFIED['key']])
    attrs[RUN_STATUS['name']] = '{}'.format(self._data[RUN_STATUS['key']])
    return attrs

def parse_data(self):
    """Parse data."""
    try:
        data = {}           
        tap_water=self._device.send('get_prop', ["tds_in"])
        data[TAP_WATER_QUALITY['key']] =int(tap_water[0]) 
        filtered_water=self._device.send('get_prop', ["tds_out"])
        data[FILTERED_WATER_QUALITY['key']] = int(filtered_water[0]) 
        temperature= self._device.send('get_prop', ["temperature"])
        data[TEMPERATURE['key']] = int(temperature[0])
        rinse= self._device.send('get_prop', ["rinse"])
        data[RINSE['key']] = rinse[0]
        run_status=self._device.send('get_prop', ["run_status"])
        data[RUN_STATUS['key']] = run_status[0]
        
        _LOGGER.info("Initializing Xiaomi water purifier with  (water quality %s...)", data[FILTERED_WATER_QUALITY['key']] )                                                        
                                                                
        f1_totaltime=self._device.send('get_prop', ["f1_totaltime"])
        f1_usedflow=self._device.send('get_prop', ["f1_usedflow"])
        f1_totalflow=self._device.send('get_prop', ["f1_totalflow"])
        f2_totalflow=self._device.send('get_prop', ["f2_totalflow"])
        f1_usedtime=self._device.send('get_prop', ["f1_usedtime"])    
        pfd = int((f1_totaltime[0] - f1_usedtime[0]) / 24)                
        _LOGGER.info("Initializing Xiaomi water purifier with  (water pfd %s...)", pfd )      
        data[PP_COTTON_FILTER_REMAINING['days_key']] = pfd
        data[PP_COTTON_FILTER_REMAINING['key']] = math.floor(pfd * 24 * 100 /f1_totaltime[0])
                                      
        f2_totaltime=self._device.send('get_prop', ["f2_totaltime"])
        f2_usedtime=self._device.send('get_prop', ["f2_usedtime"])   
        rfd = int((f2_totaltime[0] - f2_usedtime[0]) / 24)   
        data[RO_FILTER_REMAINING['days_key']] = rfd
        data[RO_FILTER_REMAINING['key']] = math.floor(rfd * 24 * 100 /f2_totaltime[0])
        


        data[WATER_USED['key']] = int((f1_totalflow[0] + f2_totalflow[0]) /2)
        data[WATER_PURIFIED['key']] = int(f1_usedflow[0])

        
                                      
        self._data = data
        self._state = self._data[FILTERED_WATER_QUALITY['key']]
        
        
    except DeviceException:
        _LOGGER.exception('Fail to get_prop from Xiaomi water purifier')
        self._data = None
        self._state = None
        raise PlatformNotReady

def update(self):
    """Get the latest data and updates the states."""
    self.parse_data()

@Nagg35
Copy link

Nagg35 commented Oct 3, 2021

Yes it's works !
I only have to correct the indentation and the begining

_LOGGER = logging.getLogger(name) has to be _LOGGER = logging.getLogger(name)

Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants