'Update the TP-Link switch\'s state.'

def update(self):

    from pyHS100 import SmartPlugException
    try:
        self._state = (self.smartplug.state == self.smartplug.SWITCH_STATE_ON)
        if self.smartplug.has_emeter:
            emeter_readings = self.smartplug.get_emeter_realtime()
            self._emeter_params[ATTR_CURRENT_CONSUMPTION] = ('%.1f    W' % emeter_readings['power'])
            self._emeter_params[ATTR_TOTAL_CONSUMPTION] = ('%.2f    kW' % emeter_readings['total'])
            self._emeter_params[ATTR_VOLTAGE] = ('%.2f    V' % emeter_readings['voltage'])
            self._emeter_params[ATTR_CURRENT] = ('%.1f    A' % emeter_readings['current'])
            emeter_statics = self.smartplug.get_emeter_daily()
            try:
                self._emeter_params[ATTR_DAILY_CONSUMPTION] = ('%.2f    kW' % emeter_statics[int(time.strftime('%e'))])
            except KeyError:
                pass
    except (SmartPlugException, OSError) as ex:
        _LOGGER.warning('Could    not    read    state    for    %s:    %s', self.name, ex)
