Giter VIP home page Giter VIP logo

Comments (4)

maury77 avatar maury77 commented on August 17, 2024

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])

from homeassistant-xiaomi-water-purifier-yunmi.

Nagg35 avatar Nagg35 commented on August 17, 2024

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

from homeassistant-xiaomi-water-purifier-yunmi.

maury77 avatar maury77 commented on August 17, 2024

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()

from homeassistant-xiaomi-water-purifier-yunmi.

Nagg35 avatar Nagg35 commented on August 17, 2024

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

from homeassistant-xiaomi-water-purifier-yunmi.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.